Skip to content
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
6 changes: 6 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -446,3 +446,9 @@ task benchmark(type: JavaExec) {
main = "org.cyclops.integratedscripting.evaluate.translation.BenchmarkValueTranslators"
}
test.dependsOn benchmark

task benchmarkScriptEvaluation(type: JavaExec) {
classpath sourceSets.test.runtimeClasspath
main = "org.cyclops.integratedscripting.evaluate.translation.BenchmarkScriptEvaluation"
}
test.dependsOn benchmarkScriptEvaluation
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.cyclops.integratedscripting.api.evaluate.translation;

import org.cyclops.integrateddynamics.api.evaluate.variable.IValueType;

/**
* A Graal proxy that wraps an Integrated Dynamics value of a known value type.
*
* Proxies implementing this interface can be mapped to their {@link IValueTranslator} directly,
* instead of having to fall back to a linear scan over all registered translators.
*
* @author rubensworks
*/
public interface IValueProxy {

/**
* @return The value type of the Integrated Dynamics value that is being proxied.
*/
public IValueType<?> getProxiedValueType();

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Value;

import javax.annotation.Nullable;

/**
* Translates ID values to and from Graal values.
* @author rubensworks
Expand All @@ -18,6 +20,22 @@ public interface IValueTranslator<V extends IValue> {

public boolean canHandleGraalValue(Value value);

/**
* If this translator handles Graal values that have exactly one member with a fixed key,
* then returning that key here allows {@link IValueTranslatorRegistry} to dispatch on it directly.
*
* This is purely an optimization: it avoids having to inspect the member keys of a value
* once for every such translator, which is relatively expensive as it crosses the host boundary.
* Translators returning a non-null key here must handle exactly those Graal values
* whose member keys are exactly the returned key.
*
* @return The single member key this translator dispatches on, or null if it dispatches differently.
*/
@Nullable
public default String getGraalValueMemberKey() {
return null;
}

boolean canTranslateNbt();

public Value translateToGraal(Context context, V value, IEvaluationExceptionFactory exceptionFactory, ValueDeseralizationContext valueDeseralizationContext) throws EvaluationException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.cyclops.integratedscripting.core.packageddependencies.UnsafeHelper;
import org.cyclops.integratedscripting.evaluate.translation.ValueTranslators;
import org.graalvm.polyglot.*;
import org.graalvm.polyglot.proxy.ProxyExecutable;

import javax.annotation.Nullable;
import java.nio.file.Path;
Expand All @@ -35,6 +36,32 @@ public class ScriptHelpers {
}
}

/**
* A factory for the {@code idContext} object, with {@code ops} defined as a self-replacing lazy getter.
* This way, the global operators are only translated once a script actually accesses them,
* while accesses after the first one are plain property reads.
*/
private static final Source SOURCE_ID_CONTEXT = Source.newBuilder("js", """
(function(resolveOps) {
var idContext = {};
Object.defineProperty(idContext, 'ops', {
configurable: true,
enumerable: true,
get: function() {
var ops = resolveOps();
Object.defineProperty(idContext, 'ops', {
value: ops,
configurable: true,
enumerable: true,
writable: true,
});
return ops;
},
});
return idContext;
})
""", "integratedscripting_idcontext.js").buildLiteral();

