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.
Thanks,
Matteo.