Fix memory retention in sorting buffers - #210
Open
lucashabersaat wants to merge 2 commits into
Open
lucashabersaat wants to merge 2 commits into
lucashabersaat wants to merge 2 commits into
Conversation
lucashabersaat
requested review from
underoot
and removed request for
a team
September 21, 2026 15:42
mourner
requested changes
Sep 21, 2026
mourner
left a comment
Member
There was a problem hiding this comment.
Unfortunately, as is this bring a +8% performance regression on our benchmark. But there seems to be an alternative that doesn't regress performance and also fixes memory retention — try this out.
Comment on lines
+547
to
+548
| sortArr.length = 0; | ||
| sortBuf.length = 0; |
Member
There was a problem hiding this comment.
Suggested change
| sortArr.length = 0; | |
| sortBuf.length = 0; | |
| sortArr.fill(null, 0, n); | |
| sortBuf.fill(null, 0, n); |
Author
|
Makes sense, thanks for the suggestion. Had to cast the type, though. |
This branch has not been deployed
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.
Clear the reusable
sortArrandsortBufarrays after z-order relinking. This prevents stale node references from retaining previously triangulated polygon graphs in case of decreasing input.Verification
Memory-retention reproduction
Used the script below to verify and measure heap retained after Earcut processes progressively smaller polygons.
Check out this branch, save the script as
memory-retention.mjs, then run each commit in a separate Node process:Script
Results
Using Node 26.8.1:
Before this PR: 4.626 MiB retained
After this PR: 0.049 MiB retained
Tell me if there is anything else you need. :)