Skip to content

Commit

Permalink
Merge pull request #1494 from dwijnand/feature/smaller-analysis
Browse files Browse the repository at this point in the history
Better memory efficiency for Analysis
  • Loading branch information
eed3si9n authored Nov 13, 2024
2 parents 1676f94 + 49adbf7 commit ced4262
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

/**
* Implement a Scala `lazy val` in Java for the facing sbt interface.
*
* <p>
* It holds a reference to a thunk that is lazily evaluated and then
* its reference is clear to avoid memory leaks in memory-intensive code.
* It needs to be defined in [[xsbti]] or a subpackage, see [[xsbti.api.Lazy]]
Expand All @@ -34,12 +34,7 @@ public static <T> xsbti.api.Lazy<T> apply(Supplier<T> sbtThunk) {
/** Return a sbt [[xsbti.api.Lazy]] from a strict value. */
public static <T> xsbti.api.Lazy<T> strict(T value) {
// Convert strict parameter to sbt function returning it
return apply(new Supplier<T>() {
@Override
public T get() {
return value;
}
});
return new StrictImpl<T>(value);
}

private static final class Thunky<T> {
Expand Down Expand Up @@ -68,4 +63,10 @@ public T get() {
return t.result;
}
}

private static final class StrictImpl<T> extends xsbti.api.AbstractLazy<T> {
private final T value;
StrictImpl(T value) { this.value = value; }
public T get() { return value; }
}
}
46 changes: 33 additions & 13 deletions internal/zinc-apiinfo/src/main/scala/xsbt/api/APIUtil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ object APIUtil {
def minimizeDefinition(d: Definition): Array[Definition] =
d match {
case c: ClassLike => Array(minimizeClass(c))
case _ => Array()
case _ => emptyDefs
}
def minimizeClass(c: ClassLike): ClassLike = {
val savedAnnotations = Discovery.defAnnotations(c.structure, (_: Any) => true).toArray[String]
Expand All @@ -73,7 +73,7 @@ object APIUtil {
c.modifiers,
c.annotations,
c.definitionType,
lzy(emptyType),
emptyTypeLzy,
lzy(struct),
savedAnnotations,
c.childrenOfSealedClass,
Expand All @@ -91,8 +91,10 @@ object APIUtil {
def filterDefinitions(
ds: Array[ClassDefinition],
isModule: Boolean
): Lazy[Array[ClassDefinition]] =
lzy(if (isModule) ds filter Discovery.isMainMethod else Array())
): Lazy[Array[ClassDefinition]] = {
val mains = if (isModule) ds.filter(Discovery.isMainMethod) else emptyClassDefs
if (mains.isEmpty) emptyClassDefsLzy else lzy(mains)
}

def isNonPrivate(d: Definition): Boolean = isNonPrivate(d.access)

Expand All @@ -104,23 +106,41 @@ object APIUtil {
}
private val emptyModifiers =
new Modifiers(false, false, false, false, false, false, false, false)
private val emptyStructure = Structure.of(lzy(Array.empty), lzy(Array.empty), lzy(Array.empty))
def emptyClassLike(name: String, definitionType: DefinitionType): ClassLike =
xsbti.api.ClassLike.of(
name,
private[this] val emptyType = EmptyType.of()
private val emptyTypeLzy = lzy(emptyType: Type)
private val emptyDefs = Array.empty[Definition]
private val emptyClassDefs = Array.empty[ClassDefinition]
private val emptyClassDefsLzy = lzy(emptyClassDefs)
private val emptyStructure = Structure.of(lzy(Array.empty), emptyClassDefsLzy, emptyClassDefsLzy)
private val emptyStructureLzy = lzy(emptyStructure)
private val emptyClassLikeTemplate =
ClassLike.of(
null,
Public.of(),
emptyModifiers,
Array.empty,
definitionType,
lzy(emptyType),
lzy(emptyStructure),
null,
emptyTypeLzy,
emptyStructureLzy,
Array.empty,
Array.empty,
true,
Array.empty
)
def emptyClassLike(name: String, definitionType: DefinitionType): ClassLike =
ClassLike.of(
name,
emptyClassLikeTemplate.access,
emptyClassLikeTemplate.modifiers,
emptyClassLikeTemplate.annotations,
definitionType,
emptyTypeLzy,
emptyStructureLzy,
emptyClassLikeTemplate.savedAnnotations,
emptyClassLikeTemplate.childrenOfSealedClass,
emptyClassLikeTemplate.topLevel,
emptyClassLikeTemplate.typeParameters,
)

private[this] def lzy[T <: AnyRef](t: T): Lazy[T] = SafeLazyProxy.strict(t)

private[this] val emptyType = EmptyType.of()
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import java.util.function.Supplier

/**
* Proxy `SafeLazy` functionality from the Java implementation
* implementation in xsbt.api.SafeLazy to Scala helpers.
* in xsbt.api.SafeLazy to Scala helpers.
*
* The implementation of these helpers are not reused between each
* other because they create intermediate anonymous functions and
Expand All @@ -35,7 +35,6 @@ object SafeLazyProxy {
* Return a lazy implementation of a strict value.
*/
def strict[T](s: T): Lazy[T] = {
val sbtThunk = new Supplier[T] { override def get() = s }
SafeLazy.apply(sbtThunk)
SafeLazy.strict(s)
}
}

0 comments on commit ced4262

Please sign in to comment.