Skip to content

Commit a3d9a30

Browse files
committed
WIP better data structures/algorithms for classpath merging
1 parent d31f703 commit a3d9a30

File tree

4 files changed

+32
-21
lines changed

4 files changed

+32
-21
lines changed

src/compiler/scala/tools/nsc/classpath/AggregateClassPath.scala

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,26 @@ case class AggregateClassPath(aggregates: Seq[ClassPath]) extends ClassPath {
7979

8080
override private[nsc] def hasPackage(pkg: PackageName) = aggregates.exists(_.hasPackage(pkg))
8181
override private[nsc] def list(inPackage: PackageName): ClassPathEntries = {
82-
83-
val (packages, classesAndSources) = aggregates.map { cp =>
82+
var packages: java.util.HashSet[PackageEntry] = null
83+
val classesAndSources = collection.mutable.ArrayBuffer[ClassRepresentation]()
84+
aggregates.foreach { cp =>
8485
try {
85-
cp.list(inPackage)
86+
val entries = cp.list(inPackage)
87+
if (entries._1.nonEmpty) {
88+
if (packages == null) packages = new java.util.HashSet[PackageEntry]()
89+
entries._1.foreach(entry => packages.add(entry))
90+
91+
classesAndSources ++= entries._2
92+
}
8693
} catch {
8794
case ex: java.io.IOException =>
8895
val e = FatalError(ex.getMessage)
8996
e.initCause(ex)
9097
throw e
9198
}
92-
}.unzip
93-
val distinctPackages = packages.flatten.distinct
99+
}
100+
101+
val distinctPackages: Seq[PackageEntry] = if (packages == null) Nil else packages.toArray(new Array[PackageEntry](packages.size()))
94102
val distinctClassesAndSources = mergeClassesAndSources(classesAndSources)
95103
ClassPathEntries(distinctPackages, distinctClassesAndSources)
96104
}
@@ -100,14 +108,13 @@ case class AggregateClassPath(aggregates: Seq[ClassPath]) extends ClassPath {
100108
* creates an entry containing both of them. If there would be more than one class or source
101109
* entries for the same class it always would use the first entry of each type found on a classpath.
102110
*/
103-
private def mergeClassesAndSources(entries: Seq[Seq[ClassRepresentation]]): Seq[ClassRepresentation] = {
111+
private def mergeClassesAndSources(entries: Seq[ClassRepresentation]): Seq[ClassRepresentation] = {
104112
var count = 0
105113
val indices = collection.mutable.HashMap[String, Int]()
106114
val mergedEntries = new ArrayBuffer[ClassRepresentation](1024)
107115

108116
for {
109-
partOfEntries <- entries
110-
entry <- partOfEntries
117+
entry <- entries
111118
} {
112119
val name = entry.name
113120
if (indices contains name) {

src/compiler/scala/tools/nsc/classpath/ZipArchiveFileLookup.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ trait ZipArchiveFileLookup[FileEntryType <: ClassRepresentation] extends ClassPa
7474
}
7575

7676
private def findDirEntry(pkg: PackageName): Option[archive.DirEntry] = {
77-
archive.allDirs.get(pkg.dirPathTrailingSlash)
77+
Option(archive.allDirs.get(pkg.dirPathTrailingSlash))
7878
}
7979

8080

src/library/scala/collection/mutable/AnyRefMap.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,10 @@ extends AbstractMap[K, V]
108108
var e = h & mask
109109
var x = 0
110110
var g = 0
111-
while ({ g = _hashes(e); g != 0}) {
112-
if (g == h && { val q = _keys(e); (q eq k) || ((q ne null) && (q equals k)) }) return e
111+
val hashes = _hashes
112+
val keys = _keys
113+
while ({ g = hashes(e); g != 0}) {
114+
if (g == h && { val q = keys(e); (q eq k) || ((q ne null) && (q equals k)) }) return e
113115
x += 1
114116
e = (e + 2*(x+1)*x - 3) & mask
115117
}

src/reflect/scala/reflect/io/ZipArchive.scala

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,19 +108,19 @@ abstract class ZipArchive(override val file: JFile, release: Option[String]) ext
108108
}
109109
}
110110

111-
private def ensureDir(dirs: mutable.Map[String, DirEntry], path: String, zipEntry: ZipEntry): DirEntry = {
111+
private def ensureDir(dirs: java.util.Map[String, DirEntry], path: String, zipEntry: ZipEntry): DirEntry = {
112112
dirs get path match {
113-
case Some(v) => v
114-
case None =>
113+
case null =>
115114
val parent = ensureDir(dirs, dirName(path), null)
116115
val dir = new DirEntry(path)
117116
parent.entries(baseName(path)) = dir
118-
dirs(path) = dir
117+
dirs.put(path, dir)
119118
dir
119+
case v => v
120120
}
121121
}
122122

123-
protected def getDir(dirs: mutable.Map[String, DirEntry], entry: ZipEntry): DirEntry = {
123+
protected def getDir(dirs: java.util.Map[String, DirEntry], entry: ZipEntry): DirEntry = {
124124
if (entry.isDirectory) ensureDir(dirs, entry.getName, entry)
125125
else ensureDir(dirs, dirName(entry.getName), null)
126126
}
@@ -172,10 +172,10 @@ final class FileZipArchive(file: JFile, release: Option[String]) extends ZipArch
172172
override def sizeOption: Option[Int] = Some(zipEntry.getSize.toInt)
173173
}
174174

175-
private[this] val dirs = mutable.HashMap[String, DirEntry]()
175+
private[this] val dirs = new java.util.HashMap[String, DirEntry]()
176176
lazy val root: DirEntry = {
177177
val root = new DirEntry("/")
178-
dirs("/") = root
178+
dirs.put("/", root)
179179
val zipFile = openZipFile()
180180
val enum = zipFile.entries()
181181

@@ -209,7 +209,7 @@ final class FileZipArchive(file: JFile, release: Option[String]) extends ZipArch
209209
root
210210
}
211211

212-
lazy val allDirs: mutable.HashMap[String, DirEntry] = { root; dirs }
212+
lazy val allDirs: java.util.Map[String, DirEntry] = { root; dirs }
213213

214214
def iterator: Iterator[Entry] = root.iterator
215215

@@ -234,7 +234,8 @@ final class FileZipArchive(file: JFile, release: Option[String]) extends ZipArch
234234
final class URLZipArchive(val url: URL) extends ZipArchive(null) {
235235
def iterator: Iterator[Entry] = {
236236
val root = new DirEntry("/")
237-
val dirs = mutable.HashMap[String, DirEntry]("" -> root)
237+
val dirs = new java.util.HashMap[String, DirEntry]()
238+
dirs.put("", root)
238239
val in = new ZipInputStream(new ByteArrayInputStream(Streamable.bytes(input)))
239240
closeables ::= in
240241

@@ -307,7 +308,8 @@ final class URLZipArchive(val url: URL) extends ZipArchive(null) {
307308
final class ManifestResources(val url: URL) extends ZipArchive(null) {
308309
def iterator = {
309310
val root = new DirEntry("/")
310-
val dirs = mutable.HashMap[String, DirEntry]("" -> root)
311+
val dirs = new java.util.HashMap[String, DirEntry]
312+
dirs.put("", root)
311313
val manifest = new Manifest(input)
312314
closeables ::= input
313315

0 commit comments

Comments
 (0)