Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions EditorConfig.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
<File Path=".github/workflows/verify.yaml" />
<Project Path="build/scripts/scripts.fsproj" />
</Folder>
<Folder Name="/benchmarks/">
<Project Path="benchmarks/EditorConfig.Benchmarks/EditorConfig.Benchmarks.csproj" />
</Folder>
<Folder Name="/src/">
<File Path="src/Directory.Build.props" />
<Project Path="src/EditorConfig.Core/EditorConfig.Core.csproj" />
Expand Down
19 changes: 19 additions & 0 deletions benchmarks/EditorConfig.Benchmarks/EditorConfig.Benchmarks.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<IsPackable>false</IsPackable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Optimize>true</Optimize>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\EditorConfig.Core\EditorConfig.Core.csproj" />
</ItemGroup>

</Project>
106 changes: 106 additions & 0 deletions benchmarks/EditorConfig.Benchmarks/EndToEndBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using System;
using System.IO;
using BenchmarkDotNet.Attributes;
using EditorConfig.Core;

namespace EditorConfig.Benchmarks;

/// <summary>
/// End-to-end benchmark: EditorConfigParser.Parse(path) for a file nested several
/// directories deep. This exercises the IsMatch-per-section hotspot (the biggest allocation
/// source before the matcher cache) and the directory-traversal path.
///
/// A temp directory tree with a realistic .editorconfig is created in [GlobalSetup]
/// and torn down in [GlobalCleanup].
/// </summary>
[MemoryDiagnoser]
public class EndToEndBenchmarks
{
private string _rootDir = null!;
private string _targetFile = null!;
private EditorConfigParser _parser = null!;
private EditorConfigParser _cachedParser = null!;

private const string EditorConfigContent = """
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.{cs,csx}]
indent_size = 4

[*.{fs,fsx}]
indent_size = 4

[*.{js,ts,jsx,tsx}]
indent_size = 2

[*.{json,yml,yaml}]
indent_size = 2

[*.md]
trim_trailing_whitespace = false

[Makefile]
indent_style = tab

[**/*.min.{js,css}]
indent_style = unset

[**/vendor/**]
indent_style = unset
indent_size = unset

[*.{bat,cmd}]
end_of_line = crlf
""";

[GlobalSetup]
public void Setup()
{
// Create a directory tree 4 levels deep so the parser traverses several parents.
_rootDir = Path.Combine(Path.GetTempPath(), $"ec-bench-{Guid.NewGuid():N}");
var deep = Path.Combine(_rootDir, "src", "EditorConfig", "Core");
Directory.CreateDirectory(deep);

// Place a root .editorconfig at the top
File.WriteAllText(Path.Combine(_rootDir, ".editorconfig"), EditorConfigContent);

// The target file being "parsed"
_targetFile = Path.Combine(deep, "Parser.cs");
File.WriteAllText(_targetFile, "// placeholder");

_parser = new EditorConfigParser();
_cachedParser = new EditorConfigParser(EditorConfigFileCache.GetOrCreate);

// Warm up the file cache so disk I/O doesn't dominate
_cachedParser.Parse(_targetFile);
}

[GlobalCleanup]
public void Cleanup()
{
if (Directory.Exists(_rootDir))
Directory.Delete(_rootDir, recursive: true);
}

/// <summary>
/// Parse with a fresh parser (no matcher cache, no file cache).
/// Reflects the current worst-case behaviour.
/// </summary>
[Benchmark(Baseline = true)]
public FileConfiguration ParseFresh() => _parser.Parse(_targetFile);

/// <summary>
/// Parse with the file cache enabled (EditorConfigFileCache).
/// Still re-compiles globs on every call.
/// </summary>
[Benchmark]
public FileConfiguration ParseWithFileCache() => _cachedParser.Parse(_targetFile);
}
115 changes: 115 additions & 0 deletions benchmarks/EditorConfig.Benchmarks/GlobMatchBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
using BenchmarkDotNet.Attributes;
using EditorConfig.Core;

namespace EditorConfig.Benchmarks;

