Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better memory efficiency for Analysis #1494

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}