k-paxian / dart-json-mapper

Serialize / Deserialize Dart Objects to / from JSON
https://pub.dev/packages/dart_json_mapper
Other
399 stars 33 forks source link

performance optimizations #176

Closed kennelken closed 2 years ago

kennelken commented 2 years ago

Hello Alexander! Let me introduce myself. My name is Sergey, mainly c# developer but now i'm actively developing flutter-based projects as a hobby.

Thank you a lot for the great job creating this library and making it public! I find its set of features much better than the official flutter json solution. Using it is many many times more pleasant! I would really love to see how the project evolves! Unfortunately at the very end of working on my current project i found out that the performance on a real json-data is not that good. In my case it was 6k lines of 'beautified' json text.

I would like to suggest you some ideas for optimizing the package. In my benchmark on my project data i achieved significant performance boost with these simple caching tricks: about -85% deserialization time (+700% deserialization performance) and -45% serialization time (+85% serialization performance). I attached some profiler data for comparison. The benchmark was: serialized my json-string 10 times, deserialized my json-string 10 times. For my real-life application the changes i suggest shortened the startup time from 8.5 seconds to 3 seconds which i think is super nice result.

Although i find it important to share the results with you, i should warn you that i spent just about 4 hours for examining the project and making changes / measuring the results what in my opinion might be not enough for making sure that everything still works as expected especially considering the project logic complexity.

In the end i want to thank you again for your amazing work!

Kind regards Sergey

profilrer_data.zip

chrome_gNoEObuL8z chrome_z8flAxhWeD chrome_jDNTS4n9mj chrome_Gc3KihPPUB .

k-paxian commented 2 years ago

Hey Sergey,

Thank you so much for your time & contribution, the changes proposed makes a lot of sense to me 👍 however, running on your branch I have noticed broken 66 unit tests. After a brief pick into the context haven't found the root cause yet, it's definitely needs more time for investigation. From what I get so far is a slight mess in the ClassInfo data, somehow ClassInfo for class Car shows different set of class members than it should 🤷

image

Hope we could work out the root cause together, what do you think? so we could adopt & release the proposed changes.

kennelken commented 2 years ago

Hey Alexander,

Too bad i have not noticed the unit tests. I will try to figure out what is wrong and let you know as soon as i manage to.

kennelken commented 2 years ago

I pushed the commit that fixes the Unit-tests. It includes some rules to invalidate cache when it's needed. 1) this is a simple one: i just wipe the cache every time DefaultTypeInfoDecorator is initialized. image 2) this one is tricky. It looks for me like ClassMirror class can be changed for a class during runtime so there are two possible ways to solve it - find a proper moment to wipe the cache, or make sure that the cached value is up to date. I chose the second approach beacause it was simplier. image

I checked the performance again after this commit and it seemed that the changes did not affect it much.

k-paxian commented 2 years ago

Thank you so much for your involvement, your amazing changes has been released 🥳 Looking forward for more contributions to come ❤️