Optimize Key.Strings by reducing memory re-allocations and simplifying control flow#385
Merged
Merged
Conversation
d4147f4 to
909c75e
Compare
909c75e to
3fb878a
Compare
Key.Strings by reducing memory re-allocations and simplifying control flow
Member
|
@gitKashish looks like the code is not compiling. |
Contributor
Author
|
Apologies for the oversight! While cleaning up the code after running the benchmarks, I accidentally removed the new |
Member
Contributor
Author
|
Thank you very much! @unknwon |
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.
Describe the pull request
Key.Stringswas reimplemented to improve performance on large INI arrays. The original implementation converted the entire string to[]runeupfront on every call, usedbytes.Bufferwith no pre-sizing, and assumed a fixed slice capacity of 2 regardless of actual delimiter count. On large arrays this caused excessive heap allocations that made the function a significant bottleneck.This was identified as the root cause of a Forgejo startup performance regression where instances with large lists in config files experienced severely degraded startup times due to repeated calls to
Key.Stringsduring config parsing.Link to the issue: https://codeberg.org/forgejo/forgejo/issues/11453
The new implementation:
utf8.DecodeRuneInStringonly at write time — avoiding the full[]runeallocationstrings.Builderpre-grown tolen(str)to eliminate internal buffer reallocationsstrings.Count(str, delim) + 1so the slice is correctly sized on the first allocationBenchmarks on a large INI array (10 runs, Windows/amd64, i5-11300H):
Raw
go benchoutputChecklist