Skip to content

Inefficient findClass implementation #144

Closed
@gkossakowski

Description

@gkossakowski

The implementation of the AggregateFlatClassPath.findClass is inefficient:

override def findClass(className: String): Option[ClassRepresentation[AbstractFile]] = {
    val (pkg, simpleClassName) = PackageNameUtils.separatePkgAndClassNames(className)

    @tailrec
    def findEntry[T <: ClassRepClassPathEntry](aggregates: Seq[FlatClassPath], getEntries: FlatClassPath => Seq[T]): Option[T] =
      if (aggregates.nonEmpty) {
        val entry = getEntries(aggregates.head).find(_.name == simpleClassName)
        if (entry.isDefined) entry
        else findEntry(aggregates.tail, getEntries)
      } else None

    val classEntry = findEntry(aggregates, classesGetter(pkg))
    val sourceEntry = findEntry(aggregates, sourcesGetter(pkg))

    (classEntry, sourceEntry) match {
      case (Some(c), Some(s)) => Some(ClassAndSourceFilesEntry(c.file, s.file))
      case (c @ Some(_), _) => c
      case (_, s) => s
    }
  }

This method list all classpath entries in a package just to pick one entry. It should just delegate to findClass of underlying classpath elements that are being aggregated.

This causes orders of magnitude slowdown of compilation in https://github.com/gkossakowski/sbt-slow-compile

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions