Cysharp / MemoryPack

Zero encoding extreme performance binary serializer for C# and Unity.
MIT License
3.25k stars 190 forks source link

feat: MemoryPackSerializer.ResetState, ResetReaderState, ResetWriterState #288

Closed alexyakunin closed 2 months ago

alexyakunin commented 5 months ago

Sometimes you want to invoke MemoryPackSerializer.Serialize while serializing another type (e.g. to serialize a complex nested structure into a byte array on serialization). And sometimes you want to do the same on deserialization. Since MemoryPack uses [ThreadStatic] to store its serializer/deserializer state, this doesn't work - unless you backup its state & reset it.

The provided implementation does exactly this. Currently we have to resort to CreateDelegate/ILEmit-based workaround to address that, this change would address the problem in more straightforward way.

neuecc commented 5 months ago

Thanks, it seems like a good approach. Could you please add unit tests and sample code to the README?

alexyakunin commented 5 months ago

Will do somewhere on this week. Thanks!

neuecc commented 2 months ago

Well, I don't think there's any particular problem, so I'll try merging it for now.

alexyakunin commented 1 month ago

Thanks - yes, sorry, didn't have a chance to get back to that. I'm 100% for tests, but I guess this specific thing is fine to be uncovered for now: it doesn't affect the primary logic + if it doesn't work, you'll get another bug report quite soon ;)