msgpack-java icon indicating copy to clipboard operation
msgpack-java copied to clipboard

Improvement: Add option to cache byte[] to String mappings for Key

Open Matulis opened this issue 5 years ago • 3 comments

In some cases when you know you have a limited set of keys and have a very large volume of incoming messages sharing the same keys, it may be beneficial to store a mapping of byte[] to Strings, this can reduce the object creation from decoding as well as allow you to utilize the same string objects in memory.

For example Jackson allows the JsonFactory feature flag of: https://fasterxml.github.io/jackson-core/javadoc/2.8/com/fasterxml/jackson/core/JsonFactory.Feature.html#INTERN_FIELD_NAMES

Matulis avatar Apr 18 '19 17:04 Matulis

Implementation could be pretty simple, but would be nice to add a dependency on a cache library(Guava Cache or Caffeine) to avoid having to roll a new one, thoughts?

Matulis avatar Apr 18 '19 17:04 Matulis

Actually I just realized that the actual implementation of the cache can be left up to the user, so I will submit a pull request with the interface that can optionally be implemented by end users

Matulis avatar Apr 18 '19 17:04 Matulis

Interesting. I guess, what you want to do is like this?

https://github.com/FasterXML/jackson-core/blob/e3574ea3db06c7e69625b5962a34430e89011ac2/src/main/java/com/fasterxml/jackson/core/sym/CharsToNameCanonicalizer.java#L485-L487

Maybe you can use this one as a cache w/o additional dependency

https://github.com/FasterXML/jackson-core/blob/e3574ea3db06c7e69625b5962a34430e89011ac2/src/main/java/com/fasterxml/jackson/core/util/InternCache.java#L29

komamitsu avatar Apr 23 '19 16:04 komamitsu