Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use
StringConcatFactory
for string concatenation on JDK 9+
JEP 280, released in JDK 9, proposes a new way to compile string concatenation using `invokedynamic` and `StringConcatFactory`. This new approach generates less bytecode, doesn't have to incur the overhead of `StringBuilder` allocations, and allows users to pick swap the concatenation technique at runtime. This changes the codegen when the target is at least Java 9 to leverage `invokedynamic` and `StringConcatFactory`. On Java 8, the old `StringBuilder` approach is still used.
- Loading branch information
1 parent
ca493d8
commit b7dc31f
Showing
4 changed files
with
209 additions
and
30 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
Binary file not shown.
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,86 @@ | ||
// java: -Xss128M | ||
|
||
import scala.tools.partest.ReplTest | ||
|
||
// ReplTest so that the long concatenation is compiled at test-run-time with the larger `Xss`. | ||
// Tests are always compiled in the partest VM. | ||
object Test extends ReplTest { | ||
def code = | ||
"""// This should generally obey 15.18.1. of the JLS (String Concatenation Operator +) | ||
|def concatenatingVariousTypes(): String = { | ||
| val str: String = "some string" | ||
| val sb: StringBuffer = new StringBuffer("some stringbuffer") | ||
| val cs: CharSequence = java.nio.CharBuffer.allocate(50).append("charsequence") | ||
| val i: Int = 123456789 | ||
| val s: Short = 345 | ||
| val b: Byte = 12 | ||
| val z: Boolean = true | ||
| val f: Float = 3.14f | ||
| val j: Long = 98762147483647L | ||
| val d: Double = 3.1415d | ||
| | ||
| "String " + str + "\n" + | ||
| "StringBuffer " + sb + "\n" + | ||
| "CharSequence " + cs + "\n" + | ||
| "Int " + i + "\n" + | ||
| "Short " + s + "\n" + | ||
| "Byte " + b + "\n" + | ||
| "Boolean " + z + "\n" + | ||
| "Float " + f + "\n" + | ||
| "Long " + j + "\n" + | ||
| "Double " + d + "\n" | ||
|} | ||
|// The characters `\u0001` and `\u0002` play a special role in `StringConcatFactory` | ||
|def concatenationInvolvingSpecialCharacters(): String = { | ||
| val s1 = "Qux" | ||
| val s2 = "Quux" | ||
| | ||
| s"Foo \u0001 $s1 Bar \u0002 $s2 Baz" | ||
|} | ||
|// Concatenation involving more than 200 elements | ||
|def largeConcatenation(): String = { | ||
| val s00 = "s00" | ||
| val s01 = "s01" | ||
| val s02 = "s02" | ||
| val s03 = "s03" | ||
| val s04 = "s04" | ||
| val s05 = "s05" | ||
| val s06 = "s06" | ||
| val s07 = "s07" | ||
| val s08 = "s08" | ||
| | ||
| // 24 rows follow | ||
| s00 + "," + s01 + "," + s02 + "," + s03 + "," + s04 + "," + s05 + "," + s06 + "," + s07 + "," + s08 + "\n" + | ||
| s00 + "," + s01 + "," + s02 + "," + s03 + "," + s04 + "," + s05 + "," + s06 + "," + s07 + "," + s08 + "\n" + | ||
| s00 + "," + s01 + "," + s02 + "," + s03 + "," + s04 + "," + s05 + "," + s06 + "," + s07 + "," + s08 + "\n" + | ||
| s00 + "," + s01 + "," + s02 + "," + s03 + "," + s04 + "," + s05 + "," + s06 + "," + s07 + "," + s08 + "\n" + | ||
| s00 + "," + s01 + "," + s02 + "," + s03 + "," + s04 + "," + s05 + "," + s06 + "," + s07 + "," + s08 + "\n" + | ||
| s00 + "," + s01 + "," + s02 + "," + s03 + "," + s04 + "," + s05 + "," + s06 + "," + s07 + "," + s08 + "\n" + | ||
| s00 + "," + s01 + "," + s02 + "," + s03 + "," + s04 + "," + s05 + "," + s06 + "," + s07 + "," + s08 + "\n" + | ||
| s00 + "," + s01 + "," + s02 + "," + s03 + "," + s04 + "," + s05 + "," + s06 + "," + s07 + "," + s08 + "\n" + | ||
| s00 + "," + s01 + "," + s02 + "," + s03 + "," + s04 + "," + s05 + "," + s06 + "," + s07 + "," + s08 + "\n" + | ||
| s00 + "," + s01 + "," + s02 + "," + s03 + "," + s04 + "," + s05 + "," + s06 + "," + s07 + "," + s08 + "\n" + | ||
| s00 + "," + s01 + "," + s02 + "," + s03 + "," + s04 + "," + s05 + "," + s06 + "," + s07 + "," + s08 + "\n" + | ||
| s00 + "," + s01 + "," + s02 + "," + s03 + "," + s04 + "," + s05 + "," + s06 + "," + s07 + "," + s08 + "\n" + | ||
| s00 + "," + s01 + "," + s02 + "," + s03 + "," + s04 + "," + s05 + "," + s06 + "," + s07 + "," + s08 + "\n" + | ||
| s00 + "," + s01 + "," + s02 + "," + s03 + "," + s04 + "," + s05 + "," + s06 + "," + s07 + "," + s08 + "\n" + | ||
| s00 + "," + s01 + "," + s02 + "," + s03 + "," + s04 + "," + s05 + "," + s06 + "," + s07 + "," + s08 + "\n" + | ||
| s00 + "," + s01 + "," + s02 + "," + s03 + "," + s04 + "," + s05 + "," + s06 + "," + s07 + "," + s08 + "\n" + | ||
| s00 + "," + s01 + "," + s02 + "," + s03 + "," + s04 + "," + s05 + "," + s06 + "," + s07 + "," + s08 + "\n" + | ||
| s00 + "," + s01 + "," + s02 + "," + s03 + "," + s04 + "," + s05 + "," + s06 + "," + s07 + "," + s08 + "\n" + | ||
| s00 + "," + s01 + "," + s02 + "," + s03 + "," + s04 + "," + s05 + "," + s06 + "," + s07 + "," + s08 + "\n" + | ||
| s00 + "," + s01 + "," + s02 + "," + s03 + "," + s04 + "," + s05 + "," + s06 + "," + s07 + "," + s08 + "\n" + | ||
| s00 + "," + s01 + "," + s02 + "," + s03 + "," + s04 + "," + s05 + "," + s06 + "," + s07 + "," + s08 + "\n" + | ||
| s00 + "," + s01 + "," + s02 + "," + s03 + "," + s04 + "," + s05 + "," + s06 + "," + s07 + "," + s08 + "\n" + | ||
| s00 + "," + s01 + "," + s02 + "," + s03 + "," + s04 + "," + s05 + "," + s06 + "," + s07 + "," + s08 + "\n" + | ||
| s00 + "," + s01 + "," + s02 + "," + s03 + "," + s04 + "," + s05 + "," + s06 + "," + s07 + "," + s08 + "\n" | ||
|} | ||
|println("----------") | ||
|println(concatenatingVariousTypes()) | ||
|println("----------") | ||
|println(concatenationInvolvingSpecialCharacters()) | ||
|println("----------") | ||
|println(largeConcatenation()) | ||
|println("----------") | ||
|""".stripMargin | ||
} |