Skip to content

Improve compression of TastyString #5307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions compiler/src/dotty/tools/dotc/core/tasty/TastyString.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,34 @@ package dotty.tools.dotc.core.tasty

import scala.runtime.quoted.Unpickler.Pickled

import java.io._
import java.util.Base64
import java.nio.charset.StandardCharsets.UTF_8

/** Utils for String representation of TASTY */
object TastyString {

// Conservative encoding, each byte is encoded in a char
// TODO improve encoding compression
private final val maxStringSize = 65535 / 2
// Max size of a string literal in the bytecode
private final val maxStringSize = 65535

/** Encode TASTY bytes into an Seq of String */
def pickle(bytes: Array[Byte]): Pickled =
bytes.sliding(maxStringSize, maxStringSize).map(bytesToString).toList

/** Decode the TASTY String into TASTY bytes */
def unpickle(strings: Pickled): Array[Byte] = {
val bytes = new Array[Byte](strings.map(_.length).sum)
var i = 0
for (str <- strings; j <- str.indices) {
bytes(i) = str.charAt(j).toByte
i += 1
def pickle(bytes: Array[Byte]): Pickled = {
val str = new String(Base64.getEncoder().encode(bytes), UTF_8)
def split(sliceEnd: Int, acc: List[String]): List[String] = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a reimplementation of sliding? If so why not str.sliding(maxStringSize, maxStringSize).toList?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This version is more efficient.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My personal opinion is to go with the simplest solution unless it is performance sensitive and it happen to be a hotspot. No strong feelings though

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will change it to the sliding. We can always change it later if needed.

if (sliceEnd == 0) acc
else {
val sliceStart = (sliceEnd - maxStringSize) max 0
split(sliceStart, str.substring(sliceStart, sliceEnd) :: acc)
}
}
bytes
split(str.length, Nil)
}

/** Encode bytes into a String */
private def bytesToString(bytes: Array[Byte]): String = {
assert(bytes.length <= maxStringSize)
val chars = new Array[Char](bytes.length)
for (i <- bytes.indices) chars(i) = (bytes(i) & 0xff).toChar
new String(chars)
/** Decode the TASTY String into TASTY bytes */
def unpickle(strings: Pickled): Array[Byte] = {
val string = new StringBuilder
strings.foreach(string.append)
Base64.getDecoder().decode(string.result().getBytes(UTF_8))
}

}