Optimize InstantDeserializer method replaceZeroOffsetAsZIfNecessary() by schlosna · Pull Request #266 · FasterXML/jackson-modules-java8 (original) (raw)
While reviewing some JFRs from some services that heavily use Jackson for deserializing timestamps, I noticed that com.fasterxml.jackson.datatype.jsr310.deser.InstantDeserializer.replaceZeroOffsetAsZIfNecessary(String) was allocating a lot of int[] from its use of java.util.regex.Pattern.matcher(CharSequence) to determine whether an input String is a zero offset and if so replace it with Z.
I would like to propose an alternative implementation to avoid expensive regex matcher allocations when replacing zero offset with 'Z'.
Using a JMH benchmark with a variety of generated timestamps I see the following results on x64 (~7x speedup) and aarch64 (~2.5x speedup):
OpenJDK 17.0.6 on x64 Intel(R) Xeon(R) Platinum 8259CL CPU @ 2.50GHz
Benchmark Mode Cnt Score Error Units
InstantDeserializerBenchmark.index avgt 5 19.543 ± 1.384 ns/op
InstantDeserializerBenchmark.regex avgt 5 155.081 ± 3.234 ns/op
OpenJDK 17.0.6 on aarch64 Apple M1 Pro
Benchmark Mode Cnt Score Error Units
InstantDeserializerBenchmark.index avgt 5 16.855 ± 0.537 ns/op
InstantDeserializerBenchmark.regex avgt 5 58.155 ± 1.444 ns/op
