Skip to content
Merged
Changes from 6 commits
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
49 changes: 48 additions & 1 deletion bson/src/main/org/bson/io/ByteBufferBsonInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ public String readString() {

@Override
public String readCString() {
ensureOpen();
int size = computeCStringLength(buffer.position());
return readString(size);
}
Expand Down Expand Up @@ -182,11 +183,57 @@ 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://en.wikipedia.org/wiki/SWAR">
*/
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, 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, 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.
*/
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.
while (pos < limit) {
if (buffer.get(pos++) == 0) {
return (pos - prevPos);
Expand Down