Release Jackson BufferRecycler to its pool in encoders#37059
Merged
bclozel merged 1 commit intoJul 16, 2026
Conversation
7745212 to
7a3adb1
Compare
5ec43f3 to
3b84f6d
Compare
Both encoders acquire a pooled BufferRecycler via factory._getBufferRecycler() but never return it. Jackson 3 changed the default pool from a ThreadLocal to a per-factory ConcurrentDequePool that only refills on an explicit releaseToPool(), so the gap leaves the pool empty and every encode allocates a fresh recycler and buffers. Hold the recycler in a local and release it once the generator and byte builder are done: in encodeValue's finally, and in the streaming path's doAfterTerminate after generator.close() and byteBuilder.release(). releaseToPool() is idempotent, so sharing the recycler with the generator cannot double-release. Closes spring-projectsgh-37059 Signed-off-by: samlightfoot <samueldlightfoot@gmail.com>
3b84f6d to
a2feb9f
Compare
Member
|
Thanks so much @samueldlightfoot for your contribution! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Both encoders acquire a pooled BufferRecycler via factory._getBufferRecycler() but never return it. Jackson 3 changed the default pool from a ThreadLocal to a per-factory ConcurrentDequePool that only refills on an explicit releaseToPool(), so the gap leaves the pool empty and every encode allocates a fresh recycler and buffers.
Hold the recycler in a local and release it once the generator and byte builder are done: in encodeValue's finally, and in the streaming path's doAfterTerminate after generator.close() and byteBuilder.release(). releaseToPool() is idempotent, so sharing the recycler with the generator cannot double-release.
Allocation profile
Setup: Bytes allocated per
encodeValuecall, measured with HotSpotThreadMXBean.getThreadAllocatedBytes()single-threaded at steady state (200k warmup, 500k ops/round × 7, median), using a default Jackson 3JsonMapper; the only difference between arms is the addedBufferRecycler.releaseToPool().The fix removes a fixed ~18 KB of recycler-buffer allocation per encode — constant in absolute terms, so the percentage just tracks payload size.