/// <summary>
/// Benchmarks for the glob matching hot path.
/// Separates "create + match" (as the parser currently does on every call)
/// vs "match only" (pre-compiled matcher) to expose the compile vs match split.
/// Globs and paths are drawn from the upstream conformance suite inputs.
/// </summary>
[MemoryDiagnoser]
public class GlobMatchBenchmarks
{
private static readonly GlobMatcherOptions Options = new()
{
MatchBase = true,
Dot = true,
AllowWindowsPaths = true,
};

// Representative sample of glob patterns from tests/glob/*.in
private static readonly string[] Globs =
[
// star.in
"a*e.c",
"Bar/*",
"*",
// star_star.in
"a**z.c",
"b/**z.c",
"d/**/z.c",
"**/*.cs",
// braces.in
"*.{py,js,html}",
"{single}.b",
"a{b,c,}.d",
"{0..9}.h",
// brackets.in
"[ab].a",
"[!ab].b",
"[d-g].c",
"[-ab].f",
// question.in
"?.a",
"?oo.b",
// realistic real-world patterns
"[*.{cs,csx,vb,vbx}]",
"**/*.{cs,fs,vb}",
"src/**/*.cs",
];

// Representative paths to match against
private static readonly string[] Paths =
[
"/home/user/project/src/EditorConfig/Parser.cs",
"/home/user/project/src/EditorConfig/Minimatcher.cs",
"/home/user/project/tests/glob/star.in",
"/home/user/project/abc.js",
"/home/user/project/foo.py",
"/home/user/project/Bar/baz.txt",
"/home/user/project/d/x/y/z/z.c",
"/home/user/project/a.a",
"/home/user/project/1.h",
];

// Pre-compiled matchers for the "match only" benchmark
private GlobMatcher[] _compiled = null!;

[GlobalSetup]
public void Setup()
{
_compiled = new GlobMatcher[Globs.Length];
for (var i = 0; i < Globs.Length; i++)
_compiled[i] = GlobMatcher.Create(Globs[i], Options);
}

/// <summary>
/// Simulates current EditorConfigParser behaviour: compile the glob fresh then match.
/// This is the hottest path before the caching fix (Step 3).
/// </summary>
[Benchmark(Baseline = true)]
public bool CreateAndMatch()
{
var hit = false;
foreach (var glob in Globs)
{
var matcher = GlobMatcher.Create(glob, Options);
foreach (var path in Paths)
hit |= matcher.IsMatch(path);
}
return hit;
}

/// <summary>
/// Match with pre-compiled matchers — the steady-state after the cache lands.
/// </summary>
[Benchmark]
public bool MatchOnly()
{
var hit = false;
foreach (var matcher in _compiled)
{
foreach (var path in Paths)
hit |= matcher.IsMatch(path);
}
return hit;
}

/// <summary>
/// Single glob compile (useful for measuring allocation of Create alone).
/// </summary>
[Benchmark]
public GlobMatcher CreateSingle() => GlobMatcher.Create("**/*.{cs,fs,vb}", Options);
}
82 changes: 82 additions & 0 deletions benchmarks/EditorConfig.Benchmarks/ParseFileBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System.IO;
using BenchmarkDotNet.Attributes;
using EditorConfig.Core;

namespace EditorConfig.Benchmarks;

/// <summary>
/// Benchmarks for parsing a single .editorconfig file from a TextReader.
/// Uses a realistic multi-section config representative of a real project.
/// </summary>
[MemoryDiagnoser]
public class ParseFileBenchmarks
{
/// <summary>
/// A realistic multi-section .editorconfig representing a typical project
/// (covers all property types, multiple globs including braces/stars/brackets).
/// </summary>
private const string SampleEditorConfig = """
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.{cs,csx}]
indent_size = 4

[*.{fs,fsx}]
indent_size = 4

[*.{vb,vbx}]
indent_size = 4

[*.{js,ts,jsx,tsx}]
indent_size = 2

[*.{json,yml,yaml}]
indent_size = 2

[*.md]
trim_trailing_whitespace = false

[*.{py,pyw}]
indent_size = 4
max_line_length = 120

[Makefile]
indent_style = tab

[**/*.min.{js,css}]
indent_style = unset

[**/vendor/**]
indent_style = unset
indent_size = unset

[*.{bat,cmd}]
end_of_line = crlf

[*.sh]
end_of_line = lf
""";

[Benchmark]
public EditorConfigFile ParseFile()
{
using var reader = new StringReader(SampleEditorConfig);
return EditorConfigFile.Parse(reader, "/home/user/project", ".editorconfig");
}

[Benchmark]
public int ParseFileAndCountSections()
{
using var reader = new StringReader(SampleEditorConfig);
var file = EditorConfigFile.Parse(reader, "/home/user/project", ".editorconfig");
return file.Sections.Count;
}
}
3 changes: 3 additions & 0 deletions benchmarks/EditorConfig.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using BenchmarkDotNet.Running;

BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

BenchmarkDotNet v0.14.0, macOS 26.5 (25F71) [Darwin 25.5.0]
Apple M2 Pro, 1 CPU, 12 logical and 12 physical cores
.NET SDK 10.0.300
[Host] : .NET 10.0.8 (10.0.826.23019), Arm64 RyuJIT AdvSIMD
DefaultJob : .NET 10.0.8 (10.0.826.23019), Arm64 RyuJIT AdvSIMD


Method | Mean | Error | StdDev | Median | Ratio | Gen0 | Gen1 | Allocated | Alloc Ratio |
------------------- |---------:|---------:|---------:|---------:|------:|-------:|-------:|----------:|------------:|
ParseFresh | 48.65 μs | 0.385 μs | 0.361 μs | 48.63 μs | 1.00 | 5.3711 | 0.2441 | 43.99 KB | 1.00 |
ParseWithFileCache | 11.17 μs | 0.221 μs | 0.467 μs | 10.98 μs | 0.23 | 0.8850 | - | 7.34 KB | 0.17 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
```

BenchmarkDotNet v0.14.0, macOS 26.5 (25F71) [Darwin 25.5.0]
Apple M2 Pro, 1 CPU, 12 logical and 12 physical cores
.NET SDK 10.0.300
[Host] : .NET 10.0.8 (10.0.826.23019), Arm64 RyuJIT AdvSIMD
DefaultJob : .NET 10.0.8 (10.0.826.23019), Arm64 RyuJIT AdvSIMD


```
| Method | Mean | Error | StdDev | Median | Ratio | Gen0 | Gen1 | Allocated | Alloc Ratio |
|------------------- |---------:|---------:|---------:|---------:|------:|-------:|-------:|----------:|------------:|
| ParseFresh | 48.65 μs | 0.385 μs | 0.361 μs | 48.63 μs | 1.00 | 5.3711 | 0.2441 | 43.99 KB | 1.00 |
| ParseWithFileCache | 11.17 μs | 0.221 μs | 0.467 μs | 10.98 μs | 0.23 | 0.8850 | - | 7.34 KB | 0.17 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Method,Job,AnalyzeLaunchVariance,EvaluateOverhead,MaxAbsoluteError,MaxRelativeError,MinInvokeCount,MinIterationTime,OutlierMode,Affinity,EnvironmentVariables,Jit,LargeAddressAware,Platform,PowerPlanMode,Runtime,AllowVeryLargeObjects,Concurrent,CpuGroups,Force,HeapAffinitizeMask,HeapCount,NoAffinitize,RetainVm,Server,Arguments,BuildConfiguration,Clock,EngineFactory,NuGetReferences,Toolchain,IsMutator,InvocationCount,IterationCount,IterationTime,LaunchCount,MaxIterationCount,MaxWarmupIterationCount,MemoryRandomization,MinIterationCount,MinWarmupIterationCount,RunStrategy,UnrollFactor,WarmupCount,Mean,Error,StdDev,Median,Ratio,Gen0,Gen1,Allocated,Alloc Ratio
ParseFresh,DefaultJob,False,Default,Default,Default,Default,Default,Default,000000000000,Empty,RyuJit,Default,Arm64,8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c,.NET 10.0,False,True,False,True,Default,Default,False,False,False,Default,Default,Default,Default,Default,Default,Default,Default,Default,Default,Default,Default,Default,Default,Default,Default,Default,16,Default,48.65 μs,0.385 μs,0.361 μs,48.63 μs,1.00,5.3711,0.2441,43.99 KB,1.00
ParseWithFileCache,DefaultJob,False,Default,Default,Default,Default,Default,Default,000000000000,Empty,RyuJit,Default,Arm64,8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c,.NET 10.0,False,True,False,True,Default,Default,False,False,False,Default,Default,Default,Default,Default,Default,Default,Default,Default,Default,Default,Default,Default,Default,Default,Default,Default,16,Default,11.17 μs,0.221 μs,0.467 μs,10.98 μs,0.23,0.8850,0.0000,7.34 KB,0.17
Loading
Loading