Skip to content

Commit

Permalink
Fix #1186: improve recycled buffer release logic (#1187)
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder authored Jan 14, 2024
1 parent c844c60 commit ec4ef5e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
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

0 comments on commit ec4ef5e

Please sign in to comment.