Skip to content

Fix #1186: improve recycled buffer release logic #1187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ a pure JSON library.
#1179: Allow configuring `DefaultPrettyPrinter` separators for empty
Arrays and Objects
(contributed by Guillaume L)
#1186: `BufferRecycler` should avoid setting replacement if one already returned, bigger
(suggested by @kkkkkhhhh)

2.16.2 (not yet released)

Expand Down
14 changes: 12 additions & 2 deletions src/main/java/com/fasterxml/jackson/core/util/BufferRecycler.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,12 @@ public byte[] allocByteBuffer(int ix, int minSize) {
}

public void releaseByteBuffer(int ix, byte[] buffer) {
_byteBuffers.set(ix, buffer);
// 13-Jan-2024, tatu: [core#1186] Replace only if beneficial:
byte[] oldBuffer = _byteBuffers.get(ix);
if ((oldBuffer == null) || buffer.length > oldBuffer.length) {
// Could use CAS, but should not really matter
_byteBuffers.set(ix, buffer);
}
}

/*
Expand All @@ -171,7 +176,12 @@ public char[] allocCharBuffer(int ix, int minSize) {
}

public void releaseCharBuffer(int ix, char[] buffer) {
_charBuffers.set(ix, buffer);
// 13-Jan-2024, tatu: [core#1186] Replace only if beneficial:
char[] oldBuffer = _charBuffers.get(ix);
if ((oldBuffer == null) || buffer.length > oldBuffer.length) {
// Could use CAS, but should not really matter
_charBuffers.set(ix, buffer);
}
}

/*
Expand Down