Conversation
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.
Description
Summary of Changes
OnDemandDocument.getElementno longer caches array elements accessed by index. Index lookups are already O(1) (this.elements[index]), so the cache only saved re-reading the same index, which no caller does:CursorResponse.shift()and thewriteErrorscheck each visit every index once. Name-based lookups keep their cache.Notes for Reviewers
The cache retained ~776 B per document (the cached entry plus the child
OnDemandDocumentstored as its value) until the whole batch was consumed. Under the old default getMorebatchSizeof 1000 that was about 1 MB per batch. Since v7.0.0 (#4729) the server fills getMore batches up to 16 MiB, which with small documents means hundreds of thousands of documents per batch.Manual measurements (Node 24.21.0, local standalone mongod 7.0.37, 2.5M documents,
find({}, { projection: { username: 1 } }), ~300k documents per getMore):batchSize(1000)For bug fixes
Current (incorrect) behavior:
Iterating a cursor over many small documents without an explicit
batchSizeretains roughly 1 KB per document until each batch is fully consumed, adding hundreds of MB of heap per batch.Expected behavior:
Memory used while iterating a batch does not grow with the number of documents already returned.
How to reproduce:
Iterate
find()with a single-field projection over ~1M+ small documents withoutbatchSize, and sampleprocess.memoryUsage().heapUsed.Affected versions:
7.0.0 and later. 6.8.0 through 6.x have the same per-document cost but are capped at 1000 documents per getMore.
Release Highlight
Reduced memory usage when iterating large cursor batches
Since v7.0.0, the server can return hundreds of thousands of small documents in a single
getMorebatch. The driver retained internal bookkeeping for every document already returned from a batch until the batch was fully consumed, which could add hundreds of MB of memory while iterating. The driver no longer retains this data, and applications no longer need to setbatchSizeto keep memory bounded.Double check the following
npm run check:lint)type(NODE-xxxx)[!]: descriptionfeat(NODE-1234)!: rewriting everything in coffeescript