Skip to content

Commit 43f1663

Browse files
committed
Merge branch 'main' into string-opt
# Conflicts: # driver-core/src/test/unit/com/mongodb/internal/connection/ByteBufferBsonOutputTest.java
2 parents 3ff0644 + 1c3fdc8 commit 43f1663

File tree

69 files changed

+1393
-208
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+1393
-208
lines changed

.evergreen/.evg.yml

+21
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,8 @@ functions:
770770
type: test
771771
params:
772772
working_dir: "src"
773+
env:
774+
PROVIDER: ${PROVIDER}
773775
script: |
774776
${PREPARE_SHELL}
775777
PROJECT_DIRECTORY=${PROJECT_DIRECTORY} .evergreen/run-perf-tests.sh
@@ -1540,6 +1542,22 @@ tasks:
15401542
# Do not rename this task – renaming resets the performance time series
15411543
- name: "perf"
15421544
tags: ["perf"]
1545+
# Benchmark could exceed 1 hour of execution.
1546+
exec_timeout_secs: 7200
1547+
commands:
1548+
- func: "bootstrap mongo-orchestration"
1549+
vars:
1550+
VERSION: "v8.0-perf"
1551+
TOPOLOGY: "server"
1552+
SSL: "nossl"
1553+
AUTH: "noauth"
1554+
- func: "run perf tests"
1555+
- func: "send dashboard data"
1556+
1557+
- name: "perf-netty"
1558+
tags: [ "perf" ]
1559+
# Benchmark could exceed 1 hour of execution.
1560+
exec_timeout_secs: 7200
15431561
commands:
15441562
- func: "bootstrap mongo-orchestration"
15451563
vars:
@@ -1548,6 +1566,8 @@ tasks:
15481566
SSL: "nossl"
15491567
AUTH: "noauth"
15501568
- func: "run perf tests"
1569+
vars:
1570+
PROVIDER: "Netty"
15511571
- func: "send dashboard data"
15521572

15531573
- name: "aws-lambda-deployed-task"
@@ -2263,6 +2283,7 @@ buildvariants:
22632283
run_on: rhel90-dbx-perf-large
22642284
tasks:
22652285
- name: "perf"
2286+
- name: "perf-netty"
22662287

22672288
- name: plain-auth-test
22682289
display_name: "PLAIN (LDAP) Auth test"

.evergreen/run-perf-tests.sh

+7-1
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,14 @@ RELATIVE_DIR_PATH="$(dirname "${BASH_SOURCE:-$0}")"
1717
export TEST_PATH="${PROJECT_DIRECTORY}/driver-performance-test-data/"
1818
export OUTPUT_FILE="${PROJECT_DIRECTORY}/results.json"
1919

20+
if [ "${PROVIDER}" = "Netty" ]; then
21+
TASK="driver-benchmarks:runNetty"
22+
else
23+
TASK="driver-benchmarks:run"
24+
fi
25+
2026
start_time=$(date +%s)
21-
./gradlew -Dorg.mongodb.benchmarks.data=${TEST_PATH} -Dorg.mongodb.benchmarks.output=${OUTPUT_FILE} driver-benchmarks:run
27+
./gradlew -Dorg.mongodb.benchmarks.data=${TEST_PATH} -Dorg.mongodb.benchmarks.output=${OUTPUT_FILE} ${TASK}
2228
end_time=$(date +%s)
2329
elapsed_secs=$((end_time-start_time))
2430

bom/build.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ dependencies {
4040
api(project(":bson-kotlinx"))
4141
api(project(":driver-kotlin-coroutine"))
4242
api(project(":driver-kotlin-sync"))
43+
api(project(":driver-kotlin-extensions"))
4344

4445
api(project(":bson-scala"))
4546
api(project(":driver-scala"))

bson/src/main/org/bson/BsonBinaryWriter.java

+39-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.List;
2525
import java.util.Stack;
2626

27+
import static java.lang.Math.max;
2728
import static java.lang.String.format;
2829
import static org.bson.assertions.Assertions.notNull;
2930

@@ -37,8 +38,37 @@ public class BsonBinaryWriter extends AbstractBsonWriter {
3738

3839
private final BsonOutput bsonOutput;
3940
private final Stack<Integer> maxDocumentSizeStack = new Stack<>();
41+
private static final int ARRAY_INDEXES_CACHE_SIZE = 1000;
42+
private static final byte[] ARRAY_INDEXES_BUFFER;
43+
private static final int[] ARRAY_INDEXES_OFFSETS;
44+
private static final int[] ARRAY_INDEXES_LENGTHS;
4045
private Mark mark;
4146

47+
static {
48+
ARRAY_INDEXES_LENGTHS = new int[ARRAY_INDEXES_CACHE_SIZE];
49+
ARRAY_INDEXES_OFFSETS = new int[ARRAY_INDEXES_CACHE_SIZE];
50+
int totalSize = 0;
51+
for (int i = 0; i < ARRAY_INDEXES_CACHE_SIZE; i++) {
52+
totalSize += (int) (Math.log10(max(i, 1))
53+
+ 1 // number of digits
54+
+ 1); // +1 for null terminator
55+
}
56+
ARRAY_INDEXES_BUFFER = new byte[totalSize];
57+
58+
// Fill buffer
59+
int offset = 0;
60+
for (int i = 0; i < ARRAY_INDEXES_CACHE_SIZE; i++) {
61+
String string = Integer.toString(i);
62+
int length = string.length();
63+
for (int j = 0; j < length; j++) {
64+
ARRAY_INDEXES_BUFFER[offset++] = (byte) string.charAt(j);
65+
}
66+
ARRAY_INDEXES_BUFFER[offset++] = 0;
67+
ARRAY_INDEXES_OFFSETS[i] = offset - (length + 1);
68+
ARRAY_INDEXES_LENGTHS[i] = length + 1; // +1 for null terminator
69+
}
70+
}
71+
4272
/**
4373
* Construct an instance.
4474
*
@@ -259,7 +289,7 @@ public void doWriteNull() {
259289
public void doWriteObjectId(final ObjectId value) {
260290
bsonOutput.writeByte(BsonType.OBJECT_ID.getValue());
261291
writeCurrentName();
262-
bsonOutput.writeBytes(value.toByteArray());
292+
bsonOutput.writeObjectId(value);
263293
}
264294

265295
@Override
@@ -397,7 +427,14 @@ public void reset() {
397427

398428
private void writeCurrentName() {
399429
if (getContext().getContextType() == BsonContextType.ARRAY) {
400-
bsonOutput.writeCString(Integer.toString(getContext().index++));
430+
int index = getContext().index++;
431+
if (index >= ARRAY_INDEXES_CACHE_SIZE) {
432+
bsonOutput.writeCString(Integer.toString(index));
433+
} else {
434+
bsonOutput.writeBytes(ARRAY_INDEXES_BUFFER,
435+
ARRAY_INDEXES_OFFSETS[index],
436+
ARRAY_INDEXES_LENGTHS[index]);
437+
}
401438
} else {
402439
bsonOutput.writeCString(getName());
403440
}

bson/src/main/org/bson/ByteBuf.java

+48
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,54 @@ public interface ByteBuf {
106106
*/
107107
ByteBuf put(byte b);
108108

109+
/**
110+
* Writes the given int value into this buffer at the current position,
111+
* using the current byte order, and increments the position by 4.
112+
*
113+
* @param b the int value to be written
114+
* @return this buffer
115+
* @throws java.nio.BufferOverflowException if there are fewer than 4 bytes remaining in this buffer
116+
* @throws java.nio.ReadOnlyBufferException if this buffer is read-only
117+
* @since 5.4
118+
*/
119+
ByteBuf putInt(int b);
120+
121+
/**
122+
* Writes the given int value into this buffer at the current position,
123+
* using the current byte order, and increments the position by 4.
124+
*
125+
* @param b the int value to be written
126+
* @return this buffer
127+
* @throws java.nio.BufferOverflowException if there are fewer than 4 bytes remaining in this buffer
128+
* @throws java.nio.ReadOnlyBufferException if this buffer is read-only
129+
* @since 5.4
130+
*/
131+
ByteBuf putInt(int index, int b);
132+
133+
/**
134+
* Writes the given double value into this buffer at the current position,
135+
* using the current byte order, and increments the position by 8.
136+
*
137+
* @param b the double value to be written
138+
* @return this buffer
139+
* @throws java.nio.BufferOverflowException if there are fewer than 8 bytes remaining in this buffer
140+
* @throws java.nio.ReadOnlyBufferException if this buffer is read-only
141+
* @since 5.4
142+
*/
143+
ByteBuf putDouble(double b);
144+
145+
/**
146+
* Writes the given long value into this buffer at the current position,
147+
* using the current byte order, and increments the position by 8.
148+
*
149+
* @param b the long value to be written
150+
* @return this buffer
151+
* @throws java.nio.BufferOverflowException if there are fewer than 8 bytes remaining in this buffer
152+
* @throws java.nio.ReadOnlyBufferException if this buffer is read-only
153+
* @since 5.4
154+
*/
155+
ByteBuf putLong(long b);
156+
109157
/**
110158
* <p>Flips this buffer. The limit is set to the current position and then the position is set to zero. If the mark is defined then it
111159
* is discarded.</p>

bson/src/main/org/bson/ByteBufNIO.java

+31-2
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,30 @@ public ByteBuf put(final byte b) {
9797
return this;
9898
}
9999

100+
@Override
101+
public ByteBuf putInt(final int b) {
102+
buf.putInt(b);
103+
return this;
104+
}
105+
106+
@Override
107+
public ByteBuf putInt(final int index, final int b) {
108+
buf.putInt(index, b);
109+
return this;
110+
}
111+
112+
@Override
113+
public ByteBuf putDouble(final double b) {
114+
buf.putDouble(b);
115+
return this;
116+
}
117+
118+
@Override
119+
public ByteBuf putLong(final long b) {
120+
buf.putLong(b);
121+
return this;
122+
}
123+
100124
@Override
101125
public ByteBuf flip() {
102126
((Buffer) buf).flip();
@@ -170,8 +194,13 @@ public ByteBuf get(final byte[] bytes, final int offset, final int length) {
170194

171195
@Override
172196
public ByteBuf get(final int index, final byte[] bytes, final int offset, final int length) {
173-
for (int i = 0; i < length; i++) {
174-
bytes[offset + i] = buf.get(index + i);
197+
if (buf.hasArray()) {
198+
System.arraycopy(buf.array(), index, bytes, offset, length);
199+
} else {
200+
// Fallback to per-byte copying if no backing array is available.
201+
for (int i = 0; i < length; i++) {
202+
bytes[offset + i] = buf.get(index + i);
203+
}
175204
}
176205
return this;
177206
}

0 commit comments

Comments
 (0)