Skip to content

Commit 5ec1c0e

Browse files
committed
_HashableList: don't use UnodifiableListView
Adds unnecessary overhead in a perf-critical case Also cache the hashCode
1 parent 10483f5 commit 5ec1c0e

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

lib/src/model_utils.dart

Lines changed: 10 additions & 8 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.

0 commit comments

Comments
 (0)