Skip to content

Commit 0ba1687

Browse files
authored
Fix RawBsonDocument encoding performance regression (#1888)
Add instanceof check in BsonDocumentCodec to route RawBsonDocument to RawBsonDocumentCodec, restoring efficient byte-copy encoding. Previous BsonType-based lookup led to sub-optimal performance as it could not distinguish RawBsonDocument from BsonDocument. JAVA-6101
1 parent f6cbb32 commit 0ba1687

5 files changed

Lines changed: 111 additions & 1 deletion

File tree

bson/src/main/org/bson/codecs/BsonDocumentCodec.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.bson.BsonType;
2323
import org.bson.BsonValue;
2424
import org.bson.BsonWriter;
25+
import org.bson.RawBsonDocument;
2526
import org.bson.codecs.configuration.CodecRegistry;
2627
import org.bson.types.ObjectId;
2728

@@ -40,6 +41,7 @@ public class BsonDocumentCodec implements CollectibleCodec<BsonDocument> {
4041
private static final String ID_FIELD_NAME = "_id";
4142
private static final CodecRegistry DEFAULT_REGISTRY = fromProviders(new BsonValueCodecProvider());
4243
private static final BsonTypeCodecMap DEFAULT_BSON_TYPE_CODEC_MAP = new BsonTypeCodecMap(getBsonTypeClassMap(), DEFAULT_REGISTRY);
44+
private static final RawBsonDocumentCodec RAW_BSON_DOCUMENT_CODEC = new RawBsonDocumentCodec();
4345

4446
private final CodecRegistry codecRegistry;
4547
private final BsonTypeCodecMap bsonTypeCodecMap;
@@ -101,6 +103,10 @@ protected BsonValue readValue(final BsonReader reader, final DecoderContext deco
101103

102104
@Override
103105
public void encode(final BsonWriter writer, final BsonDocument value, final EncoderContext encoderContext) {
106+
if (value instanceof RawBsonDocument) {
107+
RAW_BSON_DOCUMENT_CODEC.encode(writer, (RawBsonDocument) value, encoderContext);
108+
return;
109+
}
104110
writer.writeStartDocument();
105111

106112
beforeFields(writer, encoderContext, value);

driver-benchmarks/src/main/com/mongodb/benchmark/benchmarks/AbstractBsonDocumentBenchmark.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public int getBytesPerRun() {
6161
return fileLength * NUM_INTERNAL_ITERATIONS;
6262
}
6363

64-
private byte[] getDocumentAsBuffer(final T document) throws IOException {
64+
protected byte[] getDocumentAsBuffer(final T document) throws IOException {
6565
BasicOutputBuffer buffer = new BasicOutputBuffer();
6666
codec.encode(new BsonBinaryWriter(buffer), document, EncoderContext.builder().build());
6767

driver-benchmarks/src/main/com/mongodb/benchmark/benchmarks/BenchmarkSuite.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ private static void runBenchmarks()
7171
runBenchmark(new BsonDecodingBenchmark<>("Deep", "extended_bson/deep_bson.json", DOCUMENT_CODEC));
7272
runBenchmark(new BsonDecodingBenchmark<>("Full", "extended_bson/full_bson.json", DOCUMENT_CODEC));
7373

74+
runBenchmark(new RawBsonNestedEncodingBenchmark("Full RawBsonDocument in BsonDocument BSON Encoding", "extended_bson/full_bson.json"));
75+
runBenchmark(new RawBsonArrayEncodingBenchmark("Full RawBsonDocument Array in BsonDocument BSON Encoding", "extended_bson/full_bson.json", 10));
76+
7477
runBenchmark(new RunCommandBenchmark<>(DOCUMENT_CODEC));
7578
runBenchmark(new FindOneBenchmark<Document>("single_and_multi_document/tweet.json", BenchmarkSuite.DOCUMENT_CLASS));
7679

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2016-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package com.mongodb.benchmark.benchmarks;
19+
20+
import org.bson.BsonArray;import org.bson.BsonDocument;
21+
import org.bson.RawBsonDocument;
22+
import org.bson.codecs.BsonDocumentCodec;
23+
24+
import java.io.IOException;
25+
26+
public class RawBsonArrayEncodingBenchmark extends BsonEncodingBenchmark<BsonDocument> {
27+
28+
private final int arraySize;
29+
30+
public RawBsonArrayEncodingBenchmark(final String name, final String resourcePath, final int arraySize) {
31+
super(name, resourcePath, new BsonDocumentCodec());
32+
this.arraySize = arraySize;
33+
}
34+
35+
@Override
36+
public void setUp() throws IOException {
37+
super.setUp();
38+
RawBsonDocument rawDoc = new RawBsonDocument(document, codec);
39+
40+
BsonArray array = new BsonArray();
41+
for (int i = 0; i < arraySize; i++) {
42+
array.add(rawDoc);
43+
}
44+
document = new BsonDocument("results", array);
45+
46+
// Recalculate documentBytes for accurate throughput reporting
47+
documentBytes = getDocumentAsBuffer(document);
48+
49+
}
50+
51+
@Override
52+
public int getBytesPerRun() {
53+
return documentBytes.length * NUM_INTERNAL_ITERATIONS;
54+
}
55+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2016-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package com.mongodb.benchmark.benchmarks;
19+
20+
import org.bson.BsonDocument;
21+
import org.bson.RawBsonDocument;
22+
import org.bson.codecs.BsonDocumentCodec;
23+
24+
import java.io.IOException;
25+
26+
public class RawBsonNestedEncodingBenchmark extends BsonEncodingBenchmark<BsonDocument> {
27+
28+
public RawBsonNestedEncodingBenchmark(final String name, final String resourcePath) {
29+
super(name, resourcePath, new BsonDocumentCodec());
30+
}
31+
32+
@Override
33+
public void setUp() throws IOException {
34+
super.setUp();
35+
36+
RawBsonDocument rawDoc = new RawBsonDocument(document, codec);
37+
document = new BsonDocument("nested", rawDoc);
38+
39+
documentBytes = getDocumentAsBuffer(document);
40+
}
41+
42+
@Override
43+
public int getBytesPerRun() {
44+
return documentBytes.length * NUM_INTERNAL_ITERATIONS;
45+
}
46+
}

0 commit comments

Comments
 (0)