Skip to content

perf: improve write performance and add benchmarks - #5

Merged
dsherret merged 1 commit into
mainfrom
perf
Oct 15, 2026
Merged

dsherret merged 1 commit into
mainfrom
perf

Conversation

@dsherret

Copy link
Copy Markdown
Owner

Adds a BenchmarkDotNet project (CodeBlockWriterSharp.Benchmarks) and makes writing 1.7x to 58x faster with about half the allocations. The output is unchanged.

Results

BenchmarkDotNet default job, .NET 10:

Benchmark Before After Speedup Allocated before Allocated after
GenerateClient 521.0 us 248.6 us 2.1x 1,144.6 KB 600.2 KB
WriteLines 358.6 us 210.6 us 1.7x 655.1 KB 429.4 KB
WriteMultiLineText 397.0 us 199.4 us 2.0x 870.7 KB 489.2 KB
WriteSmallPieces 808.4 us 242.8 us 3.3x 1,701.2 KB 263.7 KB
QuoteLongText 29,522.3 us 507.5 us 58x 1,592,645.3 KB 1,741.1 KB

Run them with:

dotnet run -c Release --project CodeBlockWriterSharp.Benchmarks -- --filter '*'

Changes

  • Newlines: split text with IndexOf('\n') instead of Regex.Split, and append ranges of the original string instead of substrings.
  • State tracking: skip directly to the next char that can change the state instead of checking every char. The library now also targets net8.0, where this uses a vectorized SearchValues<char> search. The netstandard2.0 build uses a simple loop.
  • Quote(text): escaping was O(n²) because it built the result with string +=. It's now a single pass that returns the original string when there's nothing to escape. The quotes are written separately instead of concatenated. Removed the now unused StringUtils.EscapeChar.
  • Last char: tracked in a field instead of indexing into the StringBuilder for every write and newline.
  • Small things: Count > 0 instead of LINQ Any() in IsInString(), one Append(char, count) for indentation, and cached strings for the quote char, Space() and Tab().

Verification

  • All tests pass, including new EscapeForWithinString cases for \r\n and empty input.
  • A fuzz harness that wasn't committed ran 3,000 seeded random sequences of writer API calls. They mixed quotes, regexes, division, comments, template literals, \r\n, blocks and indentation. It also wrote a large real-world generated TypeScript file. It recorded the output plus IsInString/IsInComment/IsLastNewLine/etc. after every call. The log hash was identical to main for both the netstandard2.0 and net8.0 builds, and it differed for the build before the regex fix, so the harness does catch real differences.

- Split on newlines without a regex and without allocating substrings.
- Skip directly to chars that may change the internal state (vectorized via
  SearchValues on net8.0+).
- Escape quoted text in a single pass instead of O(n^2) string concatenation.
- Track the last written char instead of indexing into the StringBuilder.
- Avoid LINQ and small allocations on hot paths.
- Add a BenchmarkDotNet project.
@dsherret dsherret changed the title perf: Improve write performance and add benchmarks perf: improve write performance and add benchmarks Oct 15, 2026
@dsherret
dsherret merged commit bc57c97 into main Oct 15, 2026
1 check passed
@dsherret
dsherret deleted the perf branch October 15, 2026 00:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant