Skip to content

Reduce allocations from doing repeated adds into an ImmutableDictionary in SolutionCompilationState.ComputeFrozenSnapshot#76361

Merged
ToddGrun merged 2 commits intomainfrom
dev/toddgrun/CompilationTrackerImmutableDictionaryAllocations
Dec 12, 2024
Merged

Reduce allocations from doing repeated adds into an ImmutableDictionary in SolutionCompilationState.ComputeFrozenSnapshot#76361
ToddGrun merged 2 commits intomainfrom
dev/toddgrun/CompilationTrackerImmutableDictionaryAllocations

Conversation

@ToddGrun
Copy link
Contributor

@ToddGrun ToddGrun commented Dec 10, 2024

Instead, use an ImmutableSegmentedDictionaryBuilder to do the adds and then do a single update to the SolutionCompilationState._projectIdToTrackerMap after all new compilation trackers are created.

Note that there is a potential negative aspect to this change as it increases the time window during which a compilation state can be queried for in the SolutionCompilationState and not found. However, I don't believe this will cause functional issues, and wasn't hitting that concern during local testing.

*** allocations before change from typing period in csharpediting speedometer ***
image

…llocations ***

Reduce allocations from doing repeated adds into an ImmutableDictionary in SolutionCompilationState.ComputeFrozenSnapshot

Instead, use an ImmutableSegmentedDictionaryBuilder to do the adds and then do a single update to the SolutionCompilationState._projectIdToTrackerMap after all new compilation trackers are created.

Note that there is a potential negative aspect to this change as it increases the time window during which a compilation state can be queried for in the SolutionCompilationState and not found. However, I don't believe this will cause functional issues, and wasn't hitting that concern during local testing.
@ghost ghost added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead labels Dec 10, 2024
@ToddGrun
Copy link
Contributor Author

@ToddGrun
Copy link
Contributor Author

Test insertion PR: https://dev.azure.com/devdiv/DevDiv/_git/VS/pullrequest/597687

Speedometer results look good:
image

image

@ToddGrun ToddGrun marked this pull request as ready for review December 11, 2024 11:51
@ToddGrun ToddGrun requested a review from a team as a code owner December 11, 2024 11:51
@ToddGrun ToddGrun changed the title WIP: Reduce allocations from doing repeated adds into an ImmutableDictionary in SolutionCompilationState.ComputeFrozenSnapshot Reduce allocations from doing repeated adds into an ImmutableDictionary in SolutionCompilationState.ComputeFrozenSnapshot Dec 11, 2024
}

// Update _projectIdToTrackerMap to include all the newly created compilation trackers.
RoslynImmutableInterlocked.InterlockedExchange(ref _projectIdToTrackerMap, updatedIdToTrackerMapBuilder.ToImmutable());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i feel like there's some sort of race here. where multiple threads come in, update the build, and the other maps, and then blast back into this field. this actually feels like it could potentially be a problem. convince me this is 100% safe.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this may need to be a spin, with an interlocked compare-exchange. and if the field changes out from underneath us, we have to go redo this work to pick up any comp trackers made by another thread.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I'm now not convinced there wasn't a timing issue in this code before.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, this PR was the one that added the multiple usages of _projectIdToTrackerMap within the method. I've pushed out a commit that I think essentially does what you requested.

@ToddGrun
Copy link
Contributor Author

ToddGrun commented Dec 11, 2024

Commit 2 VS Test insertion to get new speedometer numbers:

https://dev.azure.com/devdiv/DevDiv/_git/VS/pullrequest/598001

Results look good again:

image

image

@ToddGrun ToddGrun merged commit c30de30 into main Dec 12, 2024
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Dec 12, 2024
@dibarbet dibarbet modified the milestones: Next, 17.13 P3 Jan 7, 2025
@phil-allen-msft phil-allen-msft deleted the dev/toddgrun/CompilationTrackerImmutableDictionaryAllocations branch February 13, 2026 17:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead VSCode

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants