Skip to content

Commit 9542f38

Browse files
oderskynicolasstucki
authored andcommitted
Optimize computation of Sourcefile#content
1 parent fc45300 commit 9542f38

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

compiler/src/dotty/tools/dotc/util/SourceFile.scala

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ object ScriptSourceFile {
3636

3737
class SourceFile(val file: AbstractFile, computeContent: => Array[Char]) extends interfaces.SourceFile {
3838

39-
lazy val content = computeContent
39+
private var myContent: Array[Char] = null
40+
41+
def content(): Array[Char] = {
42+
if (myContent == null) myContent = computeContent
43+
myContent
44+
}
4045

4146
def this(file: AbstractFile, codec: Codec) = this(file, new String(file.toByteArray, codec.charSet).toCharArray)
4247
def this(name: String, content: String) = this(new VirtualFile(name, content.getBytes), scala.io.Codec.UTF8)
@@ -54,9 +59,9 @@ class SourceFile(val file: AbstractFile, computeContent: => Array[Char]) extends
5459
}
5560
override def hashCode: Int = file.path.## + start.##
5661

57-
def apply(idx: Int): Char = content.apply(idx)
62+
def apply(idx: Int): Char = content().apply(idx)
5863

59-
val length: Int = content.length
64+
val length: Int = content().length
6065

6166
/** true for all source files except `NoSource` */
6267
def exists: Boolean = true
@@ -81,9 +86,9 @@ class SourceFile(val file: AbstractFile, computeContent: => Array[Char]) extends
8186

8287
private def isLineBreak(idx: Int) =
8388
if (idx >= length) false else {
84-
val ch = content(idx)
89+
val ch = content()(idx)
8590
// don't identify the CR in CR LF as a line break, since LF will do.
86-
if (ch == CR) (idx + 1 == length) || (content(idx + 1) != LF)
91+
if (ch == CR) (idx + 1 == length) || (content()(idx + 1) != LF)
8792
else isLineBreakChar(ch)
8893
}
8994

@@ -94,7 +99,7 @@ class SourceFile(val file: AbstractFile, computeContent: => Array[Char]) extends
9499
buf += cs.length // sentinel, so that findLine below works smoother
95100
buf.toArray
96101
}
97-
private lazy val lineIndices: Array[Int] = calculateLineIndices(content)
102+
private lazy val lineIndices: Array[Int] = calculateLineIndices(content())
98103

99104
/** Map line to offset of first character in line */
100105
def lineToOffset(index: Int): Int = lineIndices(index)
@@ -130,7 +135,7 @@ class SourceFile(val file: AbstractFile, computeContent: => Array[Char]) extends
130135
var idx = startOfLine(offset)
131136
var col = 0
132137
while (idx != offset) {
133-
col += (if (idx < length && content(idx) == '\t') (tabInc - col) % tabInc else 1)
138+
col += (if (idx < length && content()(idx) == '\t') (tabInc - col) % tabInc else 1)
134139
idx += 1
135140
}
136141
col
@@ -141,7 +146,7 @@ class SourceFile(val file: AbstractFile, computeContent: => Array[Char]) extends
141146
var idx = startOfLine(offset)
142147
val pad = new StringBuilder
143148
while (idx != offset) {
144-
pad.append(if (idx < length && content(idx) == '\t') '\t' else ' ')
149+
pad.append(if (idx < length && content()(idx) == '\t') '\t' else ' ')
145150
idx += 1
146151
}
147152
pad.result()

0 commit comments

Comments
 (0)