Skip to content

Commit 592e429

Browse files
authored
[record_use] Cache hash codes in Expando instances (dart-lang#3150)
For dart-lang#3115, we want to store the depth of constant objects in an `Expando`, to avoid exponential runtime in deep constants. We already use hashmaps/hashsets on constants, and computing the hashCode is currently already recursing. To make computing the hashcode of a large `Recordings` linear, cache the `hashCode`s in an `Expando`. See the recommendation of this pattern in: * dart-lang/language#2225
1 parent 4cbd204 commit 592e429

6 files changed

Lines changed: 49 additions & 27 deletions

File tree

‎pkgs/record_use/lib/src/constant.dart‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ final class ListConstant extends Constant {
390390
const ListConstant(this.value);
391391

392392
@override
393-
int get hashCode => deepHash(value);
393+
int get hashCode => cacheHashCode(() => deepHash(value));
394394

395395
@override
396396
bool operator ==(Object other) {
@@ -436,8 +436,10 @@ final class MapConstant extends Constant {
436436
const MapConstant(this.entries);
437437

438438
@override
439-
int get hashCode => Object.hashAll(
440-
entries.map((e) => Object.hash(e.key, e.value)),
439+
int get hashCode => cacheHashCode(
440+
() => Object.hashAll(
441+
entries.map((e) => Object.hash(e.key, e.value)),
442+
),
441443
);
442444

443445
@override
@@ -534,7 +536,8 @@ final class InstanceConstant extends Constant {
534536
}
535537

536538
@override
537-
int get hashCode => Object.hash(definition, deepHash(fields));
539+
int get hashCode =>
540+
cacheHashCode(() => Object.hash(definition, deepHash(fields)));
538541

539542
@override
540543
String toString() =>
@@ -614,7 +617,9 @@ final class EnumConstant extends Constant {
614617
}
615618

616619
@override
617-
int get hashCode => Object.hash(definition, index, name, deepHash(fields));
620+
int get hashCode => cacheHashCode(
621+
() => Object.hash(definition, index, name, deepHash(fields)),
622+
);
618623

619624
@override
620625
String toString() =>
@@ -681,7 +686,9 @@ final class RecordConstant extends Constant {
681686
}
682687

683688
@override
684-
int get hashCode => Object.hash(deepHash(positional), deepHash(named));
689+
int get hashCode => cacheHashCode(
690+
() => Object.hash(deepHash(positional), deepHash(named)),
691+
);
685692

686693
@override
687694
String toString() =>

‎pkgs/record_use/lib/src/definition.dart‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'package:meta/meta.dart';
66

7+
import 'helper.dart';
78
import 'syntax.g.dart';
89

910
/// A unique identifier for a code element, such as a class, method,
@@ -84,7 +85,8 @@ class Definition {
8485
}
8586

8687
@override
87-
int get hashCode => Object.hash(library, Object.hashAll(path));
88+
int get hashCode =>
89+
cacheHashCode(() => Object.hash(library, Object.hashAll(path)));
8890

8991
/// Returns a URI representation of this definition.
9092
///
@@ -143,10 +145,8 @@ class Name {
143145
}
144146

145147
@override
146-
int get hashCode => Object.hash(
147-
name,
148-
kind,
149-
Object.hashAllUnordered(disambiguators),
148+
int get hashCode => cacheHashCode(
149+
() => Object.hash(name, kind, Object.hashAllUnordered(disambiguators)),
150150
);
151151

152152
/// Returns a string representation of this name that can be used as a part of

‎pkgs/record_use/lib/src/helper.dart‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,11 @@ import 'package:collection/collection.dart';
77
final deepEquals = const DeepCollectionEquality().equals;
88

99
final deepHash = const DeepCollectionEquality().hash;
10+
11+
final _hashCodeCache = Expando<int>();
12+
13+
extension HashCodeCaching on Object {
14+
/// Caches the hash code of this object.
15+
int cacheHashCode(int Function() compute) =>
16+
_hashCodeCache[this] ??= compute();
17+
}

‎pkgs/record_use/lib/src/metadata.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class Metadata {
6565
}
6666

6767
@override
68-
int get hashCode => deepHash(json);
68+
int get hashCode => cacheHashCode(() => deepHash(json));
6969
}
7070

7171
/// Package private (protected) methods for [Metadata].

‎pkgs/record_use/lib/src/recordings.dart‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,10 +331,12 @@ Error: $e
331331
}
332332

333333
@override
334-
int get hashCode => Object.hash(
335-
metadata.hashCode,
336-
deepHash(calls),
337-
deepHash(instances),
334+
int get hashCode => cacheHashCode(
335+
() => Object.hash(
336+
metadata.hashCode,
337+
deepHash(calls),
338+
deepHash(instances),
339+
),
338340
);
339341

340342
/// Compares this set of usages ('actual') with the [expected] set

‎pkgs/record_use/lib/src/reference.dart‎

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ sealed class Reference {
3131
}
3232

3333
@override
34-
int get hashCode => deepHash(loadingUnits);
34+
int get hashCode => cacheHashCode(() => deepHash(loadingUnits));
3535

3636
bool _semanticEqualsShared(
3737
Reference other, {
@@ -206,11 +206,13 @@ final class CallWithArguments extends CallReference {
206206
}
207207

208208
@override
209-
int get hashCode => Object.hash(
210-
deepHash(positionalArguments),
211-
deepHash(namedArguments),
212-
receiver,
213-
super.hashCode,
209+
int get hashCode => cacheHashCode(
210+
() => Object.hash(
211+
deepHash(positionalArguments),
212+
deepHash(namedArguments),
213+
receiver,
214+
super.hashCode,
215+
),
214216
);
215217

216218
@override
@@ -436,7 +438,8 @@ final class InstanceConstantReference extends InstanceReference {
436438
}
437439

438440
@override
439-
int get hashCode => Object.hash(instanceConstant, super.hashCode);
441+
int get hashCode =>
442+
cacheHashCode(() => Object.hash(instanceConstant, super.hashCode));
440443

441444
@override
442445
@visibleForTesting
@@ -514,10 +517,12 @@ final class InstanceCreationReference extends InstanceReference {
514517
}
515518

516519
@override
517-
int get hashCode => Object.hash(
518-
deepHash(positionalArguments),
519-
deepHash(namedArguments),
520-
super.hashCode,
520+
int get hashCode => cacheHashCode(
521+
() => Object.hash(
522+
deepHash(positionalArguments),
523+
deepHash(namedArguments),
524+
super.hashCode,
525+
),
521526
);
522527

523528
@override

0 commit comments

Comments
 (0)