public static void load() {
// Do nothing
}
Expand Down Expand Up @@ -78,15 +105,23 @@ public static Context createBaseContext(@Nullable Function<Context.Builder, Cont
public static Context createPopulatedContext(@Nullable Function<Context.Builder, Context.Builder> contextBuilderModifier, ValueDeseralizationContext valueDeseralizationContext) throws EvaluationException {
Context context = createBaseContext(contextBuilderModifier);

// Create idContext field with ops
// Create idContext field with ops.
// The ops object is populated lazily, because translating all global operators is expensive,
// while many scripts never touch them.
Value jsBindings = context.getBindings("js");
Value jsObjectClass = jsBindings.getMember("Object");
Value idContext = jsObjectClass.newInstance();
Value ops = jsObjectClass.newInstance();
for (Map.Entry<String, IOperator> entry : Operators.REGISTRY.getGlobalInteractOperators().entrySet()) {
ops.putMember(entry.getKey(), ValueTranslators.REGISTRY.translateToGraal(context, ValueTypeOperator.ValueOperator.of(entry.getValue()), getDummyEvaluationExceptionFactory(), valueDeseralizationContext));
}
idContext.putMember("ops", ops);
Value idContext = context.eval(SOURCE_ID_CONTEXT).execute((ProxyExecutable) args -> {
Value ops = jsBindings.getMember("Object").newInstance();
try {
for (Map.Entry<String, IOperator> entry : Operators.REGISTRY.getGlobalInteractOperators().entrySet()) {
ops.putMember(entry.getKey(), ValueTranslators.REGISTRY.translateToGraal(context,
ValueTypeOperator.ValueOperator.of(entry.getValue()),
getDummyEvaluationExceptionFactory(), valueDeseralizationContext));
}
} catch (EvaluationException e) {
throw new RuntimeException(e);
}
return ops;
});
jsBindings.putMember("idContext", idContext);

return context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
import org.cyclops.integrateddynamics.api.evaluate.variable.IValueType;
import org.cyclops.integrateddynamics.api.evaluate.variable.ValueDeseralizationContext;
import org.cyclops.integratedscripting.api.evaluate.translation.IEvaluationExceptionFactory;
import org.cyclops.integratedscripting.api.evaluate.translation.IValueProxy;
import org.cyclops.integratedscripting.api.evaluate.translation.IValueTranslator;
import org.cyclops.integratedscripting.api.evaluate.translation.IValueTranslatorRegistry;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Value;

import javax.annotation.Nullable;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* @author rubensworks
Expand All @@ -27,6 +30,10 @@ public class ValueTranslatorRegistry implements IValueTranslatorRegistry {
private final List<IValueTranslator> translators = Lists.newArrayList();
private final Map<IValueType<?>, IValueTranslator> valueTypeTranslators = Maps.newIdentityHashMap();

// Snapshots of translators, and the member keys they dispatch on, to avoid repeated lookups while dispatching.
private IValueTranslator[] translatorsArray = new IValueTranslator[0];
private String[] translatorMemberKeys = new String[0];

private ValueTranslatorRegistry() {
}

Expand All @@ -41,6 +48,11 @@ public static ValueTranslatorRegistry getInstance() {
public void register(IValueTranslator translator) {
translators.add(translator);
valueTypeTranslators.put(translator.getValueType(), translator);

this.translatorsArray = translators.toArray(new IValueTranslator[0]);
this.translatorMemberKeys = translators.stream()
.map(IValueTranslator::getGraalValueMemberKey)
.toArray(String[]::new);
}

@Override
Expand All @@ -59,14 +71,52 @@ public <V extends IValue> Value translateToGraal(Context context, V value, IEval

@Override
public IValueTranslator getScriptValueTranslator(Value scriptValue) {
for (IValueTranslator translator : translators) {
if (translator.canHandleGraalValue(scriptValue)) {
return translator;
// Translators that dispatch on a single member key are all matched against the same member key set,
// which is only materialized once, and only once such a translator is actually reached.
// Crossing the host boundary is relatively expensive,
// so the number of calls on the Graal value is deliberately kept as low as possible here.
Set<String> valueMemberKeys = null;
boolean valueMembersResolved = false;

IValueTranslator[] translators = this.translatorsArray;
String[] translatorMemberKeys = this.translatorMemberKeys;
for (int i = 0; i < translators.length; i++) {
String translatorMemberKey = translatorMemberKeys[i];
if (translatorMemberKey == null) {
if (translators[i].canHandleGraalValue(scriptValue)) {
return translators[i];
}
} else {
if (!valueMembersResolved) {
valueMembersResolved = true;

// Fast path for values that were translated to Graal before:
// their proxy directly tells us which value type they correspond to.
IValueTranslator proxiedTranslator = getProxiedValueTranslator(scriptValue);
if (proxiedTranslator != null) {
return proxiedTranslator;
}

valueMemberKeys = scriptValue.hasMembers() ? scriptValue.getMemberKeys() : null;
}
if (valueMemberKeys != null
&& valueMemberKeys.size() == 1
&& valueMemberKeys.contains(translatorMemberKey)) {
return translators[i];
}
}
}
return null;
}

@Nullable
protected IValueTranslator getProxiedValueTranslator(Value scriptValue) {
if (scriptValue.isProxyObject() && scriptValue.asProxyObject() instanceof IValueProxy valueProxy) {
return getValueTypeTranslator(valueProxy.getProxiedValueType());
}
return null;
}

@Override
public <V extends IValue> V translateFromGraal(Context context, Value value, IEvaluationExceptionFactory exceptionFactory, ValueDeseralizationContext valueDeseralizationContext) throws EvaluationException {
IValueTranslator translator = getScriptValueTranslator(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import net.minecraft.nbt.CompoundTag;
import org.cyclops.integrateddynamics.api.evaluate.operator.IOperator;
import org.cyclops.integrateddynamics.api.evaluate.variable.IValue;
import org.cyclops.integrateddynamics.api.evaluate.variable.IValueType;
import org.cyclops.integrateddynamics.api.evaluate.variable.ValueDeseralizationContext;
import org.cyclops.integrateddynamics.core.evaluate.variable.ValueTypes;
import org.cyclops.integratedscripting.api.evaluate.translation.IValueProxy;
import org.cyclops.integrateddynamics.core.evaluate.operator.CurriedOperator;
import org.cyclops.integrateddynamics.core.evaluate.variable.ValueTypeOperator;
import org.cyclops.integrateddynamics.core.evaluate.variable.Variable;
Expand All @@ -21,7 +24,7 @@
* A Graal proxy object for NBT CompoundTag values.
* @author rubensworks
*/
public class NbtCompoundTagProxyObject implements ProxyObject {
public class NbtCompoundTagProxyObject implements ProxyObject, IValueProxy {

private final Context context;
private final IEvaluationExceptionFactory exceptionFactory;
Expand All @@ -46,6 +49,11 @@ public CompoundTag getTag() {
return tag;
}

@Override
public IValueType<?> getProxiedValueType() {
return ValueTypes.NBT;
}

@Nullable
public IValue getValue() {
return value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
import org.cyclops.integrateddynamics.api.evaluate.variable.ValueDeseralizationContext;
import org.cyclops.integrateddynamics.core.evaluate.variable.ValueTypeOperator;
import org.cyclops.integrateddynamics.core.evaluate.variable.Variable;
import org.cyclops.integrateddynamics.core.evaluate.variable.ValueTypes;
import org.cyclops.integrateddynamics.api.evaluate.variable.IValueType;
import org.cyclops.integratedscripting.api.evaluate.translation.IEvaluationExceptionFactory;
import org.cyclops.integratedscripting.api.evaluate.translation.IValueProxy;
import org.cyclops.integratedscripting.evaluate.translation.ValueTranslators;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Value;
Expand All @@ -16,7 +19,7 @@
* A Graal proxy executable for operator values.
* @author rubensworks
*/
public class OperatorProxyExecutable implements ProxyExecutable {
public class OperatorProxyExecutable implements ProxyExecutable, IValueProxy {
private final Context context;
private final ValueTypeOperator.ValueOperator value;
private final IEvaluationExceptionFactory exceptionFactory;
Expand All @@ -33,6 +36,11 @@ public ValueTypeOperator.ValueOperator getValue() {
return value;
}

@Override
public IValueType<?> getProxiedValueType() {
return ValueTypes.OPERATOR;
}

@SneakyThrows
@Override
public Object execute(Value... args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import net.minecraft.network.chat.Component;
import org.cyclops.integrateddynamics.api.evaluate.operator.IOperator;
import org.cyclops.integrateddynamics.api.evaluate.variable.IValue;
import org.cyclops.integrateddynamics.api.evaluate.variable.IValueType;
import org.cyclops.integrateddynamics.api.evaluate.variable.ValueDeseralizationContext;
import org.cyclops.integrateddynamics.core.evaluate.operator.CurriedOperator;
import org.cyclops.integrateddynamics.core.evaluate.variable.ValueObjectTypeBase;
import org.cyclops.integrateddynamics.core.evaluate.variable.ValueTypeOperator;
import org.cyclops.integrateddynamics.core.evaluate.variable.Variable;
import org.cyclops.integratedscripting.api.evaluate.translation.IEvaluationExceptionFactory;
import org.cyclops.integratedscripting.api.evaluate.translation.IValueProxy;
import org.cyclops.integratedscripting.evaluate.translation.ValueTranslators;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Value;
Expand All @@ -23,7 +25,7 @@
* A Graal proxy object for object values.
* @author rubensworks
*/
public class ValueObjectProxyObject<V extends IValue> implements ProxyObject {
public class ValueObjectProxyObject<V extends IValue> implements ProxyObject, IValueProxy {

private final Context context;
private final IEvaluationExceptionFactory exceptionFactory;
Expand Down Expand Up @@ -52,6 +54,11 @@ public ValueObjectTypeBase<V> getValueType() {
return valueType;
}

@Override
public IValueType<?> getProxiedValueType() {
return this.valueType;
}

@Nullable
public IValue getValue() {
return value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.cyclops.integratedscripting.evaluate.translation.translator;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Sets;
import net.minecraft.nbt.*;
import net.minecraft.network.chat.Component;
Expand All @@ -15,6 +16,7 @@
import org.cyclops.integratedscripting.evaluate.translation.ValueTranslators;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Value;
import org.graalvm.polyglot.proxy.ProxyObject;

import javax.annotation.Nullable;
import java.util.ArrayList;
Expand All @@ -26,6 +28,9 @@
*/
public class ValueTranslatorNbt implements IValueTranslator<ValueTypeNbt.ValueNbt> {

private static final String KEY_END_TAG = "nbt_end";
private static final ProxyObject PROXY_END_TAG = ProxyObject.fromMap(ImmutableMap.of(KEY_END_TAG, true));

@Override
public IValueType<?> getValueType() {
return ValueTypes.NBT;
Expand Down Expand Up @@ -53,7 +58,7 @@ public Value translateToGraal(Context context, ValueTypeNbt.ValueNbt value, IEva
public Value translateTag(Context context, Tag tag, IEvaluationExceptionFactory exceptionFactory, ValueDeseralizationContext valueDeseralizationContext) throws EvaluationException {
switch (tag.getId()) {
case Tag.TAG_END -> {
return context.eval("js", "exports = { 'nbt_end': true }");
return context.asValue(PROXY_END_TAG);
}
case Tag.TAG_BYTE -> {
return context.asValue(((ByteTag) tag).getAsByte());
Expand Down Expand Up @@ -107,16 +112,11 @@ public Value translateCompoundTag(Context context, CompoundTag tag, IEvaluationE
@Override
public ValueTypeNbt.ValueNbt translateFromGraal(Context context, Value value, IEvaluationExceptionFactory exceptionFactory, ValueDeseralizationContext valueDeseralizationContext) throws EvaluationException {
// Unwrap the value if it was translated in the opposite direction before.
if (value.isProxyObject()) {
try {
NbtCompoundTagProxyObject proxy = value.asProxyObject();
return ValueTypeNbt.ValueNbt.of(proxy.getTag());
} catch (ClassCastException classCastException) {
// Fallback to case below
}
if (value.isProxyObject() && value.asProxyObject() instanceof NbtCompoundTagProxyObject proxy) {
return ValueTypeNbt.ValueNbt.of(proxy.getTag());
}

if (value.getMemberKeys().equals(Sets.newHashSet("nbt_end"))) {
if (value.getMemberKeys().equals(Sets.newHashSet(KEY_END_TAG))) {
return ValueTypeNbt.ValueNbt.of(EndTag.INSTANCE);
}

Expand Down
Loading
Loading