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

avoid converting binary document to tree when possible on later mongo driver versions #2565

Merged
merged 2 commits into from
Mar 31, 2021

Conversation

richardstartin
Copy link
Member

@richardstartin richardstartin commented Mar 30, 2021

Newer mongo driver versions allow more efficient reading of BSON documents. Unfortunately this isn't possible on older driver versions, so I added some instrumentation to extract the document's buffer and do the same thing manually.

@richardstartin richardstartin requested a review from a team as a code owner March 30, 2021 17:11
@richardstartin
Copy link
Member Author

richardstartin commented Mar 30, 2021

This makes each BsonDocument 8 bytes larger, so may not be worth it

org.bson.BsonDocument object internals:
 OFFSET  SIZE               TYPE DESCRIPTION                               VALUE
      0    12                    (object header)                           N/A
     12     4      java.util.Map BsonDocument.map                          N/A
     16     4   java.lang.Object BsonDocument.__datadogContext$4           N/A
     20     4                    (loss due to the next object alignment)
Instance size: 24 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

But is probably ok for subclasses

com.mongodb.connection.ByteBufBsonDocument object internals:
 OFFSET  SIZE               TYPE DESCRIPTION                               VALUE
      0    12                    (object header)                           N/A
     12     4      java.util.Map BsonDocument.map                          N/A
     16     4   java.lang.Object BsonDocument.__datadogContext$4           N/A
     20     4   org.bson.ByteBuf ByteBufBsonDocument.byteBuf               N/A
Instance size: 24 bytes

@richardstartin richardstartin force-pushed the rgs/mongo-scrubbing-2 branch 2 times, most recently from 5d102c8 to 6f6bc08 Compare March 30, 2021 18:36
@bantonsson
Copy link
Contributor

So what do you think that the average retained weight of a BsonDocument is? Won't the 8 bytes be dwarfed by the Map and its contents?

@richardstartin
Copy link
Member Author

@bantonsson yes, and the majority of them (in my experience with this driver) tend to be the ones we'll instrument (the ones sent to and from the database) which have alignment shadow to absorb the field. The problem is that for the document type where this adds eight bytes, what you're offsetting it against also contains BsonDocuments... It's possible this will cause small problems with mapping frameworks using a little more memory.

@tylerbenson
Copy link
Contributor

If this api is only available in newer versions, why didn't muzzle require an updated version range?

@richardstartin
Copy link
Member Author

If this api is only available in newer versions, why didn't muzzle require an updated version range?

@tylerbenson the API is available in v4 of the driver (we have two mongo instrumentation modules)

@tylerbenson
Copy link
Contributor

Ok. I was confused what you were referring to since you made changes to both 3.x and 4.x modules.
Now I understand better though. You're taking advantage of the new API in 4.x and adding instrumentation to expose similar behavior to 3.x since it doesn't have the needed API.
👍

@richardstartin
Copy link
Member Author

Yes the changes were made in separate commits

@richardstartin richardstartin merged commit 3394a98 into master Mar 31, 2021
@richardstartin richardstartin deleted the rgs/mongo-scrubbing-2 branch March 31, 2021 21:39
@github-actions github-actions bot added this to the 0.78.0 milestone Mar 31, 2021
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.

None yet

3 participants