perf: remove CodeWriter bad initialisation - #4585
Conversation
thomhurst
commented
Jan 28, 2026
SummaryRemoves unnecessary pre-seeding of the static indent cache in CodeWriter constructor. Critical IssuesNone found ✅ SuggestionsNone - this is a good optimization. Analysis: The removed code was pre-seeding a static readonly ConcurrentDictionary with 10 entries on every CodeWriter instantiation. Since the cache is static and shared across all instances, most of these TryAdd calls would fail after the first instance. The GetIndentation method (line 54-58) already handles lazy initialization via GetOrAdd, making this pre-seeding redundant. This removes allocations from the source generator hot path (10 Enumerable.RepeatIterator + 10 strings per constructor call). Verdict✅ APPROVE - Valid performance optimization with no behavioral changes. |
TimothyMakkison
commented
Jan 28, 2026
Ran the benchmarks and this saves 10MB Before (timing is inaccurate, laptop not plugged in and ran once.
After
|
Uh oh!
There was an error while loading. Please reload this page.
Each time
CiodeWriteris intialised it will try to pre seed_indentCachewith different length strings.indentStringEnumerable.RepeatIteratorandstringmaking this an anti optimisation