Skip to content

perf(json): cache JsonMapper instance in jsonMapper()#718

Open
vincerevu wants to merge 2 commits intoopenai:mainfrom
vincerevu:bolt-optimize-jsonmapper-5104654577987768391
Open

perf(json): cache JsonMapper instance in jsonMapper()#718
vincerevu wants to merge 2 commits intoopenai:mainfrom
vincerevu:bolt-optimize-jsonmapper-5104654577987768391

Conversation

@vincerevu
Copy link
Copy Markdown

Summary

This PR optimizes jsonMapper() by returning a single lazily initialized and cached JsonMapper instance instead of building a new mapper on every call.

What changed

  • replaced per-call JsonMapper.builder()...build() construction with a cached singleton-style mapper
  • kept mapper configuration unchanged while ensuring initialization happens only once

Why

Building a Jackson mapper is relatively expensive because it registers modules and applies configuration during construction. Since jsonMapper() can be called frequently across parsing and serialization paths, recreating the mapper on every invocation adds unnecessary CPU and allocation overhead.

Impact

  • reduces repeated mapper construction cost
  • lowers CPU and memory allocation overhead for repeated jsonMapper() calls
  • preserves behavior while improving efficiency

Notes

Jackson ObjectMapper / JsonMapper instances are thread-safe after configuration, so reusing a single initialized instance is safe here.

Verification

  • reviewed the refactor to confirm mapper configuration is preserved
  • confirmed initialization now occurs only once
  • expected performance gains are concentrated in code paths that call jsonMapper() repeatedly

Cache the Jackson JsonMapper instance in a lazy val to avoid the massive performance overhead of registering modules and building the mapper on every single invocation. This single lazily-initialized instance is thread-safe and can be reused globally.
Cache the Jackson JsonMapper instance in a lazy val to avoid the massive performance overhead of registering modules and building the mapper on every single invocation. This single lazily-initialized instance is thread-safe and can be reused globally.
@vincerevu vincerevu requested a review from a team as a code owner April 11, 2026 02:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant