owengage / fastnbt

Fast serde serializer and deserializer for Minecraft's NBT and Anvil formats
MIT License
186 stars 34 forks source link

Make it more ergonomic to have a chunk own its data #20

Closed owengage closed 3 years ago

owengage commented 3 years ago

Because a chunk is typically from compressed data, it often gets stored in a dynamically allocated slice or some sort.

Users of fastnbt then need to store this vec with the chunk somehow. If a chunk could somehow own its buffer it might be more ergonomic to use in some use cases.

Badel2 commented 3 years ago

This is a problem of zero-copy deserialization, it can be solved by duplicating all the struct definitions and adding a variant that owns its data. This new variant could also be used to mutate and serialize chunk data. The best way to do this is using a macro to avoid duplicating code but I don't know if that macro exists. The simple alternative is to use Cow in all the fields, but that has a small runtime overhead.

This should be a common problem in the ecosystem, but I don't know how it has been solved by other projects. As for the user experience, see this stackoverflow answer which suggests using the rental crate:

https://stackoverflow.com/a/43707940

owengage commented 3 years ago

Chunks now own their data. There wasn'tuch measurable performance benefit as it turns out.