Skip to content

Commit c58f4dd

Browse files
kevmoojcollins-g
authored andcommitted
Use explicit keys for often accessed closures (#1591)
* Use explicit keys for often accessed closures * _HashableList: don't use UnodifiableListView Adds unnecessary overhead in a perf-critical case Also cache the hashCode
1 parent 1358c8f commit c58f4dd

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

lib/src/model.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1560,8 +1560,8 @@ abstract class GetterSetterCombo implements ModelElement {
15601560
if (targetClass.name == target.name) {
15611561
return original.replaceAll(constructorName, "${target.linkedName}");
15621562
}
1563-
return original.replaceAll(
1564-
"${targetClass.name}.${target.name}", "${targetClass.linkedName}.${target.linkedName}");
1563+
return original.replaceAll("${targetClass.name}.${target.name}",
1564+
"${targetClass.linkedName}.${target.linkedName}");
15651565
}
15661566

15671567
String _constantValueBase() {
@@ -2892,7 +2892,8 @@ abstract class ModelElement extends Canonicalization
28922892
///
28932893
/// For example: libraryName.className.methodName
28942894
@override
2895-
String get fullyQualifiedName => _memoizer.memoized(_buildFullyQualifiedName);
2895+
String get fullyQualifiedName => _memoizer.memoized(_buildFullyQualifiedName,
2896+
altKey: 'fullyQualifiedName');
28962897

28972898
String _fullyQualifiedNameWithoutLibrary() {
28982899
// Remember, periods are legal in library names.
@@ -2904,7 +2905,8 @@ abstract class ModelElement extends Canonicalization
29042905
// library.fullyQualifiedName seems somehow to break things.
29052906
//String get fullyQualifiedNameWithoutLibrary => _memoizer.memoized2(fullyQualifiedName.replaceFirst, "${library.fullyQualifiedName}.", '');
29062907
String get fullyQualifiedNameWithoutLibrary =>
2907-
_memoizer.memoized(_fullyQualifiedNameWithoutLibrary);
2908+
_memoizer.memoized(_fullyQualifiedNameWithoutLibrary,
2909+
altKey: 'fullyQualifiedNameWithoutLibrary');
29082910

29092911
String get sourceFileName => element.source.fullName;
29102912

lib/src/model_utils.dart

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -172,15 +172,17 @@ String crossdartifySource(
172172

173173
/// An UnmodifiableListView that computes equality and hashCode based on the
174174
/// equality and hashCode of its contained objects.
175-
class _HashableList extends UnmodifiableListView<dynamic> {
176-
_HashableList(Iterable<dynamic> iterable) : super(iterable);
175+
class _HashableList {
176+
final List _source;
177+
int _hashCache;
178+
_HashableList(this._source);
177179

178180
@override
179181
bool operator ==(other) {
180182
if (other is _HashableList) {
181-
if (this.length == other.length) {
182-
for (var index = 0; index < length; ++index) {
183-
if (this[index] != other[index]) return false;
183+
if (_source.length == other._source.length) {
184+
for (var index = 0; index < _source.length; ++index) {
185+
if (_source[index] != other._source[index]) return false;
184186
}
185187
return true;
186188
}
@@ -189,7 +191,7 @@ class _HashableList extends UnmodifiableListView<dynamic> {
189191
}
190192

191193
@override
192-
get hashCode => hashObjects(this);
194+
get hashCode => _hashCache ??= hashObjects(_source);
193195
}
194196

195197
/// Like [Memoizer], except in checked mode will validate that the value of the
@@ -252,11 +254,11 @@ class ValidatingMemoizer extends Memoizer {
252254
/// ```
253255
class Memoizer {
254256
/// Map of a function and its positional parameters (if any), to a value.
255-
Map<_HashableList, dynamic> _memoizationTable = new Map();
257+
final Map<_HashableList, dynamic> _memoizationTable = new HashMap();
256258

257259
/// Reset the memoization table, forcing calls of the underlying functions.
258260
void invalidateMemos() {
259-
_memoizationTable = new Map();
261+
_memoizationTable.clear();
260262
}
261263

262264
/// A wrapper around putIfAbsent, exposed to allow overrides.
@@ -266,8 +268,9 @@ class Memoizer {
266268

267269
/// Calls and caches the return value of [f]() if not in the cache, then
268270
/// returns the cached value of [f]().
269-
R memoized<R>(Function f) {
270-
_HashableList key = new _HashableList([f]);
271+
R memoized<R>(Function f, {String altKey}) {
272+
Object obj = altKey ?? f;
273+
_HashableList key = new _HashableList([obj]);
271274
return _cacheIfAbsent(key, f);
272275
}
273276

0 commit comments

Comments
 (0)