Skip to content

perf(MongoDB): reduce allocations and improve robustness in lock hot paths - #276

Merged
madelson merged 7 commits into
madelson:reduce-mongo-allocationsfrom
joesdu:master
Jun 10, 2026
Merged

perf(MongoDB): reduce allocations and improve robustness in lock hot paths#276
madelson merged 7 commits into
madelson:reduce-mongo-allocationsfrom
joesdu:master

Conversation

@joesdu

@joesdujoesdu commented Mar 27, 2026

Copy link
Copy Markdown
Contributor
  • Cache IMongoCollection reference in constructor instead of calling GetCollection on every TryAcquireAsync invocation
  • Promote immutable BsonDocument sub-expressions (expiredOrMissing, newFencingToken) to static readonly fields; cache newExpiresAt as an instance field (depends on expiry) to avoid rebuilding ~10 BsonDocument objects per busy-wait iteration
  • Move TTL index initialization to the start of TryAcquireAsync so it is triggered on every acquisition attempt, not only on success
  • Cache ownerFilter and renewUpdate in InnerHandle constructor; lease renewal (~every 10 s) no longer allocates new filter/update objects
  • Wrap ReleaseLockAsync in try-catch so network failures during Dispose do not surface unexpected exceptions to callers (TTL index provides eventual cleanup)
  • Add internal constructor accepting pre-parsed MongoDistributedLockOptions so MongoDistributedSynchronizationProvider can parse options once and reuse the result across all CreateLock calls
  • Fix stale comment "foo" → "expiresAt" in CheckIfIndexExists
  • Upgraded Microsoft.SourceLink.GitHub, Microsoft.Build.Tasks.Git, and Microsoft.SourceLink.Common to version 10.0.201 in DistributedLock.ZooKeeper, DistributedLock, and DistributedLockTaker.
  • Updated System.IO.Hashing to version 10.0.5 and added its dependencies.
  • Updated MongoDB.Bson and MongoDB.Driver versions to 3.7.1.
  • Updated System.Diagnostics.DiagnosticSource to version 10.0.5.
  • Adjusted DistributedLock.Oracle and DistributedLock.SqlServer versions to 1.0.5 and 1.0.7 respectively.
  • Made various transitive dependency updates to ensure compatibility and stability.

…paths
- Cache IMongoCollection<T> reference in constructor instead of calling GetCollection on every TryAcquireAsync invocation
- Promote immutable BsonDocument sub-expressions (expiredOrMissing, newFencingToken) to static readonly fields; cache newExpiresAt as an instance field (depends on expiry) to avoid rebuilding ~10 BsonDocument objects per busy-wait iteration
- Move TTL index initialization to the start of TryAcquireAsync so it is triggered on every acquisition attempt, not only on success
- Cache ownerFilter and renewUpdate in InnerHandle constructor; lease renewal (~every 10 s) no longer allocates new filter/update objects
- Wrap ReleaseLockAsync in try-catch so network failures during Dispose do not surface unexpected exceptions to callers (TTL index provides eventual cleanup)
- Add internal constructor accepting pre-parsed MongoDistributedLockOptions so MongoDistributedSynchronizationProvider can parse options once and reuse the result across all CreateLock calls
- Fix stale comment "foo" → "expiresAt" in CheckIfIndexExists
- Upgraded Microsoft.SourceLink.GitHub, Microsoft.Build.Tasks.Git, and Microsoft.SourceLink.Common to version 10.0.201 in DistributedLock.ZooKeeper, DistributedLock, and DistributedLockTaker.
- Updated System.IO.Hashing to version 10.0.5 and added its dependencies.
- Increased MongoDB.Bson and MongoDB.Driver versions to 3.7.1.
- Updated System.Diagnostics.DiagnosticSource to version 10.0.5.
- Adjusted DistributedLock.Oracle and DistributedLock.SqlServer versions to 1.0.5 and 1.0.7 respectively.
- Made various transitive dependency updates to ensure compatibility and stability.
CopilotAI review requested due to automatic review settings March 27, 2026 08:10

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR optimizes the MongoDB-based distributed lock implementation to reduce per-acquire/renew allocations and improve operational robustness (TTL index creation timing, safer dispose behavior), while also updating several NuGet dependencies across the solution.

Changes:

  • MongoDB lock hot-path optimizations: cache IMongoCollection, reuse BSON sub-expressions, and cache renewal filter/update definitions.
  • Robustness adjustments: trigger TTL index initialization on every acquisition attempt; make release-on-dispose resilient to transient failures.
  • Dependency refresh: bump MongoDB driver/BSON, SourceLink/build tooling, DiagnosticSource, System.IO.Hashing, and related transitive pins across projects.

