Skip to content

Commit

Permalink
Add methods to remove unused values (treeshake) (#1273)
Browse files Browse the repository at this point in the history
* Add methods to remove unused values (treeshake)

* minor cleanup
  • Loading branch information
johnynek authored Nov 22, 2024
1 parent 09e3e62 commit a4bbaa6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
15 changes: 11 additions & 4 deletions core/src/main/scala/org/bykn/bosatsu/Package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,17 @@ object Package {
type Parsed = Package[PackageName, Unit, Unit, List[Statement]]
type Resolved =
FixPackage[Unit, Unit, (List[Statement], ImportMap[PackageName, Unit])]
type TypedProgram[T] = (
Program[TypeEnv[Kind.Arg], TypedExpr[T], Any],
ImportMap[Interface, NonEmptyList[Referant[Kind.Arg]]]
)
type Typed[T] = Package[
Interface,
NonEmptyList[Referant[Kind.Arg]],
Referant[
Kind.Arg
],
(
Program[TypeEnv[Kind.Arg], TypedExpr[T], Any],
ImportMap[Interface, NonEmptyList[Referant[Kind.Arg]]]
)
TypedProgram[T]
]
type Inferred = Typed[Declaration]

Expand Down Expand Up @@ -528,6 +529,12 @@ object Package {
(Package.Interface, ImportedName[NonEmptyList[Referant[Kind.Arg]]])
] =
pack.program._2(n)

def filterLets(fn: Identifier => Boolean): Typed[A] = {
val (prog, importMap) = pack.program
val prog1 = prog.copy(lets = prog.lets.filter { case (b, _, _) => fn(b) })
pack.copy(program = (prog1, importMap))
}
}

def orderByName[A, B, C, D]: Order[Package[A, B, C, D]] =
Expand Down
22 changes: 21 additions & 1 deletion core/src/main/scala/org/bykn/bosatsu/PackageMap.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.bykn.bosatsu

import org.bykn.bosatsu.graph.{Memoize, Toposort}
import org.bykn.bosatsu.graph.{Dag, Memoize, Toposort}
import cats.{Foldable, Monad, Show}
import cats.data.{
Ior,
Expand Down Expand Up @@ -111,6 +111,26 @@ object PackageMap {
// convenience for type inference
def toAnyTyped[A](p: Typed[A]): Typed[Any] = p

def treeShake[A](p: Typed[A], roots: Set[(PackageName, Identifier)]): Typed[A] = {
type Ident = (PackageName, Identifier)

def dependency(a: Ident): Iterable[Ident] =
p.toMap.get(a._1) match {
case Some(pack) =>
pack.lets.flatMap { case (_, _, te) => te.globals }
case None => Nil
}

val keep = Dag.transitiveSet(roots.toList.sorted)(dependency)

val kept = p.toMap.iterator.map { case (_, pack) =>
pack.filterLets { nm => keep((pack.name, nm)) }
}
.toList

fromIterable(kept)
}

type Inferred = Typed[Declaration]

/** This builds a DAG of actual packages where names have been replaced by the
Expand Down

0 comments on commit a4bbaa6

Please sign in to comment.