Skip to content

Conversation

@kg
Copy link
Member

@kg kg commented Apr 17, 2025

No description provided.

@kg kg added NO-MERGE The PR is not ready for merge yet (see discussion for detailed reasons) NO-REVIEW Experimental/testing PR, do NOT review it labels Apr 17, 2025
@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-collections
See info in area-owners.md if you want to be subscribed.

@kg
Copy link
Member Author

kg commented Apr 17, 2025

We have a few tests that measure how many times GetHashCode or Equals get called on the comparer, which requires a check for a count of zero before doing a search. That might shed the performance benefits of this optimization...

@kg
Copy link
Member Author

kg commented Apr 17, 2025

@EgorBot -intel -amd

using System.Collections.Generic;
using BenchmarkDotNet.Attributes;

public class Bench
{
    const int Count = 40960;
    private static readonly Dictionary<int, int> ints = new ();

    static Bench () {
        for (int i = 0; i < Count; i++)
            ints[i] = i;
    }

    [Benchmark]
    public bool FindPresent() {
        for (int i = 0; i < Count; i++)
            if (!ints.TryGetValue(i, out _))
                return false;

        return true;
    }

    [Benchmark]
    public bool FindMissing() {
        for (int i = 0; i < Count; i++)
            if (ints.TryGetValue(i + Count, out _))
                return false;

        return true;
    }
}

Remove the _count check to see if we can claw back the small perf win
@kg
Copy link
Member Author

kg commented Apr 18, 2025

@EgorBot -intel -amd

using System;
using System.Collections.Generic;
using BenchmarkDotNet.Attributes;

public class Bench
{
    const int Count = 40960;
    private static readonly Dictionary<int, int> ints = new (Count * 2);
    private static readonly List<int> keys = new ();

    static Bench () {
        var rng = new Random(1);
        for (int i = 0; i < Count; i++) {
            var key = rng.Next();
            keys.Add(key);
            ints[keys[i]] = i;
        }
    }

    [Benchmark]
    public bool FindPresent() {
        for (int i = 0; i < Count; i++)
            if (!ints.TryGetValue(keys[i], out _))
                return false;

        return true;
    }
}

@kg
Copy link
Member Author

kg commented Apr 18, 2025

Looks like this change doesn't make SCG faster.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-System.Collections NO-MERGE The PR is not ready for merge yet (see discussion for detailed reasons) NO-REVIEW Experimental/testing PR, do NOT review it

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant