Skip to content

Commit d74496e

Browse files
committed
Reuse the StringBuilder in TastyPrinter
1 parent 5add9b7 commit d74496e

File tree

1 file changed

+21
-52
lines changed

1 file changed

+21
-52
lines changed

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

Lines changed: 21 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ object TastyPrinter:
6464

6565
class TastyPrinter(bytes: Array[Byte]) {
6666

67-
private val sb: StringBuilder = new StringBuilder
68-
6967
class TastyPrinterUnpickler extends TastyUnpickler(bytes) {
7068
var namesStart: Addr = uninitialized
7169
var namesEnd: Addr = uninitialized
@@ -83,49 +81,35 @@ class TastyPrinter(bytes: Array[Byte]) {
8381

8482
private def nameRefToString(ref: NameRef): String = nameToString(nameAtRef(ref))
8583

86-
private def printHeader(): Unit =
84+
private def printHeader(sb: StringBuilder): Unit =
8785
val header = unpickler.header
8886
sb.append("Header:\n")
8987
sb.append(s" version: ${header.majorVersion}.${header.minorVersion}.${header.experimentalVersion}\n")
9088
sb.append(" tooling: ").append(header.toolingVersion).append("\n")
9189
sb.append(" UUID: ").append(header.uuid).append("\n")
9290
sb.append("\n")
9391

94-
private def printNames(): Unit =
92+
private def printNames(sb: StringBuilder): Unit =
9593
sb.append(s"Names (${unpickler.namesEnd.index - unpickler.namesStart.index} bytes, starting from ${unpickler.namesStart.index}):\n")
9694
for ((name, idx) <- nameAtRef.contents.zipWithIndex) {
9795
val index = nameStr("%6d".format(idx))
9896
sb.append(index).append(": ").append(nameToString(name)).append("\n")
9997
}
10098

10199
def showContents(): String = {
102-
printHeader()
103-
printNames()
104-
unpickle(new TreeSectionUnpickler) match {
105-
case Some(s) => sb.append("\n\n").append(s)
106-
case _ =>
107-
}
108-
unpickle(new PositionSectionUnpickler) match {
109-
case Some(s) => sb.append("\n\n").append(s)
110-
case _ =>
111-
}
112-
unpickle(new CommentSectionUnpickler) match {
113-
case Some(s) => sb.append("\n\n").append(s)
114-
case _ =>
115-
}
116-
unpickle(new AttributesSectionUnpickler) match {
117-
case Some(s) => sb.append("\n\n").append(s)
118-
case _ =>
119-
}
100+
val sb: StringBuilder = new StringBuilder
101+
printHeader(sb)
102+
printNames(sb)
103+
unpickle(new TreeSectionUnpickler(sb))
104+
unpickle(new PositionSectionUnpickler(sb))
105+
unpickle(new CommentSectionUnpickler(sb))
106+
unpickle(new AttributesSectionUnpickler(sb))
120107
sb.result
121108
}
122109

123-
class TreeSectionUnpickler extends SectionUnpickler[String](ASTsSection) {
110+
class TreeSectionUnpickler(sb: StringBuilder) extends SectionUnpickler[Unit](ASTsSection) {
124111
import dotty.tools.tasty.TastyFormat.*
125-
126-
private val sb: StringBuilder = new StringBuilder
127-
128-
def unpickle(reader: TastyReader, tastyName: NameTable): String = {
112+
def unpickle(reader: TastyReader, tastyName: NameTable): Unit = {
129113
import reader.*
130114
var indent = 0
131115
def newLine() = {
@@ -186,23 +170,19 @@ class TastyPrinter(bytes: Array[Byte]) {
186170
}
187171
indent -= 2
188172
}
189-
sb.append(s"Trees (${endAddr.index - startAddr.index} bytes, starting from $base):")
173+
sb.append(s"\n\nTrees (${endAddr.index - startAddr.index} bytes, starting from $base):")
190174
while (!isAtEnd) {
191175
printTree()
192176
newLine()
193177
}
194-
sb.result
195178
}
196179
}
197180

198-
class PositionSectionUnpickler extends SectionUnpickler[String](PositionsSection) {
199-
200-
private val sb: StringBuilder = new StringBuilder
201-
202-
def unpickle(reader: TastyReader, tastyName: NameTable): String = {
181+
class PositionSectionUnpickler(sb: StringBuilder) extends SectionUnpickler[Unit](PositionsSection) {
182+
def unpickle(reader: TastyReader, tastyName: NameTable): Unit = {
203183
import reader.*
204184
val posUnpickler = new PositionUnpickler(reader, tastyName)
205-
sb.append(s"Positions (${reader.endAddr.index - reader.startAddr.index} bytes, starting from $base):\n")
185+
sb.append(s"\n\nPositions (${reader.endAddr.index - reader.startAddr.index} bytes, starting from $base):\n")
206186
val lineSizes = posUnpickler.lineSizes
207187
sb.append(s" lines: ${lineSizes.length}\n")
208188
sb.append(s" line sizes:\n")
@@ -226,43 +206,32 @@ class TastyPrinter(bytes: Array[Byte]) {
226206
sb.append(nameStr(s"${nameRef.index} [${tastyName(nameRef)}]"))
227207
sb.append("\n")
228208
}
229-
230-
sb.result
231209
}
232210
}
233211

234-
class CommentSectionUnpickler extends SectionUnpickler[String](CommentsSection) {
235-
236-
private val sb: StringBuilder = new StringBuilder
237-
238-
def unpickle(reader: TastyReader, tastyName: NameTable): String = {
212+
class CommentSectionUnpickler(sb: StringBuilder) extends SectionUnpickler[Unit](CommentsSection) {
213+
def unpickle(reader: TastyReader, tastyName: NameTable): Unit = {
239214
import reader.*
240215
val comments = new CommentUnpickler(reader).comments
241216
if !comments.isEmpty then
242-
sb.append(s"Comments (${reader.endAddr.index - reader.startAddr.index} bytes, starting from $base):\n")
217+
sb.append(s"\n\nComments (${reader.endAddr.index - reader.startAddr.index} bytes, starting from $base):\n")
243218
val sorted = comments.toSeq.sortBy(_._1.index)
244219
for ((addr, cmt) <- sorted) {
245220
sb.append(treeStr("%6d".format(addr.index)))
246221
sb.append(s": ${cmt.raw} (expanded = ${cmt.isExpanded})\n")
247222
}
248-
sb.result
249223
}
250224
}
251225

252-
class AttributesSectionUnpickler extends SectionUnpickler[String](AttributesSection) {
226+
class AttributesSectionUnpickler(sb: StringBuilder) extends SectionUnpickler[Unit](AttributesSection) {
253227
import dotty.tools.tasty.TastyFormat.*
254-
255-
private val sb: StringBuilder = new StringBuilder
256-
257-
def unpickle(reader: TastyReader, tastyName: NameTable): String = {
228+
def unpickle(reader: TastyReader, tastyName: NameTable): Unit = {
258229
import reader.*
259230
val attributes = new AttributeUnpickler(reader).attributes
260-
sb.append(s"Attributes (${reader.endAddr.index - reader.startAddr.index} bytes, starting from $base):\n")
231+
sb.append(s"\n\nAttributes (${reader.endAddr.index - reader.startAddr.index} bytes, starting from $base):\n")
261232

262233
for tag <- attributes.booleanTags do
263234
sb.append(" ").append(attributeTagToString(tag)).append("\n")
264-
265-
sb.result
266235
}
267236
}
268237

0 commit comments

Comments
 (0)