mirror of
https://github.com/dart-lang/sdk
synced 2024-09-16 04:27:17 +00:00
73b7d0c40a
This is a reland of commite2d6cb69fb
Original change's description: > Reland "[vm/compiler] Optimize switch statements" > > This is a reland of commitf5228127f8
> > Original change's description: > > [vm/compiler] Optimize switch statements > > > > Switch statements that either contain only integers or only enum values of the same type can be optimized. > > > > Depending on the number of switch expressions and the number of holes that the range of switch expressions contains, either a binary search or a jump table is used. > > > > TEST=runtime/test/vm/dart{,_2}/optimized_switch > > TEST=tests/language{,_2}/switch > > > > Fixes: https://github.com/dart-lang/sdk/issues/49585 > > > > Co-authored-by: Gabriel Terwesten gabriel@terwesten.net > > > > Change-Id: I62dcdb7843107f03de7e468c60b4db52ec78f676 > > Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/253787 > > Reviewed-by: Alexander Markov <alexmarkov@google.com> > > Commit-Queue: Alexander Markov <alexmarkov@google.com> > > TEST=runtime/test/vm/dart{,_2}/optimized_switch > TEST=tests/language{,_2}/switch > > Change-Id: Ie96e5ff41dce33e2425ba9ad15ecd931d09bec6e > Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/255700 > Reviewed-by: Alexander Markov <alexmarkov@google.com> > Commit-Queue: Alexander Markov <alexmarkov@google.com> TEST=runtime/test/vm/dart{,_2}/optimized_switch TEST=tests/language{,_2}/switch Change-Id: I56f8d25a88bb856115550c5da3f02c80c7547802 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/256180 Reviewed-by: Alexander Markov <alexmarkov@google.com> Commit-Queue: Alexander Markov <alexmarkov@google.com>
110 lines
2.3 KiB
Dart
110 lines
2.3 KiB
Dart
// Copyright (c) 2011, 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.
|
|
|
|
// Test switch statement using labels.
|
|
|
|
// VMOptions=
|
|
// VMOptions=--force-switch-dispatch-type=0
|
|
// VMOptions=--force-switch-dispatch-type=1
|
|
// VMOptions=--force-switch-dispatch-type=2
|
|
|
|
import "package:expect/expect.dart";
|
|
|
|
class Switcher {
|
|
Switcher() {}
|
|
|
|
say1(sound) {
|
|
var x = 0;
|
|
switch (sound) {
|
|
MOO:
|
|
case "moo":
|
|
x = 100;
|
|
break;
|
|
case "woof":
|
|
x = 200;
|
|
continue MOO;
|
|
default:
|
|
x = 300;
|
|
break;
|
|
}
|
|
return x;
|
|
}
|
|
|
|
say2(sound) {
|
|
var x = 0;
|
|
switch (sound) {
|
|
WOOF:
|
|
case "woof":
|
|
x = 200;
|
|
break;
|
|
case "moo":
|
|
x = 100;
|
|
continue WOOF;
|
|
default:
|
|
x = 300;
|
|
break;
|
|
}
|
|
return x;
|
|
}
|
|
|
|
// forward label to outer switch
|
|
say3(animal, sound) {
|
|
var x = 0;
|
|
switch (animal) {
|
|
case "cow":
|
|
switch (sound) {
|
|
case "moo":
|
|
x = 100;
|
|
break;
|
|
case "muh":
|
|
x = 200;
|
|
break;
|
|
default:
|
|
continue NIX_UNDERSTAND;
|
|
}
|
|
break;
|
|
case "dog":
|
|
if (sound == "woof") {
|
|
x = 300;
|
|
} else {
|
|
continue NIX_UNDERSTAND;
|
|
}
|
|
break;
|
|
NIX_UNDERSTAND:
|
|
case "unicorn":
|
|
x = 400;
|
|
break;
|
|
default:
|
|
x = 500;
|
|
break;
|
|
}
|
|
return x;
|
|
}
|
|
}
|
|
|
|
class SwitchLabelTest {
|
|
static testMain() {
|
|
Switcher s = new Switcher();
|
|
Expect.equals(100, s.say1("moo"));
|
|
Expect.equals(100, s.say1("woof"));
|
|
Expect.equals(300, s.say1("cockadoodledoo"));
|
|
|
|
Expect.equals(200, s.say2("moo"));
|
|
Expect.equals(200, s.say2("woof"));
|
|
Expect.equals(300, s.say2("")); // Dead unicorn says nothing.
|
|
|
|
Expect.equals(100, s.say3("cow", "moo"));
|
|
Expect.equals(200, s.say3("cow", "muh"));
|
|
Expect.equals(400, s.say3("cow", "boeh")); // Don't ask.
|
|
Expect.equals(300, s.say3("dog", "woof"));
|
|
Expect.equals(400, s.say3("dog", "boj")); // Ĉu vi parolas Esperanton?
|
|
Expect.equals(400, s.say3("unicorn", "")); // Still dead.
|
|
Expect.equals(500, s.say3("angry bird", "whoooo"));
|
|
}
|
|
}
|
|
|
|
main() {
|
|
SwitchLabelTest.testMain();
|
|
}
|