Repository navigation
[JSC] Prune a retained heap snapshot only after a full collection #656
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
Open
robobun
wants to merge
1
commit into
main
Choose a base branch
from
robobun/de5ee943/prune-heap-snapshot-nodes-on-full-gc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
JSTests/heapProfiler/eden-collections-between-snapshots.js
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| var SimpleObject = $vm.SimpleObject; | ||
|
|
||
| // A retained snapshot is only pruned at the end of a full collection. These checks pin what | ||
| // that relies on: a cell that a snapshot holds survives every eden collection, so its node | ||
| // keeps its identifier, and the node of a cell that dies leaves the chain in the full | ||
| // collection that frees it, before anything can reuse the address. | ||
|
|
||
| function assert(condition, reason) { | ||
| if (!condition) | ||
| throw new Error(reason); | ||
| } | ||
|
|
||
| // "nodes" is a flat list of <nodeId>, <sizeInBytes>, <nodeClassNameIndex>, <flags>. | ||
| const nodeFieldCount = 4; | ||
|
|
||
| function simpleObjectIds() { | ||
| let {nodes, nodeClassNames} = generateHeapSnapshot(); | ||
| let classNameIndex = nodeClassNames.indexOf("SimpleObject"); | ||
| let ids = []; | ||
| for (let i = 0; i < nodes.length; i += nodeFieldCount) { | ||
| if (nodes[i + 2] === classNameIndex) | ||
| ids.push(nodes[i]); | ||
| } | ||
| return ids.sort((a, b) => a - b); | ||
| } | ||
|
|
||
| function edenCollections() { | ||
| for (let round = 0; round < 20; ++round) { | ||
| let garbage = []; | ||
| for (let i = 0; i < 1000; ++i) | ||
| garbage.push({ i }); | ||
| edenGC(); | ||
| } | ||
| } | ||
|
|
||
| // Every object lives in a property of `held`, so clearing the property is what frees it. | ||
| let held = {}; | ||
|
|
||
| // Identifiers follow marking order, not allocation order, so each object gets a snapshot of its own. | ||
| (function() { held.survivor = new SimpleObject; })(); | ||
| let survivorId; | ||
| (function() { | ||
| let ids = simpleObjectIds(); | ||
| assert(ids.length === 1, "the first snapshot should hold 1 'SimpleObject' instance, has " + ids.length); | ||
| survivorId = ids[0]; | ||
| })(); | ||
|
|
||
| (function() { held.doomed = new SimpleObject; })(); | ||
| let doomedId; | ||
| (function() { | ||
| let ids = simpleObjectIds(); | ||
| assert(ids.length === 2, "the second snapshot should hold 2 'SimpleObject' instances, has " + ids.length); | ||
| assert(ids[0] === survivorId, "'survivor' should keep its identifier"); | ||
| doomedId = ids[1]; | ||
| })(); | ||
|
|
||
| edenCollections(); | ||
|
|
||
| (function() { held.lateArrival = new SimpleObject; })(); | ||
| edenGC(); | ||
|
|
||
| let lateArrivalId; | ||
| (function() { | ||
| let ids = simpleObjectIds(); | ||
| assert(ids.length === 3, "the third snapshot should hold 3 'SimpleObject' instances, has " + ids.length); | ||
| assert(ids[0] === survivorId, "'survivor' should keep its identifier across eden collections"); | ||
| assert(ids[1] === doomedId, "'doomed' should keep its identifier across eden collections"); | ||
| lateArrivalId = ids[2]; | ||
| })(); | ||
|
|
||
| held.doomed = null; | ||
| edenCollections(); | ||
| fullGC(); | ||
|
|
||
| // 'doomed' is gone, so fresh objects are free to land on the address it had. | ||
| (function() { | ||
| held.replacements = []; | ||
| for (let i = 0; i < 1000; ++i) | ||
| held.replacements.push(new SimpleObject); | ||
| })(); | ||
| edenGC(); | ||
|
|
||
| (function() { | ||
| let ids = simpleObjectIds(); | ||
| assert(ids.length === 1002, "the last snapshot should hold 1002 'SimpleObject' instances, has " + ids.length); | ||
| assert(ids[0] === survivorId, "'survivor' should still have its identifier"); | ||
| assert(ids[1] === lateArrivalId, "'lateArrival' should still have its identifier"); | ||
| assert(!ids.includes(doomedId), "no object should inherit the identifier of 'doomed'"); | ||
| assert(ids[2] > lateArrivalId, "every replacement should get a new, larger identifier"); | ||
| })(); | ||
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win
Allocate replacements before the full collection.
The current
fullGC()prunesdoomedbefore any replacement can reuse its address. Therefore,!ids.includes(doomedId)does not test the deferred-pruning interval.Move
fullGC()after the replacement allocation andedenGC(). This order detects identifier reuse if an Eden collection frees and reuses the snapshot cell.Proposed test order
🤖 Prompt for AI Agents