Skip to content

Commit

Permalink
faster TryFindReferenceEqual
Browse files Browse the repository at this point in the history
  • Loading branch information
maximv committed Nov 7, 2020
1 parent 866d6eb commit 2f00459
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
14 changes: 7 additions & 7 deletions playground/ImTools.Benchmarks/ImHashMapBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -880,13 +880,13 @@ public class Lookup
| Method | Count | Mean | Error | StdDev | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated |
|------------------------------- |------ |----------:|----------:|----------:|------:|--------:|------:|------:|------:|----------:|
| ImHashMap_TryFind | 1 | 7.569 ns | 0.2924 ns | 0.7117 ns | 1.00 | 0.00 | - | - | - | - |
| Experimental_ImHashMap_TryFind | 1 | 9.887 ns | 0.3380 ns | 0.7900 ns | 1.32 | 0.17 | - | - | - | - |
| ImHashMap234_TryFind | 1 | 8.864 ns | 0.3180 ns | 0.7557 ns | 1.18 | 0.16 | - | - | - | - |
| ImHashMap_TryFind | 1 | 7.741 ns | 0.3234 ns | 0.9435 ns | 1.00 | 0.00 | - | - | - | - |
| Experimental_ImHashMap_TryFind | 1 | 9.964 ns | 0.3488 ns | 0.9370 ns | 1.31 | 0.20 | - | - | - | - |
| ImHashMap234_TryFind | 1 | 7.620 ns | 0.3250 ns | 0.9376 ns | 1.00 | 0.18 | - | - | - | - |
| | | | | | | | | | | |
| ImHashMap_TryFind | 10 | 10.840 ns | 0.3560 ns | 0.8866 ns | 1.00 | 0.00 | - | - | - | - |
| Experimental_ImHashMap_TryFind | 10 | 15.429 ns | 0.3848 ns | 0.3214 ns | 1.41 | 0.13 | - | - | - | - |
| ImHashMap234_TryFind | 10 | 12.232 ns | 0.3887 ns | 0.9894 ns | 1.13 | 0.14 | - | - | - | - |
| ImHashMap_TryFind | 10 | 11.255 ns | 0.3801 ns | 0.9813 ns | 1.00 | 0.00 | - | - | - | - |
| Experimental_ImHashMap_TryFind | 10 | 15.733 ns | 0.4537 ns | 1.1129 ns | 1.41 | 0.16 | - | - | - | - |
| ImHashMap234_TryFind | 10 | 10.676 ns | 0.3391 ns | 0.9167 ns | 0.96 | 0.12 | - | - | - | - |
*/
[Params(1, 10)]//, 100, 1_000)]// the 1000 does not add anything as the LookupKey stored higher in the tree, 1000)]
Expand Down Expand Up @@ -1086,7 +1086,7 @@ public string Experimental_ImHashMap_TryFind()
[Benchmark]
public string ImHashMap234_TryFind()
{
_map234.TryFind(LookupKey.GetHashCode(), LookupKey, out var result);
_map234.TryFindReferenceEqual(LookupKey.GetHashCode(), LookupKey, out var result);
return (string)result;
}

Expand Down
29 changes: 29 additions & 0 deletions src/ImTools/ImTools.Experimental.234.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,35 @@ public static bool TryFind<K, V>(this ImHashMap234<K, V> map, int hash, K key, o
return false;
}

/// <summary>Looks up for the key using its hash code and returns the `true` and the found value or `false`</summary>
[MethodImpl((MethodImplOptions)256)]
public static bool TryFindReferenceEqual<K, V>(this ImHashMap234<K, V> map, int hash, K key, out V value) where K : class
{
var e = map.GetEntryOrDefault(hash);

if (e is ImHashMap234<K, V>.ValueEntry v)
{
if (e.Key == key)
{
value = v.Value;
return true;
}
}
else if (e is ImHashMap234<K, V>.ConflictsEntry c)
{
foreach (var x in c.Conflicts)
if (x.Key == key)
{
value = x.Value;
return true;
}
}

value = default(V);
return false;
}


/// <summary>Looks up for the key using its hash code and returns the `true` and the found value or `false`</summary>
[MethodImpl((MethodImplOptions)256)]
public static bool TryFind<K, V>(this ImHashMap234<K, V> map, K key, out V value) =>
Expand Down

0 comments on commit 2f00459

Please sign in to comment.