Skip to content

Commit ae40761

Browse files
committed
Optimization: Don't result of addrOfTree in Some
Instead use NoAddr as a sentinel for None.
1 parent 73c0d3c commit ae40761

File tree

4 files changed

+21
-22
lines changed

4 files changed

+21
-22
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package dotty.tools.dotc.core.tasty
33
import dotty.tools.dotc.ast.tpd
44
import dotty.tools.dotc.core.Comments.{Comment, CommentsContext, ContextDocstrings}
55
import dotty.tools.dotc.core.Contexts.Context
6-
import dotty.tools.dotc.core.tasty.TastyBuffer.Addr
6+
import dotty.tools.dotc.core.tasty.TastyBuffer.{Addr, NoAddr}
77

88
import java.nio.charset.Charset
99

10-
class CommentPickler(pickler: TastyPickler, addrOfTree: tpd.Tree => Option[Addr])(implicit ctx: Context) {
10+
class CommentPickler(pickler: TastyPickler, addrOfTree: tpd.Tree => Addr)(implicit ctx: Context) {
1111
private[this] val buf = new TastyBuffer(5000)
1212
pickler.newSection("Comments", buf)
1313

@@ -16,8 +16,8 @@ class CommentPickler(pickler: TastyPickler, addrOfTree: tpd.Tree => Option[Addr]
1616
new Traverser(ctx.docCtx.get).traverse(root)
1717
}
1818

19-
def pickleComment(addrOfTree: Option[Addr], comment: Option[Comment]): Unit = (addrOfTree, comment) match {
20-
case (Some(addr), Some(cmt)) =>
19+
def pickleComment(addr: Addr, comment: Option[Comment]): Unit = comment match {
20+
case Some(cmt) if addr != NoAddr =>
2121
val bytes = cmt.raw.getBytes(Charset.forName("UTF-8"))
2222
val length = bytes.length
2323
buf.writeAddr(addr)

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

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import TastyBuffer._
1414
import util.Spans._
1515
import TastyFormat.SOURCE
1616

17-
class PositionPickler(pickler: TastyPickler, addrOfTree: untpd.Tree => Option[Addr]) {
17+
class PositionPickler(pickler: TastyPickler, addrOfTree: untpd.Tree => Addr) {
1818
val buf: TastyBuffer = new TastyBuffer(5000)
1919
pickler.newSection("Positions", buf)
2020
import ast.tpd._
@@ -72,19 +72,18 @@ class PositionPickler(pickler: TastyPickler, addrOfTree: untpd.Tree => Option[Ad
7272
def traverse(x: Any, current: SourceFile): Unit = x match {
7373
case x: untpd.Tree =>
7474
if (x.span.exists) {
75-
val sourceChange = x.source != current
76-
val needsPos = x.span.toSynthetic != x.envelope(x.source) || alwaysNeedsPos(x)
77-
addrOfTree(x) match {
78-
case Some(addr)
79-
if needsPos && !pickledIndices.contains(addr.index) || sourceChange =>
80-
// we currently do not share trees when unpickling, so if one path to a tree contains
81-
// a source change while another does not, we have to record the position of the tree twice
82-
// in order not to miss the source change. Test case is t3232a.scala.
83-
//println(i"pickling $x with $span at $addr")
75+
val addr = addrOfTree(x)
76+
if (addr != NoAddr) {
77+
if (x.source != current) {
78+
// we currently do not share trees when unpickling, so if one path to a tree contains
79+
// a source change while another does not, we have to record the position of the tree twice
80+
// in order not to miss the source change. Test case is t3232a.scala.
81+
pickleDeltas(addr.index, x.span)
82+
pickleSource(x.source)
83+
}
84+
else if (!pickledIndices.contains(addr.index) &&
85+
(x.span.toSynthetic != x.envelope(x.source) || alwaysNeedsPos(x)))
8486
pickleDeltas(addr.index, x.span)
85-
if (sourceChange) pickleSource(x.source)
86-
case _ =>
87-
//println(i"no address for $x")
8887
}
8988
}
9089
x match {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class TastyPickler(val rootCls: ClassSymbol) {
6767
* Note that trees are looked up by reference equality,
6868
* so one can reliably use this function only directly after `pickler`.
6969
*/
70-
var addrOfTree: tpd.Tree => Option[Addr] = (_ => None)
70+
var addrOfTree: tpd.Tree => Addr = (_ => NoAddr)
7171

7272
/**
7373
* Addresses in TASTY file of symbols, stored by pickling.

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package core
44
package tasty
55

66
import util.Util.{bestFit, dble}
7-
import TastyBuffer.{Addr, AddrWidth}
7+
import TastyBuffer.{Addr, NoAddr, AddrWidth}
88
import config.Printers.pickling
99
import ast.untpd.Tree
1010

@@ -25,9 +25,9 @@ class TreeBuffer extends TastyBuffer(50000) {
2525
case addr: Addr => addr
2626
}
2727

28-
def addrOfTree(tree: Tree): Option[Addr] = treeAddrs.get(tree) match {
29-
case null => None
30-
case addr: Addr => Some(addr)
28+
def addrOfTree(tree: Tree): Addr = treeAddrs.get(tree) match {
29+
case null => NoAddr
30+
case addr: Addr => addr
3131
}
3232

3333
private def offset(i: Int): Addr = Addr(offsets(i))

0 commit comments

Comments
 (0)