perf(mongodb): cache the per-connection topology shape#8370
Merged
Conversation
`wrapConnectionCommand` (mongodb >= 4) rebuilt the same five objects from `this.address` on every command. The Connection's address is immutable for its lifetime, so the work lands once on the first command and reuses a per-Connection envelope thereafter. The cache lives in a module-scope `WeakMap` instead of a Symbol-keyed property: the Connection is a foreign object, and an own-key (even a Symbol) shows up to `Reflect.ownKeys` / `Object.getOwnPropertySymbols`, forces a hidden-class transition on first hit, and can collide with another tracer's instrumentation walking the same connection. The extra `WeakMap.get` is single-digit nanoseconds and the foreign object stays untouched. The address parser tightens the previous `length === 2` split to "exactly one `:`, non-empty parts on both sides" so non-standard addresses (random UUIDs, IPv6 forms, empty port / host halves) all collapse to the empty-options topology. `synthesizeTopology` is exported so the parser is unit-tested directly rather than through the mongodb integration suite. Bench (Node 24.15 / V8 13.6, n=500 k x 7 trials, drop best+worst, warm cache, hit-only): per-command rebuild 19.30 ns/op cached envelope (WeakMap) 8.32 ns/op speedup: 2.32x
Contributor
Overall package sizeSelf size: 5.81 MB Dependency sizes| name | version | self size | total size | |------|---------|-----------|------------| | import-in-the-middle | 3.0.1 | 82.56 kB | 817.39 kB | | dc-polyfill | 0.1.11 | 25.74 kB | 25.74 kB |🤖 This report was automatically generated by heaviest-objects-in-the-universe |
🎉 All green!❄️ No new flaky tests detected 🎯 Code Coverage (details) 🔗 Commit SHA: ab6cee6 | Docs | Datadog PR Page | Give us feedback! |
BenchmarksBenchmark execution time: 2026-05-08 19:02:58 Comparing candidate commit ab6cee6 in PR branch Found 0 performance improvements and 0 performance regressions! Performance is the same for 1730 metrics, 114 unstable metrics. |
rochdev
approved these changes
May 8, 2026
rochdev
pushed a commit
that referenced
this pull request
May 13, 2026
`wrapConnectionCommand` (mongodb >= 4) rebuilt the same five objects from `this.address` on every command. The Connection's address is immutable for its lifetime, so the work lands once on the first command and reuses a per-Connection envelope thereafter. The cache lives in a module-scope `WeakMap` instead of a Symbol-keyed property: the Connection is a foreign object, and an own-key (even a Symbol) shows up to `Reflect.ownKeys` / `Object.getOwnPropertySymbols`, forces a hidden-class transition on first hit, and can collide with another tracer's instrumentation walking the same connection. The extra `WeakMap.get` is single-digit nanoseconds and the foreign object stays untouched. The address parser tightens the previous `length === 2` split to "exactly one `:`, non-empty parts on both sides" so non-standard addresses (random UUIDs, IPv6 forms, empty port / host halves) all collapse to the empty-options topology. `synthesizeTopology` is exported so the parser is unit-tested directly rather than through the mongodb integration suite. Bench (Node 24.15 / V8 13.6, n=500 k x 7 trials, drop best+worst, warm cache, hit-only): per-command rebuild 19.30 ns/op cached envelope (WeakMap) 8.32 ns/op speedup: 2.32x
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
wrapConnectionCommand(mongodb >= 4) rebuilt the same five objects fromthis.addresson every command. The Connection's address is immutable for its lifetime, so the work lands once on the first command and reuses a per-Connection envelope thereafter.The cache lives in a module-scope
WeakMapinstead of a Symbol-keyed property: the Connection is a foreign object, and an own-key (even a Symbol) shows up toReflect.ownKeys/Object.getOwnPropertySymbols, forces a hidden-class transition on first hit, and can collide with another tracer's instrumentation walking the same connection. The extraWeakMap.getis single-digit nanoseconds and the foreign object stays untouched.The address parser tightens the previous
length === 2split to "exactly one:, non-empty parts on both sides" so non-standard addresses (random UUIDs, IPv6 forms, empty port / host halves) all collapse to the empty-options topology.synthesizeTopologyis exported so the parser is unit-tested directly rather than through the mongodb integration suite.Bench (Node 24.15 / V8 13.6, n=500 k x 7 trials, drop best+worst, warm cache, hit-only):
per-command rebuild 19.30 ns/op
cached envelope (WeakMap) 8.32 ns/op speedup: 2.32x