Skip to content

Commit de43fb4

Browse files
committed
Optimize SourceFile.fromId
1 parent 5749b5f commit de43fb4

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,14 @@ class SourceFile(val file: AbstractFile, computeContent: => Array[Char]) extends
173173
}
174174

175175
private def newChunk: Int = sourceOfChunk.synchronized {
176-
val id = sourceOfChunk.length * ChunkSize
177-
sourceOfChunk += this
176+
val id = chunks << ChunkSizeLog
177+
if (chunks == sourceOfChunk.length) {
178+
val a = new Array[SourceFile](chunks * 2)
179+
Array.copy(sourceOfChunk, 0, a, 0, chunks)
180+
sourceOfChunk = a
181+
}
182+
sourceOfChunk(chunks) = this
183+
chunks += 1
178184
ctr.set(id + 1)
179185
id
180186
}
@@ -184,11 +190,12 @@ object SourceFile {
184190

185191
type PathName = TermName
186192

187-
def fromId(id: Int): SourceFile =
188-
sourceOfChunk(id / ChunkSize)
193+
def fromId(id: Int): SourceFile = sourceOfChunk(id >> ChunkSizeLog)
189194

190-
private val ChunkSize = 1024
191-
@sharable private val sourceOfChunk = mutable.ArrayBuffer[SourceFile]()
195+
private final val ChunkSizeLog = 10
196+
private final val ChunkSize = 1 << ChunkSizeLog
197+
@sharable private var chunks: Int = 0
198+
@sharable private var sourceOfChunk: Array[SourceFile] = new Array[SourceFile](2000)
192199
}
193200

194201
@sharable object NoSource extends SourceFile(NoAbstractFile, Array[Char]()) {

0 commit comments

Comments
 (0)