Skip to content

Commit 2167869

Browse files
Merge pull request #5307 from dotty-staging/improve-compression-of-tasty-string
Improve compression of TastyString
2 parents f7b993a + 2f66995 commit 2167869

File tree

1 file changed

+13
-20
lines changed

1 file changed

+13
-20
lines changed

compiler/src/dotty/tools/dotc/core/tasty/TastyString.scala

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,27 @@ package dotty.tools.dotc.core.tasty
22

33
import scala.runtime.quoted.Unpickler.Pickled
44

5+
import java.io._
6+
import java.util.Base64
7+
import java.nio.charset.StandardCharsets.UTF_8
8+
59
/** Utils for String representation of TASTY */
610
object TastyString {
711

8-
// Conservative encoding, each byte is encoded in a char
9-
// TODO improve encoding compression
10-
private final val maxStringSize = 65535 / 2
12+
// Max size of a string literal in the bytecode
13+
private final val maxStringSize = 65535
1114

1215
/** Encode TASTY bytes into an Seq of String */
13-
def pickle(bytes: Array[Byte]): Pickled =
14-
bytes.sliding(maxStringSize, maxStringSize).map(bytesToString).toList
16+
def pickle(bytes: Array[Byte]): Pickled = {
17+
val str = new String(Base64.getEncoder().encode(bytes), UTF_8)
18+
str.sliding(maxStringSize, maxStringSize).toList
19+
}
1520

1621
/** Decode the TASTY String into TASTY bytes */
1722
def unpickle(strings: Pickled): Array[Byte] = {
18-
val bytes = new Array[Byte](strings.map(_.length).sum)
19-
var i = 0
20-
for (str <- strings; j <- str.indices) {
21-
bytes(i) = str.charAt(j).toByte
22-
i += 1
23-
}
24-
bytes
25-
}
26-
27-
/** Encode bytes into a String */
28-
private def bytesToString(bytes: Array[Byte]): String = {
29-
assert(bytes.length <= maxStringSize)
30-
val chars = new Array[Char](bytes.length)
31-
for (i <- bytes.indices) chars(i) = (bytes(i) & 0xff).toChar
32-
new String(chars)
23+
val string = new StringBuilder
24+
strings.foreach(string.append)
25+
Base64.getDecoder().decode(string.result().getBytes(UTF_8))
3326
}
3427

3528
}

0 commit comments

Comments
 (0)