Skip to content

Optimize String length computation. #1685

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 19 commits into from
May 2, 2025
Merged
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
47 changes: 47 additions & 0 deletions bson/src/main/org/bson/io/ByteBufferBsonInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,58 @@ public void skipCString() {
buffer.position(pos + length);
}

/**
* Detects the position of the first NULL (0x00) byte in a 64-bit word using SWAR technique.
* <a href="https://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord">
*/
private int computeCStringLength(final int prevPos) {
ensureOpen();
int pos = buffer.position();
int limit = buffer.limit();

int chunks = (limit - pos) >>> 3;
// Process 8 bytes at a time.
for (int i = 0; i < chunks; i++) {
long word = buffer.getLong(pos);
/*
Subtract 0x0101010101010101L to cause a borrow on 0x00 bytes.
if original byte is 00000000 -> 00000000 - 00000001 = 11111111 (borrow causes high bit set to 1).
Copy link
Contributor

Choose a reason for hiding this comment

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

Optional. A little bit less clear than

if original byte is 00000000, then 00000000 - 00000001 = 11111111 (borrow causes high bit set to 1).

*/
long mask = word - 0x0101010101010101L;
/*
mask will only have high bits set iff it was a 0x00 byte (0x00 becomes 0xFF because of the borrow).
~word will have bits that were originally 0 set to 1.
mask & ~word will have high bits set iff original byte was 0x00.
*/
mask &= ~word;
/*
0x8080808080808080:
10000000 10000000 10000000 10000000 10000000 10000000 10000000 10000000

mask:
00000000 00000000 11111111 00000000 00000001 00000001 00000000 00000111

ANDing mask with 0x8080808080808080 isolates the high bit (0x80) in positions where
the original byte was 0x00, setting the high bit to 1 only at the 0x00 byte position.
Copy link
Contributor

Choose a reason for hiding this comment

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

Optional. maybe the following verbiage is little bit clearer:

, by setting the high bit to 1 only at the 0x00 byte position.


result:
00000000 00000000 10000000 00000000 00000000 00000000 00000000 00000000
^^^^^^^^
The high bit is set only at the 0x00 byte position.
Copy link
Contributor

Choose a reason for hiding this comment

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

Optional. the above comment seems duplicated and could be deleted

*/
mask &= 0x8080808080808080L;
if (mask != 0) {
/*
* Performing >>> 3 (i.e., dividing by 8) gives the byte offset from the least significant bit (LSB).
*/
int offset = Long.numberOfTrailingZeros(mask) >>> 3;
// Find the NULL terminator at pos + offset
return (pos - prevPos) + offset + 1;
}
pos += 8;
}

// Process remaining bytes one-by-one.
Copy link
Contributor

Choose a reason for hiding this comment

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

seems one-by-one is more commonly used as one by one

while (pos < limit) {
if (buffer.get(pos++) == 0) {
return (pos - prevPos);
Expand Down