Skip to content

Enhance buffer handling to reduce direct list access. #1675

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

vbabanin
Copy link
Member

@vbabanin vbabanin commented Apr 5, 2025

Summary

The PR improves buffer handling by introducing a currentByteBuffer field to cache the current ByteBuf, reducing direct list access which is costly because of bounds checking. Additionally, caching currentByteBuffer helps with JIT (Just-In-Time) inlining based on compilation profiling. By making the buffer access more predictable and, the JIT compiler can better inline the getCurrentByteBuffer method, leading to improved runtime performance.

Test Name Base Mean Compare Mean Perf Diff Percent Z-Score
Deep BSON Encoding 62.2417 71.5916 15.02% 6.7074
Flat BSON Encoding 133.9693 172.8321 29.01% 10.3677
Full BSON Encoding 139.8435 172.5080 23.36% 20.1526
Large doc Client BulkWrite insert 56.1605 62.9421 13.57% 12.0393
Large doc Collection BulkWrite insert 62.0188 72.3853 16.71% 17.0376
Large doc bulk insert 60.6105 70.3881 16.13% 15.0138
Large doc insertOne 63.2810 73.0264 15.40% 11.1243

Perf analyzer results: Link

JAVA-5846

@vbabanin vbabanin self-assigned this Apr 5, 2025
@vbabanin vbabanin requested review from a team and katcharov and removed request for a team April 8, 2025 05:47
@vbabanin vbabanin marked this pull request as ready for review April 8, 2025 15:07
Copy link
Collaborator

@katcharov katcharov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Let's add a secondary.

@vbabanin vbabanin requested a review from nhachicha April 12, 2025 05:18
Copy link
Contributor

@nhachicha nhachicha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM shouldn't we release the cached buffer when calling close? (the underlying Native C/C++ buffer could be reference counted and needs to drop to 0 to actually free the memory)

@vbabanin
Copy link
Member Author

vbabanin commented Apr 15, 2025

LGTM shouldn't we release the cached buffer when calling close? (the underlying Native C/C++ buffer could be reference counted and needs to drop to 0 to actually free the memory)

In this case, currentByteBuffer is already part of bufferList, and we don’t retain any additional reference once it's cached, so it should be released as expected a part of bufferList traversal.

That said, this is a good point. It made me realize it would be clearer to explicitly nullify the currentByteBuffer field in close(), just as we already call bufferList.clear(). I’ll update accordingly - thanks! Let me know what you think.

@vbabanin vbabanin requested a review from nhachicha April 15, 2025 21:54
Copy link
Contributor

@nhachicha nhachicha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

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.

3 participants