Skip to content

Commit

Permalink
Fix #17577: scaladoc hangs with deep inheritance
Browse files Browse the repository at this point in the history
This commit has a pair of fixes that bring the scaladoc build time for the repository mentioned in #17577 to a minute instead of hanging.

- Reimplement HierarchyGraph's `+` method in terms of `++` Previously, `++` was implemented in terms of `+`, but `+` called `distinct` resulting in poor performance. Now `distinct` is only called once for a `++` call.
- Get the distinct `LinkToType` values in `InheritanceInformationTransformer.apply`. Previously, `subtypes` could contain duplicates, causing redundant  calls to `getEdges`

[Cherry-picked 9859e89]
  • Loading branch information
reardonj authored and Kordyjan committed Nov 17, 2023
1 parent 854dc51 commit d3266d8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
8 changes: 4 additions & 4 deletions scaladoc/src/dotty/tools/scaladoc/api.scala
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@ case class LinkToType(signature: Signature, dri: DRI, kind: Kind)
case class HierarchyGraph(edges: Seq[(LinkToType, LinkToType)], sealedNodes: Set[LinkToType] = Set.empty):
def vertecies: Seq[LinkToType] = edges.flatten((a, b) => Seq(a, b)).distinct
def verteciesWithId: Map[LinkToType, Int] = vertecies.zipWithIndex.toMap
def +(edge: (LinkToType, LinkToType)): HierarchyGraph = HierarchyGraph((edges :+ edge).distinct)
def ++(edges: Seq[(LinkToType, LinkToType)]): HierarchyGraph = edges.foldLeft(this) {
case (acc, edge) => acc + edge
}
def +(edge: (LinkToType, LinkToType)): HierarchyGraph = this ++ Seq(edge)
def ++(edges: Seq[(LinkToType, LinkToType)]): HierarchyGraph =
this.copy(edges = this.edges.view.concat(edges).distinct.toSeq)
object HierarchyGraph:
def empty = HierarchyGraph(Seq.empty)
def withEdges(edges: Seq[(LinkToType, LinkToType)]) = HierarchyGraph.empty ++ edges
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ package transformers

class InheritanceInformationTransformer(using DocContext) extends (Module => Module):
override def apply(original: Module): Module =
val subtypes = getSupertypes(original.rootPackage).groupMap(_(0))(_(1))
val subtypes = getSupertypes(original.rootPackage).groupMap(_(0))(_(1)).view.mapValues(_.distinct).toMap
original.updateMembers { m =>
val edges = getEdges(m.asLink.copy(kind = bareClasslikeKind(m.kind)), subtypes)
val st: Seq[LinkToType] = edges.map(_._1).distinct
m.withKnownChildren(st).withNewGraphEdges(edges)
m.withKnownChildren(st).withNewGraphEdges(edges.toSeq)
}

private def getEdges(ltt: LinkToType, subtypes: Map[DRI, Seq[LinkToType]]): Seq[(LinkToType, LinkToType)] =
val st: Seq[LinkToType] = subtypes.getOrElse(ltt.dri, Nil)
st.flatMap(s => Seq(s -> ltt) ++ getEdges(s, subtypes))
val st: Seq[LinkToType] = subtypes.getOrElse(ltt.dri, Vector.empty)
st.flatMap(s => Vector(s -> ltt) ++ getEdges(s, subtypes))

private def bareClasslikeKind(kind: Kind): Kind = kind match
case _: Kind.Trait => Kind.Trait(Nil, Nil)
Expand Down

0 comments on commit d3266d8

Please sign in to comment.