Skip to content

Modernize EditorConfig.Core for high performance and low allocations - #37

Merged
Mpdreamz merged 2 commits into
masterfrom
perf/modernize-core
Jun 1, 2026
Merged

Mpdreamz merged 2 commits into
masterfrom
perf/modernize-core

Conversation

@Mpdreamz

@Mpdreamz Mpdreamz commented Jun 1, 2026

Copy link
Copy Markdown
Member

Summary

This PR modernizes the EditorConfig.Core library implementation to be high-performance and allocation-light, while keeping all three target frameworks (netstandard2.0, net462, net10.0) and passing the full upstream conformance suite (200/201 — the one pre-existing failure is unrelated to this PR).

A BenchmarkDotNet project is added first to establish a baseline, so every change is validated against real numbers rather than assumed to help.

Changes

Infrastructure

  • benchmarks/EditorConfig.Benchmarks/ — new [MemoryDiagnoser] benchmark project covering three hot paths: glob matching, INI file parsing, and end-to-end EditorConfigParser.Parse. Baseline results committed to benchmarks/results/.
  • EditorConfig.Core.csproj — add PolySharp (source-only polyfills) and System.Memory (legacy TFMs only) so span APIs and [GeneratedRegex] are available everywhere.

EditorConfigParser.cs — glob matcher cache (biggest win)

IsMatch was calling GlobMatcher.Create(glob, options) on every section for every file, recompiling the entire pattern (brace expansion, tokenization, precomputation) on each call. Added a per-instance ConcurrentDictionary<string, GlobMatcher> cache keyed on the glob string. Safe because _globOptions is a fixed field per parser instance.

Minimatcher.cs — span-based matching engine

  • MatchContext → ref struct with ReadOnlySpan<char> myStr. The internal matching state machine was already offset-based and allocation-light; the ref struct conversion eliminates the remaining heap traffic on the match path. Added IsMatch(ReadOnlySpan<char>) public overload.
  • All (startIndex, count) string searches (IndexOf, LastIndexOf, IndexOfAny) converted to myStr.Slice(start, count).IndexOf(…) span equivalents with correct index rebasing. The offset-based asterisk/dot/globstar logic is preserved verbatim.
  • OneChar.CheckChar — replaced c.ToString() (allocation per character in [...] class patterns) with a direct char.IndexOf for ordinal and a ToUpperInvariant loop for OrdinalIgnoreCase.
  • BraceExpand — replaced member += c string concatenation with StringBuilder, matching the convention in Parse.
  • ourHasBraces regex — replaced with two IndexOf calls (IndexOf('{') + IndexOf('}', open+1)). Behavior-identical to \{.*\}.
  • 4× result.LastOrDefault() in Parse — replaced with direct index access; using System.Linq removed.

EditorConfigFile.cs — source-generated regexes + INI parse cleanup

  • Made partial; SectionRe/CommentRe/PropertyRe declared as [GeneratedRegex]-annotated partial methods on net10.0, with hand-written static readonly Regex fallback bodies on legacy TFMs. Call sites are identical across all targets.
  • KnownProperties changed from string[] + LINQ Contains (O(n) linear scan) to HashSet<string>(OrdinalIgnoreCase) (O(1) lookup).
  • Regex.Matches(line) → Regex.Match(line) for both property and section matching — avoids allocating a MatchCollection when only the first match is ever used.

FileConfiguration.cs — single-pass section merge

The constructor was iterating sections nine times (one SelectMany for raw properties + eight LastOrDefault calls for typed properties). Replaced with a single reverse loop: iterating from last to first section, first-write-wins for each key exactly replicates last-section-wins semantics.

EditorConfigFileCache.cs — stat-based cache key

The original implementation computed a SHA-256 hash of the file content on every GetOrCreate call (even cache hits), then re-read the file a second time on a cache miss. The new implementation uses FileInfo.LastWriteTimeUtc + FileInfo.Length as the cache key — a single cheap metadata stat, no file content read on cache hits. Invalidation semantics match every standard file-watching system (VS Code, webpack, etc.) and are appropriate for .editorconfig files edited by normal tooling.

Benchmark results

Measured on Apple M2 Pro · .NET 10.0.8 · Arm64 RyuJIT AdvSIMD

Glob matching (GlobMatcher)

Method Before After Δ Time Before alloc After alloc Δ Alloc
CreateAndMatch (compile + match, 20 globs × 9 paths) 13,294 ns 11,298 ns −15% 20,592 B 19,896 B −3%
MatchOnly (pre-compiled, 20 globs × 9 paths) 6,511 ns 4,700 ns −28% 912 B 0 B −100%
CreateSingle (compile one pattern) 797 ns 587 ns −26% 1,920 B 1,944 B 0%

The match path is now fully allocation-free (ref struct + ReadOnlySpan<char>).

End-to-end parse (EditorConfigParser.Parse)

Method Before After Δ Time Before alloc After alloc Δ Alloc
ParseFresh (no caches) 71.01 μs 48.65 μs −31% 78.72 KB 43.99 KB −44%
ParseWithFileCache (warm cache) 55.03 μs 11.17 μs −80% 37.84 KB 7.34 KB −81%

The warm-cache path improves by 5× in both time and allocations. The stat-based cache key means a repeated call for an unchanged file is one FileInfo stat + dictionary lookup.

INI file parse (EditorConfigFile.Parse)

Method Before After Δ Time Before alloc After alloc Δ Alloc
ParseFile 21.22 μs 10.39 μs −51% 40.76 KB 34.86 KB −14%

Parse time halved by source-generated regexes, HashSet property lookup, and avoiding MatchCollection allocation.

Verification

  • TUnit: 77/77 passing (dotnet run --project src/EditorConfig.Tests -c Release)
  • CMake/CTest conformance: 200/201 passing — the one failure (spaces_in_middle_key) is pre-existing and unrelated to this PR
  • AOT smoke test: native binary (osx-arm64) builds and runs correctly

🤖 Generated with Claude Code

Mpdreamz and others added 2 commits June 1, 2026 14:37
- Add BenchmarkDotNet project to track regressions
- Add PolySharp + System.Memory to enable modern APIs on all three TFMs
- Source-generated regexes on net10, compiled fallback on legacy TFMs
- Cache compiled GlobMatcher per glob string in EditorConfigParser
- Rewrite MatchContext as a ref struct over ReadOnlySpan<char>
- Eliminate per-match allocations in OneChar.CheckChar and BraceExpand
- Replace 8x LastOrDefault passes in FileConfiguration with single reverse loop
- Switch EditorConfigFileCache to mtime+size key — no file read on cache hits

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
The single-pass reverse loop inserted keys into the raw properties dict
in reverse section order, breaking the conformance test leading_slash_relevance
which expects keys output in the order their sections appear in the file.

Separate the two concerns: forward pass builds the raw dict (last-wins on
duplicates, first-encounter insertion order preserved), reverse pass extracts
the 8 typed properties (last non-null section wins).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@Mpdreamz
Mpdreamz merged commit 664dcc6 into master Jun 1, 2026
10 checks passed
@Mpdreamz
Mpdreamz deleted the perf/modernize-core branch June 1, 2026 13:35
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