Skip to content

Conversation

@Erarndt
Copy link
Contributor

@Erarndt Erarndt commented Jul 7, 2025

Fixes #

Context

The WeakStringCache used to store all the strings in the same dictionary, but was recently switched to use separate collections to store the longer strings that are held by weak references vs shorter strings that always used a string reference. In both cases, a StringWeakHandled was allocated to track the value in the dictionary. This is only needed when we use a handle to track larger strings, so we can avoid the allocation in most cases and simplify StringWeakHandle since it no longer has to potentially store a strong reference to a string. This saves ~20MB of allocations when building Roslyn.

Before:
image

After:
image

Changes Made

Testing

Notes

Copilot AI review requested due to automatic review settings July 7, 2025 21:49
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR simplifies the weak‐string caching logic by storing short strings directly in the concurrent cache and removing the extra strong‐reference path from the single‐threaded cache.

  • Removed the referencedString field and length check in StringWeakHandle, using only a weak GCHandle
  • Switched _stringsByHashCode in the concurrent cache to hold raw strings and introduced _weakHandlesByHashCode for long strings
  • Inlined and refactored GetOrCreateEntry into two local functions for the strong‐ and weak‐handle paths

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/StringTools/WeakStringCache.cs Dropped referencedString and always uses weakHandle; removed the length constant
src/StringTools/WeakStringCache.Concurrent.cs Changed strong‐string cache to ConcurrentDictionary<int, string>, refactored retrieval logic into local functions
Comments suppressed due to low confidence (1)

src/StringTools/WeakStringCache.cs:74

  • [nitpick] All strings now use a weak GCHandle, which may introduce extra allocation and collection overhead for short strings. If preserving short‐string references was intentional, consider reintroducing a strong‐reference path for those below a length threshold.
                if (weakHandle.IsAllocated)

Copy link
Member

@SimaTian SimaTian left a comment

Choose a reason for hiding this comment

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

Reasonable change in an are we recently modified - so it should be safe enough to update.

@ghost ghost assigned JanProvaznik Aug 4, 2025
@JanProvaznik JanProvaznik merged commit b81081a into dotnet:main Aug 20, 2025
9 checks passed
JanProvaznik added a commit that referenced this pull request Aug 26, 2025
@Erarndt Erarndt deleted the dev/erarndt/StringWeakHandle branch September 22, 2025 18:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants