Skip to content

Commit

Permalink
JAVA-5788 Latin heap array fast path
Browse files Browse the repository at this point in the history
  • Loading branch information
franz1981 committed Feb 12, 2025
1 parent 2049481 commit 82782ee
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 2 deletions.
7 changes: 7 additions & 0 deletions bson/src/main/org/bson/ByteBuf.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ public interface ByteBuf {
*/
ByteBuf flip();

/**
* States whether this buffer is backed by an accessible byte array.
*
* @return {@code true} if, and only if, this buffer is backed by an array and is not read-only
*/
boolean hasArray();

/**
* <p>Returns the byte array that backs this buffer <em>(optional operation)</em>.</p>
*
Expand Down
5 changes: 5 additions & 0 deletions bson/src/main/org/bson/ByteBufNIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ public ByteBuf flip() {
return this;
}

@Override
public boolean hasArray() {
return buf.hasArray();
}

@Override
public byte[] array() {
return buf.array();
Expand Down
8 changes: 6 additions & 2 deletions bson/src/main/org/bson/io/OutputBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,15 @@ public void writeLong(final long value) {
writeInt64(value);
}

private int writeCharacters(final String str, final boolean checkForNullCharacters) {
protected int writeCharacters(final String str, final boolean checkForNullCharacters) {
return writeCharacters(str, 0, checkForNullCharacters);
}

protected final int writeCharacters(final String str, int start, final boolean checkForNullCharacters) {
int len = str.length();
int total = 0;

for (int i = 0; i < len;) {
for (int i = start; i < len;) {
int c = Character.codePointAt(str, i);

if (checkForNullCharacters && c == 0x0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.mongodb.internal.connection;

import org.bson.BsonSerializationException;
import org.bson.ByteBuf;
import org.bson.io.OutputBuffer;

Expand All @@ -27,6 +28,7 @@

import static com.mongodb.assertions.Assertions.assertTrue;
import static com.mongodb.assertions.Assertions.notNull;
import static java.lang.String.format;

/**
* <p>This class is not part of the public API and may be removed or changed at any time</p>
Expand Down Expand Up @@ -273,6 +275,42 @@ public void close() {
}
}

@Override
protected int writeCharacters(final String str, final boolean checkForNullCharacters) {
ensureOpen();
ByteBuf buf = getCurrentByteBuffer();
int i = 0;
if (buf.hasArray() && (buf.remaining() >= str.length() + 1)) {
byte[] array = buf.array();
int pos = buf.position();
int len = str.length();
for (;i < len; i++) {
char c = str.charAt(i);
if (checkForNullCharacters && c == 0x0) {
throw new BsonSerializationException(format("BSON cstring '%s' is not valid because it contains a null character "
+ "at index %d", str, i));
}
if (c > 0x80) {
break;
}
array[pos + i] = (byte) c;
}
if (i == len) {
int total = len + 1;
array[pos + len] = 0;
position += total;
buf.position(pos + total);
return len + 1;
}
// ith character is not ASCII
if (i > 0) {
position += i;
buf.position(pos + i);
}
}
return i + super.writeCharacters(str, i, checkForNullCharacters);
}

private static final class BufferPositionPair {
private final int bufferIndex;
private int position;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,11 @@ private int getShort(final int index) {
return (short) (get(index) & 0xff | (get(index + 1) & 0xff) << 8);
}

@Override
public boolean hasArray() {
return false;
}

@Override
public byte[] array() {
throw new UnsupportedOperationException("Not implemented yet!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ public ByteBuf flip() {
return this;
}

public boolean hasArray() {
return proxied.hasArray();
}

@Override
public byte[] array() {
return proxied.array();
Expand Down

0 comments on commit 82782ee

Please sign in to comment.