Reviewed changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated 5 comments.

Show a summary per file
FileDescription
src/DistributedLock.MongoDB/MongoDistributedLock.csCaches collection/expressions and reduces allocations in acquire/renew; adjusts TTL init timing; swallows release failures on dispose.
src/DistributedLock.MongoDB/MongoIndexInitializer.csMakes index-creation helper static and fixes TTL index existence check logic.
src/DistributedLock.MongoDB/MongoDistributedSynchronizationProvider.csParses Mongo lock options once in provider and reuses them when creating locks.
src/Directory.Packages.propsCentral version bumps (e.g., MongoDB.Driver, SourceLink, DiagnosticSource).
src/DistributedLock.MongoDB/packages.lock.jsonUpdates locked dependency graph for MongoDB package changes.
src/DistributedLock/packages.lock.jsonUpdates locked dependency graph for core package changes (SourceLink/System.IO.Hashing/etc.).
src/DistributedLock.Azure/packages.lock.jsonUpdates locked dependency graph for Azure package changes and transitive updates.
src/DistributedLock.Core/packages.lock.jsonUpdates locked dependency graph (notably SourceLink/System.IO.Hashing changes).
src/DistributedLock.FileSystem/packages.lock.jsonUpdates locked dependency graph for SourceLink/System.IO.Hashing and transitive updates.
src/DistributedLock.MySql/packages.lock.jsonUpdates locked dependency graph for SourceLink/System.IO.Hashing and transitive updates.
src/DistributedLock.Oracle/packages.lock.jsonUpdates locked dependency graph for SourceLink/System.IO.Hashing and transitive updates.
src/DistributedLock.Postgres/packages.lock.jsonUpdates locked dependency graph for SourceLink/System.IO.Hashing and transitive updates.
src/DistributedLock.Redis/packages.lock.jsonUpdates locked dependency graph for SourceLink/System.IO.Hashing and transitive updates.
src/DistributedLock.SqlServer/packages.lock.jsonUpdates locked dependency graph for SourceLink/System.IO.Hashing and transitive updates.
src/DistributedLock.Tests/packages.lock.jsonUpdates locked dependency graph for tests (now only contains net8.0 graph).
src/DistributedLock.WaitHandles/packages.lock.jsonUpdates locked dependency graph for SourceLink/System.IO.Hashing and transitive updates.
src/DistributedLock.ZooKeeper/packages.lock.jsonUpdates locked dependency graph for SourceLink/System.IO.Hashing and transitive updates.
src/DistributedLockTaker/packages.lock.jsonUpdates locked dependency graph (MongoDB driver and other dependency pins).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment threadsrc/DistributedLock.MongoDB/MongoDistributedLock.cs Outdated
Comment threadsrc/DistributedLock.MongoDB/MongoDistributedLock.cs Outdated
Comment threadsrc/DistributedLock.MongoDB/MongoDistributedLock.cs Outdated
Comment threadsrc/DistributedLock.Tests/packages.lock.json
Comment threadsrc/DistributedLock.Core/packages.lock.json

@madelsonmadelson left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks for submitting @joesdu ! I've taken a look and left some comments. Please let me know when you're done responding/addressing.

Comment threadsrc/DistributedLock.MongoDB/MongoDistributedLock.cs Outdated
Comment threadsrc/DistributedLock.MongoDB/MongoDistributedLock.cs Outdated
Comment threadsrc/DistributedLock.MongoDB/MongoDistributedLock.cs Outdated
Comment threadsrc/DistributedLock.MongoDB/MongoDistributedLock.cs
Comment threadsrc/DistributedLock.MongoDB/MongoDistributedLock.cs Outdated
Comment threadsrc/DistributedLock.MongoDB/MongoDistributedLock.cs Outdated
Comment threadsrc/DistributedLock.MongoDB/MongoDistributedSynchronizationProvider.cs Outdated
Comment threadsrc/DistributedLock.MongoDB/MongoIndexInitializer.cs Outdated
Comment threadsrc/DistributedLock.MongoDB/MongoIndexInitializer.cs Outdated
Comment threadsrc/Directory.Packages.props
@joesdu
joesdu requested a review from madelsonMay 31, 2026 08:02
@madelson
madelson changed the base branch from master to reduce-mongo-allocationsJune 10, 2026 00:26
@madelson
madelson merged commit a83418a into madelson:reduce-mongo-allocationsJun 10, 2026
1 check was pending
@madelsonmadelson added this to the 2.8.2 milestone Jun 10, 2026
Sign up for freeto 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.

3 participants

@joesdu@madelson