Skip to content

Commit 943279f

Browse files
committed
[Experiment] Global file content cache
1 parent 250facc commit 943279f

File tree

2 files changed

+23
-41
lines changed

2 files changed

+23
-41
lines changed

compiler/src/dotty/tools/dotc/core/classfile/ReusableDataReader.scala

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -35,47 +35,15 @@ final class ReusableDataReader() extends DataReader {
3535

3636
def reset(file: dotty.tools.io.AbstractFile): this.type = {
3737
this.size = 0
38-
file.sizeOption match {
39-
case Some(size) =>
40-
if (size > data.length) {
41-
data = new Array[Byte](nextPositivePowerOfTwo(size))
42-
} else {
43-
java.util.Arrays.fill(data, 0.toByte)
44-
}
45-
val input = file.input
46-
try {
47-
var endOfInput = false
48-
while (!endOfInput) {
49-
val remaining = data.length - this.size
50-
if (remaining == 0) endOfInput = true
51-
else {
52-
val read = input.read(data, this.size, remaining)
53-
if (read < 0) endOfInput = true
54-
else this.size += read
55-
}
56-
}
57-
bb = ByteBuffer.wrap(data, 0, size)
58-
} finally {
59-
input.close()
60-
}
61-
case None =>
62-
val input = file.input
63-
try {
64-
var endOfInput = false
65-
while (!endOfInput) {
66-
val remaining = data.length - size
67-
if (remaining == 0) {
68-
data = java.util.Arrays.copyOf(data, nextPositivePowerOfTwo(size))
69-
}
70-
val read = input.read(data, this.size, data.length - this.size)
71-
if (read < 0) endOfInput = true
72-
else this.size += read
73-
}
74-
bb = ByteBuffer.wrap(data, 0, size)
75-
} finally {
76-
input.close()
77-
}
38+
val bytes = file.toByteArray
39+
val size = bytes.length
40+
if (size > data.length) {
41+
data = new Array[Byte](nextPositivePowerOfTwo(size))
42+
} else {
43+
java.util.Arrays.fill(data, 0.toByte)
7844
}
45+
System.arraycopy(bytes, 0, data, 0, size)
46+
bb = ByteBuffer.wrap(data, 0, size)
7947
this
8048
}
8149

compiler/src/dotty/tools/io/AbstractFile.scala

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,20 @@ object AbstractFile {
5555
else new PlainFile(new Path(Paths.get(url.toURI)))
5656

5757
def getResources(url: URL): AbstractFile = ZipArchive.fromManifestURL(url)
58+
59+
val cache = scala.collection.mutable.Map[String, (Long, Array[Byte])]()
60+
inline def cachedBytes(file: AbstractFile)(compute: => Array[Byte]): Array[Byte] = {
61+
val key = file.absolutePath
62+
val lastModified = file.lastModified
63+
cache.get(key) match {
64+
case Some((modTime, bytes)) if modTime == lastModified =>
65+
bytes
66+
case _ =>
67+
val bytes = compute
68+
cache.update(key, (lastModified, bytes))
69+
bytes
70+
}
71+
}
5872
}
5973

6074
/**
@@ -169,7 +183,7 @@ abstract class AbstractFile extends Iterable[AbstractFile] {
169183
/** Returns contents of file (if applicable) in a byte array.
170184
*/
171185
@throws(classOf[IOException])
172-
def toByteArray: Array[Byte] = {
186+
def toByteArray: Array[Byte] = AbstractFile.cachedBytes(this) {
173187
val in = input
174188
sizeOption match {
175189
case Some(size) =>

0 commit comments

Comments
 (0)