This repository was archived by the owner on Nov 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathserialize_binary.rs
75 lines (60 loc) · 1.82 KB
/
serialize_binary.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use legion::*;
use serde::{de::DeserializeSeed, Deserialize, Serialize};
#[derive(Default, Copy, Clone, Serialize, Deserialize)]
struct Transform([f32; 16]);
#[derive(Default, Copy, Clone, Serialize, Deserialize)]
struct Position {
x: f32,
y: f32,
z: f32,
}
#[derive(Default, Copy, Clone, Serialize, Deserialize)]
struct Rotation {
x: f32,
y: f32,
z: f32,
}
#[derive(Default, Copy, Clone, Serialize, Deserialize)]
struct Velocity {
x: f32,
y: f32,
z: f32,
}
pub struct Benchmark(World, Registry<u8>);
impl Benchmark {
pub fn new() -> Self {
let mut world = World::default();
world.extend(
(
vec![Transform::default(); 1000],
vec![Position::default(); 1000],
vec![Rotation::default(); 1000],
vec![Velocity::default(); 1000],
)
.into_soa(),
);
let mut registry = Registry::default();
registry.register::<Transform>(0);
registry.register::<Position>(1);
registry.register::<Rotation>(2);
registry.register::<Velocity>(3);
Self(world, registry)
}
pub fn run(&mut self) {
let Self(world, registry) = self;
let mut canon = legion::serialize::Canon::default();
let serializable = &world.as_serializable(any(), &*registry, &mut canon);
let encoded = bincode::serialize(serializable).unwrap();
use bincode::config::Options;
let mut deserializer = bincode::de::Deserializer::from_slice(
&encoded[..],
bincode::config::DefaultOptions::new()
.with_fixint_encoding()
.allow_trailing_bytes(),
);
registry
.as_deserialize(&mut canon)
.deserialize(&mut deserializer)
.unwrap();
}
}