forked from scala/scala3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Emit efficient code for switch over strings
The pattern matcher will now emit `Match` with `String` scrutinee as well as the existing `Int` scrutinee. The JVM backend handles this case by emitting bytecode that switches on the String's `hashCode` (this matches what Java does). The SJS already handles `String` matches. The approach is similar to scala/scala#8451 (see scala/bug#11740 too), except that instead of doing a transformation on the AST, we just emit the right bytecode straight away. This is desirable since it means that Scala.js (and any other backend) can choose their own optimised strategy for compiling a match on strings.
- Loading branch information
1 parent
cca5f8f
commit 0214cd1
Showing
6 changed files
with
295 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
2 | ||
-1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import annotation.switch | ||
|
||
object Test { | ||
def test(s: String): Int = { | ||
(s : @switch) match { | ||
case "1" => 0 | ||
case null => -1 | ||
case _ => s.toInt | ||
} | ||
} | ||
|
||
def main(args: Array[String]): Unit = { | ||
println(test("2")) | ||
println(test(null)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
fido Success(dog) | ||
garfield Success(cat) | ||
wanda Success(fish) | ||
henry Success(horse) | ||
felix Failure(scala.MatchError: felix (of class java.lang.String)) | ||
deuteronomy Success(cat) | ||
===== | ||
AaAa 2031744 Success(1) | ||
BBBB 2031744 Success(2) | ||
BBAa 2031744 Failure(scala.MatchError: BBAa (of class java.lang.String)) | ||
cCCc 3015872 Success(3) | ||
ddDd 3077408 Success(4) | ||
EEee 2125120 Failure(scala.MatchError: EEee (of class java.lang.String)) | ||
===== | ||
A Success(()) | ||
X Failure(scala.MatchError: X (of class java.lang.String)) | ||
===== | ||
Success(3) | ||
null Success(2) | ||
7 Failure(scala.MatchError: 7 (of class java.lang.String)) | ||
===== | ||
pig Success(1) | ||
dog Success(2) | ||
===== | ||
Ea 2236 Success(1) | ||
FB 2236 Success(2) | ||
cC 3136 Success(3) | ||
xx 3840 Success(4) | ||
null 0 Success(4) |
Oops, something went wrong.