ezhou413 commented on code in PR #23483:
URL: https://github.com/apache/kafka/pull/23483#discussion_r4124921908
##########
clients/src/main/java/org/apache/kafka/clients/producer/internals/ChunkedByteBufferOutputStream.java:
##########
@@ -237,15 +261,32 @@ private void releaseUnusedChunks() {
if (currentChunk == null) // already deallocated; nothing attached
return;
List<ByteBuffer> unused = chunks.subList(currentChunkIndex + 1,
chunks.size());
- if (pool != null) {
- for (ByteBuffer chunk : unused)
+ for (ByteBuffer chunk : unused) {
+ boolean poolOwned = removeByIdentity(poolAllocatedChunks, chunk);
+ if (poolOwned && pool != null)
pool.deallocate(chunk);
}
// Remove the released chunks from `chunks`, so they are
// not deallocated again on batch completion.
unused.clear();
}
+ /**
+ * Removes the first element identical ({@code ==}) to {@code target} from
{@code list}, returning
+ * whether it was present. Uses reference identity rather than {@link
Object#equals} because
+ * {@link ByteBuffer#equals} compares contents, which would match the
wrong chunk (e.g. two empty
+ * chunks compare equal).
+ */
+ private static boolean removeByIdentity(List<ByteBuffer> list, ByteBuffer
target) {
Review Comment:
Good call, didn't know about this. thanks!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]