Repository navigation
Add focused evaluation performance benchmarks - #14660
Conversation
There was a problem hiding this comment.
Pull request overview
This PR restructures MSBuild’s BenchmarkDotNet coverage to provide smaller, more targeted evaluation/expansion benchmarks and a repeatable cross-TFM runner, making performance investigations faster and easier to validate.
Changes:
- Adds a cross-target-framework benchmark runner script (with named benchmark sets, isolated artifacts per TFM, optional launch-count, and optional power-plan enforcement).
- Replaces the monolithic expander benchmark with focused suites (properties/items/metadata/mixed), scaling benchmarks, condition parsing/evaluation benchmarks, and expression-shredder throughput/allocation benchmarks.
- Introduces small reusable benchmark infrastructure (temporary directories, expander/project fixtures) and updates existing modifier/escaping cache benchmarks to make cache state deterministic per iteration.
Reviewed changes
Copilot reviewed 27 out of 27 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/MSBuild.Benchmarks/Run-Benchmarks.ps1 | New cross-TFM runner with set/filter selection and per-TFM artifacts. |
| src/MSBuild.Benchmarks/readme.md | Documents the new runner, sets, and run modes. |
| src/MSBuild.Benchmarks/Program.cs | Adds --enforce-power-plan handling and config mutator wiring. |
| src/MSBuild.Benchmarks/ItemSpecModifiersCachingBenchmark.cs | Reworks setup to make cold/hot cache measurements deterministic per iteration. |
| src/MSBuild.Benchmarks/Infrastructure/TemporaryDirectory.cs | New helper to manage benchmark-scoped temp directories. |
| src/MSBuild.Benchmarks/Infrastructure/ExpanderBuilder.cs | New builder for constructing expanders from explicit properties/items/metadata. |
| src/MSBuild.Benchmarks/Infrastructure/ExpanderBenchmarkFixture.cs | New fixture owning expander + backing project lifetime. |
| src/MSBuild.Benchmarks/Infrastructure/BenchmarkProject.cs | New helper owning ProjectCollection/ProjectRootElement for benchmark instances/items. |
| src/MSBuild.Benchmarks/ExpanderBenchmark.cs | Removes the prior combined expander benchmark. |
| src/MSBuild.Benchmarks/Evaluation/Items/LazyItemEvaluationBenchmark.cs | New end-to-end evaluation benchmark focused on lazy item operations. |
| src/MSBuild.Benchmarks/Evaluation/ExpressionShredder/ExpressionShredderBenchmark.cs | New throughput benchmark for ExpressionShredder entry points with warm-cache rooting. |
| src/MSBuild.Benchmarks/Evaluation/ExpressionShredder/ExpressionShredderAllocationBenchmark.cs | New cold-cache allocation diagnostic benchmark (opt-in category). |
| src/MSBuild.Benchmarks/Evaluation/Expansion/PropertyFunctionExpansionBenchmark.cs | New focused property-function parsing/invocation expansion benchmarks. |
| src/MSBuild.Benchmarks/Evaluation/Expansion/PropertyExpansionScalingBenchmark.cs | New property-reference scaling benchmarks with representative expression shapes. |
| src/MSBuild.Benchmarks/Evaluation/Expansion/PropertyExpansionBenchmark.cs | New focused property expansion shape benchmarks. |
| src/MSBuild.Benchmarks/Evaluation/Expansion/PropertyBagCardinalityBenchmark.cs | New benchmark varying unreferenced property-bag size while keeping references constant. |
| src/MSBuild.Benchmarks/Evaluation/Expansion/MixedExpansionBenchmark.cs | New mixed expansion benchmarks crossing property/item/metadata pipelines. |
| src/MSBuild.Benchmarks/Evaluation/Expansion/MetadataExpansionScalingBenchmark.cs | New metadata-reference scaling benchmarks. |
| src/MSBuild.Benchmarks/Evaluation/Expansion/MetadataExpansionBenchmark.cs | New focused metadata expansion benchmarks. |
| src/MSBuild.Benchmarks/Evaluation/Expansion/ItemFunctionExpansionBenchmark.cs | New focused item-function parsing/chaining expansion benchmarks. |
| src/MSBuild.Benchmarks/Evaluation/Expansion/ItemExpansionBenchmark.cs | New focused item-vector expansion/transform benchmarks. |
| src/MSBuild.Benchmarks/Evaluation/Conditions/ConditionStrings.cs | New shared condition-string corpus for parsing/evaluation suites. |
| src/MSBuild.Benchmarks/Evaluation/Conditions/ConditionParsingBenchmark.cs | New condition parse-only benchmark (scanner/parser/tree construction). |
| src/MSBuild.Benchmarks/Evaluation/Conditions/ConditionEvaluationBenchmark.cs | New end-to-end condition evaluation benchmark with warmed expression-tree cache. |
| src/MSBuild.Benchmarks/EscapingUtilitiesCachingBenchmark.cs | New per-iteration cache hit/miss benchmark for escaping cache behavior. |
| src/MSBuild.Benchmarks/EscapingUtilitiesBenchmark.cs | Removes cached-escape benchmarks now covered by the dedicated caching suite. |
| src/MSBuild.Benchmarks/DefiningProjectModifiersBenchmark.cs | Reworks setup to make defining-project cache behavior explicit/deterministic per iteration. |
There was a problem hiding this comment.
Generated by Expert Code Review (on open) for #14660 · sonnet46 · 75.9 AIC · ⌖ 5.63 AIC · ⊞ 5.2K
Comments that could not be inline-anchored
src/MSBuild.Benchmarks/Evaluation/Items/LazyItemEvaluationBenchmark.cs:52
Cross-platform correctness bug: The glob patterns in the project XML use hardcoded backslash separators (src\**\*.cs), but the actual files are created using Path.Combine which produces forward-slash paths on Linux/macOS. On non-Windows platforms, Path.Combine("src", $"dir{i % 10}", $"File{i}.cs") creates paths like src/dir0/File0.cs, while the MSBuild glob src\**\*.cs won't match them. This means the WildcardInclude, MetadataUpdate, Remove, and Combined benchmarks silentl…
src/MSBuild.Benchmarks/Evaluation/Items/LazyItemEvaluationBenchmark.cs:69
Cross-platform correctness bug (same as above, continued): The Update and Remove project XML snippets also use hardcoded backslash glob patterns (src\dir1\**\*.cs, src\dir9\**\*.cs). Same fix applies — use forward slashes.
Note that src\dir1\**\*.cs and src\dir9\**\*.cs also depend on the directory structure that uses i % 10 for subdirectory names (dir0–dir9). This part is fine, just needs the path separator fix.
src/MSBuild.Benchmarks/Evaluation/Items/LazyItemEvaluationBenchmark.cs:128
BenchmarkDotNet measurement concern — ProjectCollection created inside benchmark method: Each call to EvaluateItems creates a new ProjectCollection and loads/evaluates the project from disk. While the remarks note this intentionally, consider whether ProjectCollection construction and disposal overhead (which creates thread pools, registers with global state, etc.) is obscuring the lazy item evaluation costs you want to measure.
If the intent is to benchmark evaluation overhead onl…
src/MSBuild.Benchmarks/EscapingUtilitiesCachingBenchmark.cs:43
Potential measurement skew from [RunOncePerIteration] + [IterationSetup] interaction: [IterationSetup] runs before each iteration (including warmup), and [RunOncePerIteration] runs the benchmark body exactly once per iteration. The s_generation field is static, so it is shared across all benchmark methods in the class. When BenchmarkDotNet runs multiple jobs or methods in the same process, the counter can be non-zero for the first real measurement, which is fine. However, if the…
src/MSBuild.Benchmarks/Evaluation/Conditions/ConditionParsingBenchmark.cs:124
Cache pollution between iterations: ConditionEvaluationBenchmark explicitly primes the production expression-tree cache in GlobalSetup (which is correct), but ConditionParsingBenchmark runs new Parser().Parse(...) per benchmark invocation without noting whether the production ConditionEvaluator parser cache is warm or cold during measurement. If ConditionEvaluationBenchmark and ConditionParsingBenchmark run in the same process (same BDN job), the eval benchmark's warmup could …
src/MSBuild.Benchmarks/ItemSpecModifiersCachingBenchmark.cs:45
Not using shared TemporaryDirectory infrastructure: This benchmark manages its own temp directory (_tempDir) with manual creation/deletion logic, while the newly introduced TemporaryDirectory class exists precisely for this pattern. The existing code even uses "MSBuildBenchmarks" as the parent folder, whereas TemporaryDirectory uses "MSBuild.Benchmarks" — a minor inconsistency. Consider switching to TemporaryDirectory for consistency, or doing so in a follow-up. Not a blocking…
src/MSBuild.Benchmarks/DefiningProjectModifiersBenchmark.cs:45
Same TemporaryDirectory inconsistency: DefiningProjectModifiersBenchmark also manually manages its own temp directory using "MSBuildBenchmarks" as the parent, bypassing the new TemporaryDirectory infrastructure. The directory cleanup in GlobalCleanup (line 137) could leave orphaned directories if the cleanup fails or the process is killed. TemporaryDirectory.Dispose() has the same limitation, but centralizing cleanup logic makes it easier to improve later.
Replace the monolithic Expander benchmark with targeted property, item, metadata, mixed-expression, condition, shredder, and lazy-item suites. Add reusable setup helpers and make cache-state expectations explicit. Add a cross-TFM PowerShell runner with broad and granular benchmark sets, storing results under the repository artifacts directory.
Create fresh item instances for each iteration so cold and repeated modifier lookups measure the intended cache state. Clear the shared defining-project cache after setup to prevent state leaking between iterations. Batch item accesses and normalize results with OperationsPerInvoke. Move item discovery and materialization out of the measured code.
Separate cached escaping into explicit cache-hit and cache-miss cases. Prime hit inputs before measurement and generate unique miss inputs for each iteration so warmup cannot change the measured cache state. Batch operations to reduce harness overhead while preserving input length and escaping characteristics.
Expose launch count through the benchmark runner for higher-confidence comparisons across independent processes. Keep power-plan enforcement opt-in so dedicated machines retain their configured plan. Document smoke, exploratory, and confirmation runs, including execution cost and cross-framework comparison constraints.
Measure fixed property-expansion shapes with 10 and 100 unrelated properties in the backing bag. Include a no-expansion baseline to separate lookup effects from fixed benchmark overhead. Add a dedicated category and runner set while preserving the broader property-expansion scaling groups.
Resolve explicit relative artifact paths against PowerShell's current location instead of the process working directory, which may not track Set-Location. This ensures paths such as .\artifacts are created under the directory from which Run-Benchmarks.ps1 was invoked. Document the relative path behavior for the ArtifactsPath parameter.
b72d611 to
aba6a34
Compare
Document the intentional cold-path and cache-isolation behavior of the focused evaluation benchmarks. Use the shared temporary-directory helper for modifier benchmarks to centralize setup and cleanup.
|
Replies to Expert Code Review feedback that weren't presented as inline comments for some reason. Note that some of the feedback text was truncated.
|
Include transforms with and without arguments in the benchmark cases for collecting referenced item names and metadata.
BenchmarkDotNet redirects project outputs into a shared directory. The netstandard2.0 Framework and Utilities reference assemblies can then overwrite the current-TFM implementations and prevent the generated benchmark executable from loading. Skip the RoslynCodeTaskFactory reference payload only for benchmark builds. Regular Tasks builds retain the existing payload.
Resolve the SDK requested by global.json from DOTNET_INSTALL_DIR, PATH, or the repository-local .dotnet fallback. Print the selected SDK path and propagate its host settings to BenchmarkDotNet child processes and generated apphosts. Restore the caller's environment when benchmark execution completes.
Upgrade both BenchmarkDotNet packages to 0.16.0-preview.1. Disable central package management for the benchmark project so its newer Microsoft.CodeAnalysis.CSharp dependency is not downgraded to the version pinned for repository analyzers.
OvesN
left a comment
There was a problem hiding this comment.
Thank you for this PR! The benchmarks look reasonable
> [!IMPORTANT] > This depends on #14660. Until that PR merges, this PR includes its benchmark commits; the expansion changes begin after bd61887. > [!TIP] > Each commit is self-contained and has a good description. Consider reviewing commit-by-commit starting at 71d8b82. ### Context Expansion repeatedly scans strings for property, item, and metadata markers and performs parsing, dispatch, and context resolution for every match. These operations are pervasive during project evaluation, so even small per-expression or per-item costs accumulate across large solutions. This change optimizes those hot paths while preserving existing expansion behavior. > [!NOTE] > This is the first in a series of expansion performance PRs. It focuses on targeted, behavior-preserving reductions in CPU overhead; allocation-focused work and more fundamental architectural changes are intentionally left for follow-up PRs. There is still substantial low-hanging fruit in these paths. ### Changes Made - Add specialized marker detection and indexing APIs for `$(`, `@(`, and `%(` expressions, replacing duplicated and broader searches throughout evaluation. - Replace the stateful item-expression enumerator with indexed capture parsing and avoid wrapping large item captures in `Nullable<T>`. - Specialize common quoted-transform shapes so literal, exact, embedded-single, and multiple-match expressions avoid general-purpose scanning and builders. - Separate property-specific parenthesis scanning from general function argument parsing and classify ordinary properties while scanning. - Encapsulate property expansion state in a `readonly ref struct` and cache invariant expansion policy. - Join explicitly separated item vectors directly with `ValueStringBuilder` on .NET and `StringBuilderCache` on .NET Framework, avoiding intermediate collections and LINQ. - Cache item-spec modifier classification while parsing quoted-transform metadata. - Dispatch implicit quoted transforms directly instead of synthesizing an intrinsic function call and argument array. - Resolve project-directory and defining-project context only for modifiers that consume those values. ### Benchmark Results The baseline is `bd61887f13`; the optimized implementation is `34c6229708`. Negative timing percentages indicate faster execution. The four benchmark classes were unchanged between these commits. #### 🚀 Performance Highlights | Area | .NET 11 | LegacyJIT x86 | Allocation impact | |---|---:|---:|---:| | 🔍 Marker detection (`NoExpansion`) | **30.9–51.1% faster** | **40.6–56.0% faster** | ➖ Unchanged | | ✨ Quoted transform, 100 items | **48.3% faster** | **23.6% faster** | 📉 -32 B / -17 B | | 🔗 Explicit separator, 100 items | **12.1% faster** | **28.9% faster** | 📉 -1,664 B / -844 B | | 🏷️ Qualified metadata | **32.9% faster** | **19.1% faster** | ➖ Unchanged | | ⚙️ Multiple distinct properties | **15.8% faster** | **14.7% faster** | ➖ Unchanged | | 🧩 Mixed property, metadata, and 100-item transform | **59.3% faster** | **27.0% faster** | 📉 -32 B / -16 B | <details> <summary><strong>.NET 11.0 — RyuJIT x64</strong></summary> #### ItemExpansionBenchmark | Method | Items | Mean: baseline → latest | Time | Allocated: baseline → latest | |---|---:|---:|---:|---:| | NoExpansion | 10 | 26.22 → 13.92 ns | **-46.9%** | 0 → 0 B | | Simple | 10 | 573.49 → 557.89 ns | -2.7% | 280 → 280 B | | WithQuotedTransform | 10 | 3,029.15 → 1,712.49 ns | **-43.5%** | 1,024 → 992 B (-32 B) | | WithSeparator | 10 | 600.19 → 530.68 ns | **-11.6%** | 768 → 544 B (-29.2%) | | WithQuotedTransformAndUnescape | 10 | 3,016.00 → 1,725.81 ns | **-42.8%** | 1,024 → 992 B (-32 B) | | NoExpansion | 100 | 24.80 → 13.62 ns | **-45.1%** | 0 → 0 B | | Simple | 100 | 3,923.71 → 3,847.24 ns | -1.9% | 1,720 → 1,720 B | | WithQuotedTransform | 100 | 25,621.73 → 13,239.86 ns | **-48.3%** | 7,505 → 7,473 B (-32 B) | | WithSeparator | 100 | 3,552.17 → 3,123.38 ns | **-12.1%** | 5,624 → 3,960 B (-29.6%) | | WithQuotedTransformAndUnescape | 100 | 25,539.33 → 13,135.71 ns | **-48.6%** | 7,505 → 7,473 B (-32 B) | #### MetadataExpansionBenchmark | Method | Mean: baseline → latest | Time | Allocated | |---|---:|---:|---:| | NoExpansion | 15.81 → 10.92 ns | **-30.9%** | 0 → 0 B | | Unqualified | 167.10 → 116.72 ns | **-30.1%** | 0 → 0 B | | Qualified | 227.22 → 152.43 ns | **-32.9%** | 48 → 48 B | | Multiple | 290.44 → 224.46 ns | **-22.7%** | 0 → 0 B | | MultipleAndUnescape | 307.20 → 224.48 ns | **-26.9%** | 0 → 0 B | #### MixedExpansionBenchmark | Method | Mean: baseline → latest | Time | Allocated: baseline → latest | |---|---:|---:|---:| | NoExpansion | 38.72 → 18.94 ns | **-51.1%** | 0 → 0 B | | PropertyAndItem | 17,827.80 → 7,470.08 ns | **-58.1%** | 7,497 → 7,465 B (-32 B) | | PropertyAndMetadata | 525.56 → 405.57 ns | **-22.8%** | 0 → 0 B | | All | 19,144.79 → 7,785.14 ns | **-59.3%** | 7,665 → 7,633 B (-32 B) | | AllAndUnescape | 18,446.47 → 7,810.63 ns | **-57.7%** | 7,665 → 7,633 B (-32 B) | #### PropertyExpansionBenchmark | Method | Mean: baseline → latest | Time | Allocated | |---|---:|---:|---:| | NoExpansion | 16.95 → 11.52 ns | **-32.0%** | 0 → 0 B | | Single | 103.20 → 80.27 ns | **-22.2%** | 0 → 0 B | | Embedded | 209.95 → 205.71 ns | -2.0% | 40 → 40 B | | MultipleDistinct | 481.61 → 405.37 ns | **-15.8%** | 0 → 0 B | | MultipleRepeated | 472.70 → 402.12 ns | **-14.9%** | 0 → 0 B | | Undefined | 183.21 → 183.53 ns | +0.2% | 40 → 40 B | | LongValue | 88.46 → 73.90 ns | **-16.5%** | 0 → 0 B | | MultipleAndUnescape | 413.11 → 416.51 ns | +0.8% | 0 → 0 B | </details> <details> <summary><strong>.NET Framework 4.8.1 — LegacyJIT x86</strong></summary> #### ItemExpansionBenchmark | Method | Items | Mean: baseline → latest | Time | Allocated: baseline → latest | |---|---:|---:|---:|---:| | NoExpansion | 10 | 191.4 → 112.6 ns | **-41.2%** | 0 → 0 B | | Simple | 10 | 2,793.1 → 2,667.0 ns | -4.5% | 152 → 152 B | | WithQuotedTransform | 10 | 13,727.2 → 10,449.7 ns | **-23.9%** | 962 → 946 B (-16 B) | | WithSeparator | 10 | 2,246.3 → 1,674.0 ns | **-25.5%** | 593 → 469 B (-20.9%) | | WithQuotedTransformAndUnescape | 10 | 13,778.2 → 10,467.6 ns | **-24.0%** | 962 → 946 B (-16 B) | | NoExpansion | 100 | 190.9 → 112.4 ns | **-41.1%** | 0 → 0 B | | Simple | 100 | 19,269.2 → 18,822.0 ns | -2.3% | 874 → 874 B | | WithQuotedTransform | 100 | 120,620.9 → 92,212.8 ns | **-23.6%** | 7,816 → 7,799 B (-17 B) | | WithSeparator | 100 | 10,977.8 → 7,810.4 ns | **-28.9%** | 9,162 → 8,318 B (-9.2%) | | WithQuotedTransformAndUnescape | 100 | 121,049.3 → 91,529.3 ns | **-24.4%** | 7,816 → 7,799 B (-17 B) | #### MetadataExpansionBenchmark | Method | Mean: baseline → latest | Time | Allocated | |---|---:|---:|---:| | NoExpansion | 172.0 → 102.1 ns | **-40.6%** | 0 → 0 B | | Unqualified | 946.8 → 712.2 ns | **-24.8%** | 0 → 0 B | | Qualified | 1,120.2 → 905.8 ns | **-19.1%** | 40 → 40 B | | Multiple | 1,454.9 → 1,139.5 ns | **-21.7%** | 0 → 0 B | | MultipleAndUnescape | 1,523.0 → 1,170.3 ns | **-23.2%** | 0 → 0 B | #### MixedExpansionBenchmark | Method | Mean: baseline → latest | Time | Allocated: baseline → latest | |---|---:|---:|---:| | NoExpansion | 343.4 → 151.2 ns | **-56.0%** | 0 → 0 B | | PropertyAndItem | 79,038.1 → 57,716.0 ns | **-27.0%** | 7,806 → 7,790 B (-16 B) | | PropertyAndMetadata | 2,541.3 → 2,103.6 ns | **-17.2%** | 0 → 0 B | | All | 80,833.6 → 58,976.5 ns | **-27.0%** | 7,914 → 7,898 B (-16 B) | | AllAndUnescape | 81,588.6 → 59,358.2 ns | **-27.2%** | 7,914 → 7,898 B (-16 B) | #### PropertyExpansionBenchmark | Method | Mean: baseline → latest | Time | Allocated | |---|---:|---:|---:| | NoExpansion | 166.0 → 88.49 ns | **-46.7%** | 0 → 0 B | | Single | 459.8 → 333.3 ns | **-27.5%** | 0 → 0 B | | Embedded | 1,037.3 → 879.82 ns | **-15.2%** | 28 → 28 B | | MultipleDistinct | 2,269.6 → 1,935.24 ns | **-14.7%** | 0 → 0 B | | MultipleRepeated | 2,194.5 → 1,921.38 ns | **-12.4%** | 0 → 0 B | | Undefined | 959.3 → 827.33 ns | **-13.8%** | 28 → 28 B | | LongValue | 441.7 → 300.15 ns | **-32.0%** | 0 → 0 B | | MultipleAndUnescape | 2,301.5 → 1,908.8 ns | **-17.1%** | 0 → 0 B | </details> ### Analysis - Quoted item transforms improve by **42.8–48.6% on .NET 11** and **23.6–24.4% on LegacyJIT**. Savings scale with item count, confirming that per-item modifier classification and context lookup work was removed. - Mixed expressions containing a 100-item quoted transform improve by **57.7–59.3% on .NET 11** and approximately **27% on LegacyJIT**. - Metadata expansion improves by **19.1–32.9%** across qualified, unqualified, multiple, and unescaping cases without allocation changes. - Property expansion improves by **12.4–32.0% on LegacyJIT**. Common .NET 11 property shapes improve by **14.9–22.2%**; the small changes to embedded, undefined, and unescaping cases are within benchmark noise. - Explicit separator expansion reduces allocations by approximately **29% on .NET 11** and saves 124–844 B on LegacyJIT while improving execution by **11.6–28.9%**. - The fixed 32 B x64 and approximately 16 B x86 reduction in quoted and mixed item benchmarks matches removal of the synthetic transform argument array. - No benchmark case shows a meaningful timing regression or allocation increase. ### Testing - Full test suite passes. - Captured BenchmarkDotNet baselines and optimized results for `ItemExpansionBenchmark`, `MetadataExpansionBenchmark`, `MixedExpansionBenchmark`, and `PropertyExpansionBenchmark` on .NET 11.0 x64 and .NET Framework 4.8.1 LegacyJIT x86. ### Notes This change intentionally preserves existing expansion semantics and public API behavior. The focused benchmark infrastructure is reviewed separately in #14660.
Context
Performance investigations across evaluation and expansion need focused, repeatable benchmarks. The existing expander benchmark combined unrelated parameters, making targeted validation slow and obscuring the behavior being measured.
Changes Made
Testing
.\src\MSBuild.Benchmarks\Run-Benchmarks.ps1 -All -Job dry -LaunchCount 1on a DevBox VM.net472andnet11.0, producing 22 reports per target framework with no failures or missing results..\src\MSBuild.Benchmarks\Run-Benchmarks.ps1 -Set PropertyBagCardinality -Framework net11.0 -LaunchCount 3.Notes
This PR changes benchmark infrastructure only. Dry and short jobs are intended for execution checks, while final performance comparisons should use the default adaptive job with multiple launches on a controlled machine.