On Sun, Nov 17, 2019 at 10:27 PM Matteo Olivi <[email protected]> wrote: > > Hello, > Do Jackson ObjectMappers cache serialized objects? > > I'm writing a benchmark to test which serialization format is faster. For > each serialization format, one ObjectMapper for the format is instantiated > (by passing the factory for the given serialization format to the > ObjectMapper constructor). The same ObjectMapper instance is then used to > serialize the same object multiple times, and my code prints the time it took > to serialize the object for each iteration. I've noticed that for every > format the first iteration always takes way longer than the others (e.g. 83 > ms at first iteration vs =~ 20ms at others). This is true when deserializing > objects too. > > IMO there are two possible reasons: > 1) The JVM or the ObjectMapper allocates memory for some data structure > lazily, and this needs to be done only once, so the first iteration always > takes longer. > 2) The ObjectMapper keeps a cache of serialized objects. If this is true the > results printed from my benchmark are garbage. > > I did a test where at every iteration one field of the object to serialize is > mutated, and I observed the same phenomena (slow first iteration), so this > seems to rule out caching, but I want to be 100% confident.
ObjectMapper does not cache objects it serializers, so benchmark is not flawed in that way. But there is definitely quite a bit first-time work to be done for constructing all handlers for given type, including serializer(s): these are cached. In addition the thing to know about JVM benchmarking is that there is a lot of optimization for byte code that happens over time, and not right away: for example just-in-time compilation of (some of) byte code into native code. So it is very typical for initial run times (for first couple of seconds) to be much slower than later runs. I hope this helps, -+ Tatu +- -- You received this message because you are subscribed to the Google Groups "jackson-user" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/jackson-user/CAL4a10g%2BZOF1A%2BrtzZ122wu%3D3fyauzPCZcGdDxL8Ra9rCCy7HA%40mail.gmail.com.
