orlp / slotmap

Slotmap data structure for Rust
zlib License
1.12k stars 70 forks source link

Does serializing a slotmap result in old keys becoming available again? #86

Closed zesterer closed 2 years ago

zesterer commented 2 years ago

Hello,

I'm using the serde feature to serialize both keys and a SlotMap. When deserializing, I want to be sure that old keys (i.e: those that no longer correspond to an entry in the SlotMap) are not able to accidentally reoccur when inserting new items. Is it the case that slotmap will ensure that old index + generation pairs cannot be recreated, even when serializing and then deserializing?

orlp commented 2 years ago

Is it the case that slotmap will ensure that old index + generation pairs cannot be recreated, even when serializing and then deserializing?

Yes, the version for each slot is included in the serialization format, and can only ever be incremented*. So old keys remain invalid.

* In the current version of slotmap if a slot gets re-used 2^31 times the version field will overflow and theoretically very old keys would become temporarily valid again (only one at a time). In the future I plan to 'retire' such a slot instead, simply never re-using it again. This technically would leak memory, but only one slot for every 2^31 insertion/deletion pairs on average, so well-worth the trade-off to never ever accidentally validate old keys.

zesterer commented 2 years ago

Thanks for the clarification!