dotnet / dotnet/msbuild

[Performance]: Microsoft.Build.Evaluation.Expander can be more efficient by using Spans

Open
#10,961 2 comments 0 reactions 0 assignees View on GitHub
Area: Performance Priority:2 triaged
Dominant language
C#
Stars
5.5k
Forks
1.5k
Avg merge
1d 19h
Merged PRs (30d)
135

Description

### Issue Description

Noticed this while looking into a profile of VS solution load, particularly into string.Substring usage. [ExpandPropertiesLeaveTypedAndEscaped](https://github.com/dotnet/msbuild/blob/e4f527b8f8807345382bab5b5f1fe70ba6190cd0/src/Build/Evaluation/Expander.cs#L1184) showed up in this context, as about 0.3% of all allocations in VS during the solution load.

This led me to ping Rainer with the following questions, and he asked me to file this issue to track.

```
1) The substring call looks like it's done potentially twice, could the second call just use the existing value if it's not null?
2) The first calls ExpandRegistryValue with the substring value. It looks like that method could just take in a ReadOnlySpan and use that.
3) The second calls ExpandPropertyBody with the substring value. This one has a lot of uses of that value, but again, it appears it might be ReadOnlySpan'able.

Also, there is the SplitFileSpec call which is a separate question. I assume that changing that to ReadOnlySpans isn't feasible?
```

![Image](https://github.com/user-attachments/assets/7021f52b-a6c9-4e6e-9d14-07def120b2c7)

### Steps to Reproduce

Open Roslyn.sln in VS

### Data

The etl failed uploading several times. I've copied to an internal share: \\vwdbuild01\dev\toddgrun\msbuild\PerfViewData.etl.zip

### Analysis

_No response_

### Versions & Configurations

VS 17.13

### Regression

- [ ] yes
- [x] no

### Regression Details

_No response_

Contributor guide

No contributing guide indexed for this repository

Research direction

Start in src/Build/Evaluation/Expander.cs at ExpandPropertiesLeaveTypedAndEscaped and trace its Substring calls into ExpandRegistryValue and ExpandPropertyBody. Evaluate whether existing values and ReadOnlySpan can avoid the reported allocations, and separately assess the feasibility of changing SplitFileSpec; done means addressing the feasible allocation reductions without changing expansion behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
build-system, performance
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.