203955bc48
This CL includes the following fixes: * Fix for incorrect non-nullable assumption about _Closure._hash field. * Add error handling into BecomeMapTraits::Hash. * Correct assertions for validating layout of Closure objects. * Add identityHashCode to the list of VM entry points in precompiler. Closes #30211. Original code review: https://codereview.chromium.org/2983823002/ Original CL description: This performance improvement is inspired by Flutter listeners stored in the HashSet (see ObserverList) and frequently checked using HashSet.contains(). If there are many such listeners and they are implicit instance closures (for example, created by 'new Listenable.merge(...)'), HashSet.contains() becomes very slow. It spends a lot of time in Closure_equals native method due to hash collisions between closure objects with same function but different receivers. This CL improves hashCode() calculation for implicit instance closures by mixing function hashcode with identity hashcode of the receiver. For explicit closures and static implicit closures hashCode() is improved by using identityHashCode() of a closure object. Also, hashcode is calculated once and cached in each closure instance. The size of a closure instance doesn't grow up because there was unused word-size padding both on 32-bit and 64-bit architectures. The execution time of the following micro-benchmark is reduced from 47665ms to 135ms on my Linux/x64 box. ------------------------------------- import "dart:collection"; class Foo { int _a; Foo(this._a); void bar() {} } main() { HashSet hs = new HashSet(); for (int i = 0; i < 1000; ++i) { hs.add(new Foo(i).bar); } var watch = new Stopwatch()..start(); for (int i = 0; i < 1000; ++i) { for (var c in hs) { hs.contains(c); } } int time = watch.elapsedMilliseconds; print("Time: ${time}ms\n"); } ------------------------------------- R=zra@google.com Review-Url: https://codereview.chromium.org/2988493002 .
43 lines
1.6 KiB
Dart
43 lines
1.6 KiB
Dart
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
class _Closure implements Function {
|
|
bool operator ==(other) native "Closure_equals";
|
|
|
|
int get hashCode {
|
|
if (_hash == null) {
|
|
_hash = _computeHash();
|
|
}
|
|
return _hash;
|
|
}
|
|
|
|
_Closure get call => this;
|
|
|
|
_Closure _clone() native "Closure_clone";
|
|
|
|
int _computeHash() native "Closure_computeHash";
|
|
|
|
// No instance fields should be declared before the following 4 fields whose
|
|
// offsets must be identical in Dart and C++.
|
|
|
|
// The following fields are declared both in raw_object.h (for direct access
|
|
// from C++ code) and also here so that the offset-to-field map used by
|
|
// deferred objects is properly initialized.
|
|
// Caution: These fields are not Dart instances, but VM objects. Their Dart
|
|
// names do not need to match the C++ names, but they must be private.
|
|
var _instantiator_type_arguments;
|
|
var _function_type_arguments;
|
|
var _function;
|
|
var _context;
|
|
|
|
// Note: _Closure objects are created by VM "magically", without invoking
|
|
// constructor. So, _Closure default constructor is never compiled and
|
|
// detection of default-initialized fields is not performed.
|
|
// As a consequence, VM incorrectly assumes that _hash field is not
|
|
// nullable and may incorrectly remove 'if (_hash == null)' in get:hashCode.
|
|
// This initializer makes _hash field nullable even without constructor
|
|
// compilation.
|
|
var _hash = null;
|
|
}
|