Skip to content

Avoid O(n^2) inserts when formatting enums - #346

Merged
SimonCropp merged 1 commit into
mainfrom
Avoid-O(n-2)-inserts-when-formatting-enums
Feb 7, 2026
Merged

Avoid O(n^2) inserts when formatting enums#346
SimonCropp merged 1 commit into
mainfrom
Avoid-O(n-2)-inserts-when-formatting-enums

Conversation

@SimonCropp

Copy link
Copy Markdown
Member

Replace repeated StringBuilder.Insert(0, ...) calls with a stack-allocated buffer of matched indices and a final forward Append pass. This collects matching enum value indices during the scan (using stackalloc Span) and then builds the result string in forward order with StringBuilder.Append and separators, avoiding the O(n²) cost of inserting at the front repeatedly. Keeps logic readable and is more efficient for typical small enum sizes.

Replace repeated StringBuilder.Insert(0, ...) calls with a stack-allocated buffer of matched indices and a final forward Append pass. This collects matching enum value indices during the scan (using stackalloc Span<int>) and then builds the result string in forward order with StringBuilder.Append and separators, avoiding the O(n²) cost of inserting at the front repeatedly. Keeps logic readable and is more efficient for typical small enum sizes.
@SimonCropp SimonCropp added this to the 0.33.1 milestone Feb 7, 2026
@SimonCropp
SimonCropp merged commit 315f81a into main Feb 7, 2026
4 of 5 checks passed
@SimonCropp
SimonCropp deleted the Avoid-O(n-2)-inserts-when-formatting-enums branch August 14, 2026 22:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant