Skip to content

Conversation

@TehPers
Copy link
Contributor

@TehPers TehPers commented Apr 10, 2025

Objective

This allows users to serialize and deserialize both prehashed values and maps/sets with prehashed keys, avoiding the need for a conversion.

Solution

When serializing, only the inner stored value (of type V) is serialized. The struct acts as a transparent wrapper around it, so the Hashed struct is invisible to serde.

When deserializing, the reverse happens: the inner value is deserialized, then the Hashed struct is initialized from the deserialized value, computing the hash at that time. This means that the computed hash may differ between what was serialized and what was deserialized if the hasher produces two different values at those times - for example, across different runs of the application using different versions of bevy. This should normally not matter since the hash usually only needs to be consistent in the same run of the application.

Testing

There are unit tests added for both Hashed<V, FixedHasher> and HashMap<Hashed<K, FixedHasher>, V, PreHash>. The tests for the hashmap are to ensure that PreHashMap can be serialized/deserialized as well, and serializes in the expected format.

Some other bevy crates depend on serde_test, but that crate appears to be unmaintained now, so I'm using serde_json for testing instead.

@TehPers TehPers marked this pull request as draft April 10, 2025 04:44
@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events C-Performance A change motivated by improving speed, memory usage or compile times A-Utils Utility functions and types S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged and removed A-ECS Entities, components, systems, and events labels Apr 10, 2025
@TehPers TehPers marked this pull request as ready for review April 10, 2025 05:47
@TehPers TehPers force-pushed the tehpers/impl-serde-for-hashed branch from bdf1347 to a431a0d Compare April 18, 2025 04:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Utils Utility functions and types C-Performance A change motivated by improving speed, memory usage or compile times S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged

2 participants