Skip to content

Conversation

@FGasper
Copy link
Collaborator

@FGasper FGasper commented Oct 24, 2025

This PR contains several commits that optimize various aspects of BSON handling & garbage-collection.

The commits will be preserved in the merge. Review should consider each individually.

This code gets called repeatedly, so it’s worth optimizing.
This also optimizes the field-names uniqueness check.
@FGasper FGasper force-pushed the felipe_optimize_bson branch from 05f1ea5 to 2868f19 Compare October 24, 2025 03:25
Unmarshaling is slow due to reflection. This function gets called
frequently enough to justify the optimization.
@FGasper FGasper force-pushed the felipe_optimize_bson branch 3 times, most recently from 66c7611 to 31a3273 Compare October 24, 2025 16:40
@FGasper FGasper requested a review from tdq45gj October 24, 2025 16:41
@FGasper FGasper force-pushed the felipe_optimize_bson branch from 31a3273 to 899476f Compare October 24, 2025 18:25
Copy link
Collaborator

@tdq45gj tdq45gj left a comment

Choose a reason for hiding this comment

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

LGTM. I have two questions for my curiosity, but none of them is blocking.

strLen := len(typedIn)
buf := make([]byte, 5+strLen)

binary.LittleEndian.PutUint32(buf, 1+uint32(strLen))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Am I understanding it correctly that it is safe because all BSON values are little-endian except timestamp and counter values?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

All BSON numbers are little-endian, yeah. http://bsonspec.org

// if the target type doesn’t match the value.
//
// Augment bsonType if you find a type here that’s missing.
func CastRawValue[T bsonCastRecipient](in bson.RawValue) (T, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This function and ToRawValue seems to be re-implementing driver's logic. I wonder if the driver has the same functions or do we have to implement these ourselves.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good call … I found AppendInt32/Int64/String and have replaced them in here.

This makes the document IDs easier to reason about and sets up
further optimizations.

It also adds a few “shortcut” utilities to marshal/unmarshal BSON
without reflection.
This fixes a typo that caused more recheck batches than was intended.
It’s merely a small performance fix; it does not impact correctness.
This minimizes GC overhead, especially from BSON marshaling.
@FGasper FGasper force-pushed the felipe_optimize_bson branch from 899476f to 5afa227 Compare October 28, 2025 00:04
@FGasper FGasper merged commit 68f3e3e into mongodb-labs:main Oct 28, 2025
99 checks passed
@FGasper FGasper deleted the felipe_optimize_bson branch October 28, 2025 00:20
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