Skip to content
This repository was archived by the owner on Sep 25, 2026. It is now read-only.

Improve ValueSet implementation to avoid HashSet creation every time - #2397

Merged
vitek-karas merged 5 commits into
dotnet:feature/damAnalyzerfrom
vitek-karas:BetterValueSet
Dec 1, 2021
Merged

vitek-karas merged 5 commits into
dotnet:feature/damAnalyzerfrom
vitek-karas:BetterValueSet

Conversation

@vitek-karas

@vitek-karas vitek-karas commented Nov 25, 2021 •

Copy link
Copy Markdown
Member

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 (SingleValue is a record class, ValueNode in 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.

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.
@vitek-karas
vitek-karas requested a review from sbomer November 25, 2021 17:52
@vitek-karas vitek-karas self-assigned this Nov 25, 2021
yield break;

foreach (var targetValue in target.Values) {
foreach (var targetValue in target) {

This comment was marked as resolved.

{
_state = 0;
if (values is SealedHashSet valuesSet) {
_enumerator = valuesSet.GetEnumerator ();

@Therzok Therzok Nov 26, 2021 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Would defining a new generic argument constraint over TValue help prevent the boxing?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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)

Comment thread src/ILLink.Shared/DataFlow/ValueSet.cs Outdated
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>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why not call it EnumerableValues to make it clear what it does?
I'd also seal the class

Comment thread src/ILLink.Shared/DataFlow/ValueSet.cs Outdated
// if the class is sealed (as then the runtime can do a simple method table pointer comparison)
class SealedHashSet : HashSet<TValue>
{
public SealedHashSet () { }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why is this ctor needed?

Comment thread src/ILLink.Shared/DataFlow/ValueSet.cs Outdated
return hashCode;
}
if (_values is SealedHashSet valuesSet) {
int hashCode = 0;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'd move the code to SealedHashSet

@sbomer sbomer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Very nice, thank you!

Comment thread src/ILLink.Shared/DataFlow/ValueSet.cs Resolved
Comment thread src/ILLink.Shared/DataFlow/ValueSet.cs Outdated

// 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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe the difference is that for HashSet<T>, the T isn't constrained to notnull like it is here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I could swear I looked through the HashSet code and tried something similar with the T. Anyway...
Thanks a lot!

Comment thread src/ILLink.Shared/DataFlow/ValueSet.cs Resolved
@vitek-karas
vitek-karas merged commit f49f449 into dotnet:feature/damAnalyzer Dec 1, 2021
@vitek-karas
vitek-karas deleted the BetterValueSet branch December 1, 2021 12:25
agocke pushed a commit to dotnet/runtime that referenced this pull request Nov 16, 2022
…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
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants