-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Prototype EmptyArray optimization for Dictionary #114792
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
Conversation
Tagging subscribers to this area: @dotnet/area-system-collections |
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... |
@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
@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;
}
} |
Looks like this change doesn't make SCG faster. |
No description provided.