In the game I'm working on, I have a set of images representing the foreground and background, as well as entity placements around the map. I allow any image format supported by the Image crate. This is a convenient format for map-making, but I have a separate utility which precompiles the map into its canonical structure and serializes it to a file:
#[derive(Serialize, Deserialize)]
pub struct Graphics {
pub z0: Vec<Color>,
}
#[derive(Serialize, Deserialize)]
pub struct Map {
pub width: usize,
pub height: usize,
pub graphics: Graphics,
pub player_spawn_points: [Position; 8],
pub weapon_spawn_points: Vec<(Position, Weapon)>,
}
Currently I'm just testing with only the foreground included and it takes 15 seconds on a release build to load and deserialize this structure for a 3,264px x 2,448px image. Loading the original image as an uncompressed bitmap only takes 3 seconds.
I'm sure there are more clever ways to represent the map graphics, but even so the time taken here is very surprising. Am I choosing the wrong tool for reading in and writing out a large amount of binary data?
perf
output:
- 100.00% map_compiler β
- 46.79% map_compiler β
15.76% [.] serde_cbor::de::Deserializer<R>::parse_value β
12.87% [.] <serde_cbor::read::IoRead<R> as serde_cbor::read::Read>::read_into β
12.31% [.] serde_cbor::read::IoRead<R>::next_inner β
5.84% [.] serde_cbor::de::Deserializer<R>::parse_array β
0.01% [.] <serde_cbor::read::IoRead<R> as serde_cbor::read::Read>::read_to_buffer β
0.00% [.] core::str::from_utf8 β
- 41.50% [unknown] β
41.08% [k] 0xffffffffa9400163 β
0.42% [k] 0xffffffffa9400b07 β
- 11.68% libpthread-2.30.so β
11.68% [.] __libc_read β
+ 0.02% ld-2.30.so β
+ 0.01% libc-2.30.so
It does seem that about half the time is spent on syscalls according to time
as well. I stripped out everything from the map_compiler
binary except the actual deserialization call:
fn main() {
let b: Map = serde_cbor::from_reader(std::fs::File::open("maps/zorf").unwrap()).unwrap();
}
target/release/map_compiler 8.60s user 8.32s system 99% cpu 16.938 total