-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
✨ Implement ImmutableSegmentedHashSet<T> #54719
Conversation
These types are copied from dotnet/runtime@v5.0.8.
a3df5bf
to
0ca55e6
Compare
@dotnet/roslyn-compiler this is now ready for review |
} | ||
else | ||
{ | ||
// TODO: Avoid the builder allocation |
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.
Are these todo's from the original source?
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.
No, these are ways the current implementation could theoretically be improved. My hope is that if someone sees allocations of certain types in the future, they are directed to the most straightforward way to eliminate those allocations.
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.
Here's an example of how I broke out a ValueBuilder
nested type to reduce builder allocations in ImmutableSegmentedList<T>
:
#54424
https://github.com/dotnet/roslyn/blob/main/src/Dependencies/Collections/ImmutableSegmentedList%601%2BValueBuilder.cs
@dotnet/roslyn-compiler for second review |
4 similar comments
@dotnet/roslyn-compiler for second review |
@dotnet/roslyn-compiler for second review |
@dotnet/roslyn-compiler for second review |
@dotnet/roslyn-compiler for second review |
How can we see the delta from what the BCL sources were to what you added as a part of moving to Roslyn? I'm less interested in reviewing the BCL sources (battle tested, already reviewed, etc ...). Think it's more interesting for us to look at the delta to see what has changed and evaluate that. |
@jaredpar There is no exact mirror for this type in the BCL. The closest we have is the AVL-backed Note that I followed the pattern from earlier immutable types in this library and made |
/// <inheritdoc cref="ImmutableHashSet{T}.Clear()"/> | ||
public ImmutableSegmentedHashSet<T> Clear() | ||
{ | ||
var self = this; |
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.
Unsure what the goal of this pattern is here. Can you elaborate?
I can only see this being of value if you're protecting against mixed read / writes of this value in a location. Don't think we generally protect against incorrect uses like that.
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.
Good catch. This type appears to be missing the equivalent of this notice which is applied to ImmutableSegmentedList<T>
:
roslyn/src/Dependencies/Collections/ImmutableSegmentedList`1.cs
Lines 59 to 69 in 519058e
/// <para><strong>IMPORTANT NOTICE FOR MAINTAINERS AND REVIEWERS:</strong></para> | |
/// | |
/// <para>This type should be thread-safe. As a struct, it cannot protect its own fields from being changed from one | |
/// thread while its members are executing on other threads because structs can change <em>in place</em> simply by | |
/// reassigning the field containing this struct. Therefore it is extremely important that <strong>⚠⚠ Every member | |
/// should only dereference <c>this</c> ONCE ⚠⚠</strong>. If a member needs to reference the | |
/// <see cref="_list"/> field, that counts as a dereference of <c>this</c>. Calling other instance members | |
/// (properties or methods) also counts as dereferencing <c>this</c>. Any member that needs to use <c>this</c> more | |
/// than once must instead assign <c>this</c> to a local variable and use that for the rest of the code instead. | |
/// This effectively copies the one field in the struct to a local variable so that it is insulated from other | |
/// threads.</para> |
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.
I'll file a follow-up issue to add this documentation and implement the value builder.
Builds on #54574