mirror of
https://github.com/dart-lang/sdk
synced 2024-09-05 16:41:07 +00:00
![Leaf Petersen](/assets/img/avatar_default.png)
Change-Id: Ib33169c3e0ffc870915c189404074a1dea472546 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/196548 Reviewed-by: Bob Nystrom <rnystrom@google.com> Commit-Queue: Leaf Petersen <leafp@google.com>
69 lines
1.2 KiB
Dart
69 lines
1.2 KiB
Dart
// Copyright (c) 2018, 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.
|
|
|
|
// @dart = 2.9
|
|
|
|
import "package:expect/expect.dart";
|
|
|
|
// Class hierarchy on an abstract class
|
|
// that defines a "next" structure.
|
|
|
|
abstract class A {
|
|
A next;
|
|
}
|
|
|
|
class B extends A {
|
|
B(A n) {
|
|
this.next = n;
|
|
}
|
|
}
|
|
|
|
// Method that counts length of list.
|
|
// With only Bs, the getter can be
|
|
// inlined without check class.
|
|
int countMe(A i) {
|
|
int x = 0;
|
|
while (i != null) {
|
|
A next = i.next;
|
|
x++;
|
|
i = next;
|
|
}
|
|
return x;
|
|
}
|
|
|
|
int doitHot(A a) {
|
|
// Warm up the JIT.
|
|
int d = 0;
|
|
for (int i = 0; i < 1000; i++) {
|
|
d += countMe(a);
|
|
}
|
|
return d;
|
|
}
|
|
|
|
// Nasty class that overrides the getter.
|
|
class C extends A {
|
|
C(A n) {
|
|
this.next = n;
|
|
}
|
|
// New override.
|
|
A get next => null;
|
|
}
|
|
|
|
int bringInC(A a) {
|
|
// Introduce C to compiler.
|
|
a = new C(a);
|
|
return doitHot(a);
|
|
}
|
|
|
|
main() {
|
|
// Make a list with just Bs.
|
|
A a = null;
|
|
for (int i = 0; i < 1000; i++) {
|
|
a = new B(a);
|
|
}
|
|
|
|
Expect.equals(1000 * 1000, doitHot(a));
|
|
Expect.equals(1000, bringInC(a));
|
|
}
|