Peak memory use optimizations. #199
Merged
+40
−16
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.
Reduce peak memory usage of swift-format by discarding collections once they are no longer needed.
Token
arrays frombeforeMap
andafterMap
while processing the associated token.preVisitedExprs
after visitation finishes for the root expression of the tree.beforeMap
andafterMap
By the end of a file, these maps can accumulate a meaningful number of tokens such that they significantly impact the peak memory usage.
This admittedly replaces a O(1) lookup operation with a O(N) lookup-and-remove operation, by replacing
subscript
withremoveValue(forKey:)
. In my testing, I found that this had no impact on the runtime. My hypothesis is that removing from these dictionaries keeps them small enough that O(N) runtime isn't a problem.preVisitedExprs
This set previously contained
ExprSyntax
objects and was maintained for the entire file. Once the visitor has finished with all children of the node that initiated inserting the contextual breaks, it will never visit any of the children again. This set can be emptied at that point to keep it from growing until the visitor finishes with the whole file.Results
The runtime is unchanged, but this reduces peak memory usage especially for large files.