-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Reduce unsafe usage in AdaptiveCapacityDictionary #64617
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Reduce unsafe usage in AdaptiveCapacityDictionary #64617
Conversation
There was a problem hiding this 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 removes unnecessary unsafe memory operations from AdaptiveCapacityDictionary by replacing MemoryMarshal.GetArrayDataReference and MemoryMarshal.CreateSpan with the safer AsSpan method. The change also optimizes the FindIndex method by caching the span property access outside the loop.
- Simplified
ArrayStorageSpanproperty to useAsSpan(0, _count)instead of unsafeMemoryMarshalAPIs - Optimized
FindIndexby cachingArrayStorageSpanto avoid repeated property access within the loop
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.
Removes unnecessary calls to the unsafe
MemoryMarshal.GetArrayDataReferenceandMemoryMarshal.CreateSpanmethods. The only thing this was doing was saving a single bounds check per call toFindIndexorTryFindItem, and this single bounds check is dwarfed by the call toIEqualityComparer<T>.Equalswithin the loop.I hoisted the span into a local within the
FindIndexmethod to ensure the bounds check occurs only once ahead of the loop, not within the loop itself.