Skip to content

Commit

Permalink
Use values iterator and update in map equality/hash (#718)
Browse files Browse the repository at this point in the history
Initially I tried using `entries` instead of a `values` iterator, but that was surprisingly slow. This appears to be a good middle ground (see history of this comment for old results).

Hashing is about the same, but much faster for maps which don't have O(1) lookup. Equality is much faster.

## Benchmarks before:

DeepCollectionQualityUnordered.equals(RunTime): 7427.29020979021 us.
DeepCollectionQualityUnordered.hash(RunTime): 217.8173707406213 us.
DeepCollectionQuality.equals(RunTime): 2653.23875 us.
DeepCollectionQuality.hash(RunTime): 178.1674653887114 us.

## Benchmarks after:

DeepCollectionQualityUnordered.equals(RunTime): 4435.374 us.
DeepCollectionQualityUnordered.hash(RunTime): 212.8631545473818 us.
DeepCollectionQuality.equals(RunTime): 1989.1746626686656 us.
DeepCollectionQuality.hash(RunTime): 178.3396697902722 us.
  • Loading branch information
jakemac53 authored Nov 6, 2024
1 parent 9ab5a18 commit 1156cfe
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
3 changes: 3 additions & 0 deletions pkgs/collection/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
## 1.19.1-wip

- Add `IterableMapEntryExtension` for working on `Map` as a list of pairs, using
`Map.entries`.
- Optimize equality and hash code for maps by using `update` and a `values`
iterator to avoid extra lookups.

## 1.19.1

Expand Down
26 changes: 18 additions & 8 deletions pkgs/collection/lib/src/equality.dart
Original file line number Diff line number Diff line change
Expand Up @@ -325,16 +325,19 @@ class MapEquality<K, V> implements Equality<Map<K, V>> {
var length = map1.length;
if (length != map2.length) return false;
Map<_MapEntry, int> equalElementCounts = HashMap();
var values1 = map1.values.iterator;
for (var key in map1.keys) {
var entry = _MapEntry(this, key, map1[key]);
var count = equalElementCounts[entry] ?? 0;
equalElementCounts[entry] = count + 1;
var value = (values1..moveNext()).current;
var entry = _MapEntry(this, key, value);
equalElementCounts.update(entry, _addOne, ifAbsent: _one);
}
final values2 = map2.values.iterator;
for (var key in map2.keys) {
var entry = _MapEntry(this, key, map2[key]);
var count = equalElementCounts[entry];
if (count == null || count == 0) return false;
equalElementCounts[entry] = count - 1;
var value = (values2..moveNext()).current;
var entry = _MapEntry(this, key, value);
var count = equalElementCounts.update(entry, _subtractOne,
ifAbsent: _negativeOne);
if (count < 0) return false;
}
return true;
}
Expand All @@ -343,9 +346,11 @@ class MapEquality<K, V> implements Equality<Map<K, V>> {
int hash(Map<K, V>? map) {
if (map == null) return null.hashCode;
var hash = 0;
var values = map.values.iterator;
for (var key in map.keys) {
var value = (values..moveNext()).current;
var keyHash = _keyEquality.hash(key);
var valueHash = _valueEquality.hash(map[key] as V);
var valueHash = _valueEquality.hash(value);
hash = (hash + 3 * keyHash + 7 * valueHash) & _hashMask;
}
hash = (hash + (hash << 3)) & _hashMask;
Expand Down Expand Up @@ -489,3 +494,8 @@ class CaseInsensitiveEquality implements Equality<String> {
@override
bool isValidKey(Object? object) => object is String;
}

int _addOne(int i) => i + 1;
int _subtractOne(int i) => i - 1;
int _one() => 1;
int _negativeOne() => -1;

0 comments on commit 1156cfe

Please sign in to comment.