Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider making serialization deterministic #233

Open
Oberdiah opened this issue Aug 10, 2023 · 0 comments
Open

Consider making serialization deterministic #233

Oberdiah opened this issue Aug 10, 2023 · 0 comments

Comments

@Oberdiah
Copy link

Oberdiah commented Aug 10, 2023

This doesn't affect functionality in any real way, but currently serialization is non-deterministic, meaning serializing, deserializing, and then serializing the world again doesn't always result in the same serialization the second time.

MRE to demonstrate: (May need to run a few times to get a fail, sometimes you'll get lucky)

pub type TestWorldRegistry = Registry!(f32, i32);
#[test]
pub fn test_serialize_world() {
	let mut world = World::<TestWorldRegistry>::new();
	world.insert(entity!(16f32));
	world.insert(entity!(32i32));

	let serialized_a = bincode::serialize(&world).unwrap();
	let deserialized_world: World<TestWorldRegistry> = bincode::deserialize(&serialized_a).unwrap();
	let serialized_b = bincode::serialize(&deserialized_world).unwrap();

	assert_eq!(serialized_a, serialized_b);
}

Possible fix that appears to work, in archetypes/impl_serde.rs @ line 30

    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut archetypes: Vec<_> = self.iter().collect();
        archetypes.sort_by(|a, b| unsafe {
            let ident1 = a.identifier();
            let ident2 = b.identifier();
            ident1.iter().cmp(ident2.iter())
        });
        serializer.collect_seq(archetypes)
    }

Of course, this does have a performance impact, but given it's unlikely for the number of archetypes to ever approach the number of entities in a scene, I believe it's negligible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant