Skip to content

Commit

Permalink
Using CachingSupplier in PE directly. Avoiding double wrapping.
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslavTulach committed Aug 29, 2024
1 parent 6ee9c8f commit 9d1f2f9
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.enso.interpreter.runtime.error.PanicSentinel;
import org.enso.interpreter.runtime.library.dispatch.TypeOfNode;
import org.enso.interpreter.runtime.library.dispatch.TypesLibrary;
import org.enso.interpreter.runtime.util.CachingSupplier;
import org.graalvm.collections.Pair;

public abstract class ReadArgumentCheckNode extends Node {
Expand Down Expand Up @@ -165,7 +166,8 @@ public static ReadArgumentCheckNode build(EnsoContext ctx, String comment, Type

public static ReadArgumentCheckNode meta(
String comment, Supplier<? extends Object> metaObjectSupplier) {
return ReadArgumentCheckNodeFactory.MetaCheckNodeGen.create(comment, metaObjectSupplier);
var cachingSupplier = CachingSupplier.wrap(metaObjectSupplier);
return ReadArgumentCheckNodeFactory.MetaCheckNodeGen.create(comment, cachingSupplier);
}

public static boolean isWrappedThunk(Function fn) {
Expand Down Expand Up @@ -477,10 +479,10 @@ String expectedTypeMessage() {
}

abstract static class MetaCheckNode extends ReadArgumentCheckNode {
private final Supplier<? extends Object> expectedSupplier;
private final CachingSupplier<? extends Object> expectedSupplier;
@CompilerDirectives.CompilationFinal private String expectedTypeMessage;

MetaCheckNode(String name, Supplier<? extends Object> expectedMetaSupplier) {
MetaCheckNode(String name, CachingSupplier<? extends Object> expectedMetaSupplier) {
super(name);
this.expectedSupplier = expectedMetaSupplier;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
public final class LazyObjectNode extends ExpressionNode {

private final String error;
private final Supplier<? extends Object> supply;
private final CachingSupplier<? extends Object> supply;

private LazyObjectNode(String error, Supplier<? extends Object> supply) {
this.error = error;
this.supply = supply;
this.supply = CachingSupplier.wrap(supply);
}

/**
Expand All @@ -30,7 +30,7 @@ private LazyObjectNode(String error, Supplier<? extends Object> supply) {
* errorMessage} error is created
*/
public static ExpressionNode build(String errorMessage, Supplier<TruffleObject> supplier) {
return new LazyObjectNode(errorMessage, new CachingSupplier<>(supplier));
return new LazyObjectNode(errorMessage, supplier);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ private Map<String, Map<String, Supplier<LoadedBuiltinMethod>>> registerBuiltinM
constr -> {
Map<String, Supplier<LoadedBuiltinMethod>> atomNodes =
getOrUpdate(builtinMethodNodes, constr.getName());
atomNodes.put(builtinMethodName, new CachingSupplier<>(() -> meta.toMethod()));
atomNodes.put(builtinMethodName, CachingSupplier.wrap(() -> meta.toMethod()));

Map<String, LoadedBuiltinMetaMethod> atomNodesMeta =
getOrUpdate(builtinMetaMethods, constr.getName());
Expand All @@ -253,7 +253,7 @@ private Map<String, Map<String, Supplier<LoadedBuiltinMethod>>> registerBuiltinM
() -> {
Map<String, Supplier<LoadedBuiltinMethod>> atomNodes =
getOrUpdate(builtinMethodNodes, builtinMethodOwner);
atomNodes.put(builtinMethodName, new CachingSupplier<>(() -> meta.toMethod()));
atomNodes.put(builtinMethodName, CachingSupplier.wrap(() -> meta.toMethod()));

Map<String, LoadedBuiltinMetaMethod> atomNodesMeta =
getOrUpdate(builtinMetaMethods, builtinMethodOwner);
Expand Down Expand Up @@ -420,12 +420,12 @@ private Map<String, Map<String, Supplier<LoadedBuiltinMethod>>> readBuiltinMetho
constr -> {
Map<String, Supplier<LoadedBuiltinMethod>> atomNodes =
getOrUpdate(methodNodes, constr.getName());
atomNodes.put(builtinMethodName, new CachingSupplier<>(builtin));
atomNodes.put(builtinMethodName, CachingSupplier.forValue(builtin));
},
() -> {
Map<String, Supplier<LoadedBuiltinMethod>> atomNodes =
getOrUpdate(methodNodes, builtinMethodOwner);
atomNodes.put(builtinMethodName, new CachingSupplier<>(builtin));
atomNodes.put(builtinMethodName, CachingSupplier.forValue(builtin));
});
});
return methodNodes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ public void registerMethod(Type type, String method, Function function) {
if (methodMap.containsKey(method) && !type.isBuiltin()) {
throw new RedefinedMethodException(type.getName(), method);
} else {
methodMap.put(method, new CachingSupplier<>(function));
methodMap.put(method, CachingSupplier.forValue(function));
}
}

Expand All @@ -390,7 +390,7 @@ public void registerMethod(Type type, String method, Supplier<Function> supply)
if (methodMap.containsKey(method) && !type.isBuiltin()) {
throw new RedefinedMethodException(type.getName(), method);
} else {
methodMap.put(method, new CachingSupplier<>(supply));
methodMap.put(method, CachingSupplier.wrap(supply));
}
}

Expand Down Expand Up @@ -419,7 +419,7 @@ public void registerConversionMethod(Type toType, Type fromType, Function functi
*/
public void registerPolyglotSymbol(String name, Supplier<TruffleObject> symbolFactory) {
assert moduleScope == null;
polyglotSymbols.put(name, new CachingSupplier<>(symbolFactory));
polyglotSymbols.put(name, CachingSupplier.wrap(symbolFactory));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,26 @@ public final class CachingSupplier<T> implements Supplier<T> {
@CompilerDirectives.CompilationFinal private boolean memoComputed;
@CompilerDirectives.CompilationFinal private T memo;

public CachingSupplier(Supplier<T> supply) {
private CachingSupplier(Supplier<T> supply) {
this.supply = supply;
}

public CachingSupplier(T memo) {
private CachingSupplier(T memo) {
this.supply = null;
this.memo = memo;
this.memoComputed = true;
}

public static <V> CachingSupplier<V> wrap(Supplier<V> supply) {
if (supply instanceof CachingSupplier<V> cs) {
return cs;
} else {
return new CachingSupplier<>(supply);
}
}

public static <V> CachingSupplier<V> forValue(V value) {
return new CachingSupplier<>(value);
}

@Override
Expand Down

0 comments on commit 9d1f2f9

Please sign in to comment.