dotnet / dotnet/msbuild

[Performance]: BuildEventArgsWriter.WriteProjectItems spends a large % CPU time on CopyOnWriteDictionary

Open
#10,490 0 comments 0 reactions 1 assignee Claimed by @rainersigwald View on GitHub
Area: Performance Priority:3 triaged
Dominant language
C#
Stars
5.5k
Forks
1.5k
Avg merge
1d 8h
Merged PRs (30d)
141

Description

### Issue Description

While profiling BinaryReader/Writer, I have noticed a large % of time in creating CopyOnWriteDictionary/ImmutableDictionary. This event is triggered from LogProjectEvaluationFinished() where it enumerates through all the items and metadata. This is a lot of data but it should not be as much seen. The time is spent in creating the list of metadata using CopyOnWriteDictionary/ImmutableDictionary but it discard shortly afterwards.

### Steps to Reproduce

I used a medium to large solution of 200-300 projects to run the `clean` target (on an already cleaned build). This focuses the time spent on Project Evaluations.

Message me if you like access to my specific solution.

### Data

![image](https://github.com/user-attachments/assets/32c32660-9abf-4cb7-b7b7-e01e1e374d36)
![image](https://github.com/user-attachments/assets/a7713122-14c1-4305-b2d2-ba24b4df9dde)

### Analysis

I have commented out the writer of `foreach (var kvp in item.EnumerateMetadata())` in BuildEventArgsWriter.cs. The repro improved from 10s to 6s. It is hard to confirm if 6s is correct final performance, as there are a lot of Lazy or delayed evaluations, but it should be closer to 6s or 7s than 10s.

### Versions & Configurations

_No response_

### Regression

- [ ] yes
- [X] no

### Regression Details

No, but it is hard to confirm as this is my first time profiling this code path.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.