Repository navigation
Modernize EditorConfig.Core for high performance and low allocations - #37
Merged
Merged
Conversation
- 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>
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.
Summary
This PR modernizes the
EditorConfig.Corelibrary 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-endEditorConfigParser.Parse. Baseline results committed tobenchmarks/results/.EditorConfig.Core.csproj— addPolySharp(source-only polyfills) andSystem.Memory(legacy TFMs only) so span APIs and[GeneratedRegex]are available everywhere.EditorConfigParser.cs— glob matcher cache (biggest win)IsMatchwas callingGlobMatcher.Create(glob, options)on every section for every file, recompiling the entire pattern (brace expansion, tokenization, precomputation) on each call. Added a per-instanceConcurrentDictionary<string, GlobMatcher>cache keyed on the glob string. Safe because_globOptionsis a fixed field per parser instance.Minimatcher.cs— span-based matching engineMatchContext→ref structwithReadOnlySpan<char> myStr. The internal matching state machine was already offset-based and allocation-light; theref structconversion eliminates the remaining heap traffic on the match path. AddedIsMatch(ReadOnlySpan<char>)public overload.(startIndex, count)string searches (IndexOf,LastIndexOf,IndexOfAny) converted tomyStr.Slice(start, count).IndexOf(…)span equivalents with correct index rebasing. The offset-based asterisk/dot/globstar logic is preserved verbatim.OneChar.CheckChar— replacedc.ToString()(allocation per character in[...]class patterns) with a directchar.IndexOffor ordinal and aToUpperInvariantloop forOrdinalIgnoreCase.BraceExpand— replacedmember += cstring concatenation withStringBuilder, matching the convention inParse.ourHasBracesregex — replaced with twoIndexOfcalls (IndexOf('{')+IndexOf('}', open+1)). Behavior-identical to\{.*\}.result.LastOrDefault()inParse— replaced with direct index access;using System.Linqremoved.EditorConfigFile.cs— source-generated regexes + INI parse cleanuppartial;SectionRe/CommentRe/PropertyRedeclared as[GeneratedRegex]-annotated partial methods onnet10.0, with hand-writtenstatic readonly Regexfallback bodies on legacy TFMs. Call sites are identical across all targets.KnownPropertieschanged fromstring[]+ LINQContains(O(n) linear scan) toHashSet<string>(OrdinalIgnoreCase)(O(1) lookup).Regex.Matches(line)→Regex.Match(line)for both property and section matching — avoids allocating aMatchCollectionwhen only the first match is ever used.FileConfiguration.cs— single-pass section mergeThe constructor was iterating
sectionsnine times (oneSelectManyfor raw properties + eightLastOrDefaultcalls 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 keyThe original implementation computed a SHA-256 hash of the file content on every
GetOrCreatecall (even cache hits), then re-read the file a second time on a cache miss. The new implementation usesFileInfo.LastWriteTimeUtc+FileInfo.Lengthas 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.editorconfigfiles edited by normal tooling.Benchmark results
Measured on Apple M2 Pro · .NET 10.0.8 · Arm64 RyuJIT AdvSIMD
Glob matching (
GlobMatcher)CreateAndMatch(compile + match, 20 globs × 9 paths)MatchOnly(pre-compiled, 20 globs × 9 paths)CreateSingle(compile one pattern)The match path is now fully allocation-free (
ref struct+ReadOnlySpan<char>).End-to-end parse (
EditorConfigParser.Parse)ParseFresh(no caches)ParseWithFileCache(warm cache)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
FileInfostat + dictionary lookup.INI file parse (
EditorConfigFile.Parse)ParseFileParse time halved by source-generated regexes,
HashSetproperty lookup, and avoidingMatchCollectionallocation.Verification
dotnet run --project src/EditorConfig.Tests -c Release)spaces_in_middle_key) is pre-existing and unrelated to this PRosx-arm64) builds and runs correctly🤖 Generated with Claude Code