Repository navigation
Improve ValueSet implementation to avoid HashSet creation every time - #2397
Conversation
The idea is simple, store only a single object field. If the field is null, the set is empty. If the field is a HashSet, then it contains multiple values in that hashset. If the field is TValue, then it set contains single value - that value. I probably went a bit overboard on optimizations here, but I wanted to try what it takes to implement a good collection class. The result should not allocate at all when "Foreached" over if it contains no or single value (by far the most common case). Unfortunately it still has to box the value. Maybe we should store two fields to avoid boxing by adding a cost of a single pointer field.
| yield break; | ||
|
|
||
| foreach (var targetValue in target.Values) { | ||
| foreach (var targetValue in target) { |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
| { | ||
| _state = 0; | ||
| if (values is SealedHashSet valuesSet) { | ||
| _enumerator = valuesSet.GetEnumerator (); |
There was a problem hiding this comment.
Would defining a new generic argument constraint over TValue help prevent the boxing?
There was a problem hiding this comment.
I didn't try to optimize the case where it falls back to the hashset. Based on measurements in illink, that case is super rare (for a simple ASP.NET app out of 8200 values inspected, only 100 has more than value in them - those would fall back to hashset)
| public readonly HashSet<TValue>? Values; | ||
| // Since we're going to do lot of type checks for this class a lot, it is much more efficient | ||
| // if the class is sealed (as then the runtime can do a simple method table pointer comparison) | ||
| class SealedHashSet : HashSet<TValue> |
There was a problem hiding this comment.
Why not call it EnumerableValues to make it clear what it does?
I'd also seal the class
| // if the class is sealed (as then the runtime can do a simple method table pointer comparison) | ||
| class SealedHashSet : HashSet<TValue> | ||
| { | ||
| public SealedHashSet () { } |
There was a problem hiding this comment.
Why is this ctor needed?
| return hashCode; | ||
| } | ||
| if (_values is SealedHashSet valuesSet) { | ||
| int hashCode = 0; |
There was a problem hiding this comment.
I'd move the code to SealedHashSet
|
|
||
| // TODO: How to get this to work - without the '!' at the end this complains that default can return null. | ||
| // But how does this work for HashSet<T>? That will return null from Current in reality as well... | ||
| // It seems that the nullability is not propagated "inside" HashSet (since that one is implemented with TValue being nullable) |
There was a problem hiding this comment.
Maybe the difference is that for HashSet<T>, the T isn't constrained to notnull like it is here.
There was a problem hiding this comment.
Yes - that is the difference. Hashset implements this without the ! obviously. But if I use Hashset with a T which is constrained, like this:
class NonNullHashSet<T> : HashSet<T> where T : notnull { }There's no warning generated by the compiler - even though the implementation is identical. (I know that the compiler can't really warn here as it doesn't see INTO the hashset).
@agocke - maybe you know about this problem some more?
I don't really mind the ! mainly because IEnumerator.Current is defined as returning default if the enumerator is not positioned on a valid value (before start, or after end), so the functionality is correct. I'm just curious how this is supposed to work.
There was a problem hiding this comment.
It looks like the compiler does warn when T is unconstrained, in case it's instantiated with a non-nullable reference type.
And the implementation of HashSet<T> uses ! I think for the same reason you did here:
https://github.com/dotnet/runtime/blob/85642f8147d9e2ba860c21baea25cbb4f12e665e/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/HashSet.cs#L1541.
There was a problem hiding this comment.
I could swear I looked through the HashSet code and tried something similar with the T. Anyway...
Thanks a lot!
Co-authored-by: Sven Boemer <[email protected]>
…otnet/linker#2397) The idea is simple, store only a single object field. If the field is null, the set is empty. If the field is a HashSet, then it contains multiple values in that hashset. If the field is TValue, then it set contains single value - that value. I probably went a bit overboard on optimizations here, but I wanted to try what it takes to implement a good collection class. The result should not allocate at all when "Foreached" over if it contains no or single value (by far the most common case). Co-authored-by: Sven Boemer <[email protected]> Commit migrated from dotnet/linker@f49f449
The idea is simple, store only a single object field.
If the field is null, the set is empty.
If the field is a HashSet, then it contains multiple values in that hashset.
If the field is TValue, then it set contains single value - that value.
I probably went a bit overboard on optimizations here, but I wanted to try what it takes to implement a good collection class. The result should not allocate at all when "Foreached" over if it contains no or single value (by far the most common case).
Unfortunately it still has to box the value. Maybe we should store two fields to avoid boxing by adding a cost of a single pointer field.I just realized that all our uses cases will store reference types as values (
SingleValueis a record class,ValueNodein the linker currently is just a normal class). This means that when a single value is stored the ValueSet will have the same memory and GC footprint as a direct reference to the value.