Skip to content
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

Optimize BSON codec lookup. #1632

Merged
merged 16 commits into from
Mar 4, 2025
Merged

Optimize BSON codec lookup. #1632

merged 16 commits into from
Mar 4, 2025

Conversation

vbabanin
Copy link
Member

@vbabanin vbabanin commented Feb 18, 2025

This PR introduces performance optimizations for BSON encoding and decoding to improve efficiency for both BsonArrayCodec BsonDocumentCodec in codec lookups. The latest benchmarks:

BsonArrayCodec:

Benchmark Mode Count Before (ops/s) After (ops/s)
BsonArrayCodecBenchmark.decode thrpt 60 29,222.3 43,156.9
BsonArrayCodecBenchmark.encode thrpt 60 12,611.6 14,886.9

BsonDocumentCodec:
The BsonDocumentBenchmark parses simple BsonInt32 fields to minimize the impact of other codecs to evaluate BsonDocumentCodec's internal performance.

Benchmark Mode Count Before (ops/s) After (ops/s)
BsonDocumentBenchmark.decode thrpt 60 30,511.05 41,312.66
BsonDocumentBenchmark.encode thrpt 60 33,476.20 39,243.81

Changes:

  • Use BsonTypeCodecMap for encoding and decoding in BsonArrayCodec and BsonDocumentCodec to enable faster access.
  • Optimize BsonTypeClassMap by replacing Map with a plain array to eliminate redundant hash computations.

JAVA-5339

- Use `BsonTypeCodecMap` for decoding in `BsonArray` to enable faster access.
- Optimize `BsonTypeClassMap` by replacing `Map` with a plain array to eliminate redundant hash computations.

JAVA-5339
@vbabanin vbabanin self-assigned this Feb 18, 2025
@vbabanin vbabanin changed the title Optimize BSON Array decoding. Optimize BSON codec lookup. Feb 19, 2025
@vbabanin vbabanin requested a review from rozza February 20, 2025 00:24
@vbabanin vbabanin marked this pull request as ready for review February 20, 2025 07:46
Copy link
Member

@rozza rozza left a comment

Choose a reason for hiding this comment

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

Looks good - a couple of questions mainly for my edification

annotationProcessor 'org.openjdk.jmh:jmh-generator-annprocess:1.37'
}

tasks.register("jmh", JavaExec) {
Copy link
Member

Choose a reason for hiding this comment

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

Does this get run via evergreen in the same way the other benchmarks do?

Copy link
Member Author

@vbabanin vbabanin Feb 27, 2025

Choose a reason for hiding this comment

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

Currently, no. I initially considered adding it to Evergreen, but it would expand this PR and would require some time to test it. This task is mainly for local benchmarking to quickly assess the relative performance impact of small components. However, I think we should consider adding it to Evergreen in the future, as it could provide more insight into performance changes.

Copy link
Member

Choose a reason for hiding this comment

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

In that case in the future it may be worth splitting jmh benchmarks into their own project.

For example reorganize both into a benchmarks folder but have separate configuration for each:

 benchmarks
   |
   | - driver (the current driver-benchmarks)
   | - jmh (jmh test suite)

Nothing to do now.

@jyemin jyemin requested review from jyemin and removed request for jyemin February 26, 2025 17:09
Using ordinal() ties the mapping to enum declaration order, which risks subtle bugs in the future.

JAVA-5339
@vbabanin vbabanin requested a review from rozza February 27, 2025 07:32
Copy link
Member

@rozza rozza left a comment

Choose a reason for hiding this comment

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

I have added some suggestions - that reduce the import footprint but realisitcally will have little to no impact on the jmh tests - so are optional nit fixes.

LGTM

vbabanin and others added 8 commits March 4, 2025 00:00
…nArrayCodecBenchmark.java

Co-authored-by: Ross Lawley <ross.lawley@gmail.com>
…nDocumentBenchmark.java

Co-authored-by: Ross Lawley <ross.lawley@gmail.com>
…nDocumentBenchmark.java

Co-authored-by: Ross Lawley <ross.lawley@gmail.com>
…nArrayCodecBenchmark.java

Co-authored-by: Ross Lawley <ross.lawley@gmail.com>
…nUtils.java

Co-authored-by: Ross Lawley <ross.lawley@gmail.com>
…nUtils.java

Co-authored-by: Ross Lawley <ross.lawley@gmail.com>
@vbabanin vbabanin merged commit 5ed3c31 into mongodb:main Mar 4, 2025
60 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants