Skip to content

Commit

Permalink
Optimize do not try to unpack scoped values if method scopes are disa…
Browse files Browse the repository at this point in the history
…bled on the fast-path.
  • Loading branch information
chumer committed Sep 3, 2021
1 parent d730aac commit d33989b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -714,9 +714,14 @@ public boolean allowsImplementation(HostAccess access, Class<?> type) {
return access.allowsImplementation(type);
}

@Override
public boolean isMethodScopingEnabled(HostAccess access) {
return access.isMethodScopingEnabled();
}

@Override
public boolean isMethodScoped(HostAccess access, Executable e) {
return access.getMethodScopingClosure().test(e);
return access.isMethodScoped(e);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ public final class HostAccess {
private final boolean methodScopingDefault;
private final EconomicSet<Class<? extends Annotation>> disableMethodScopingAnnotations;
private final EconomicSet<Executable> disableMethodScoping;
private Predicate<Executable> methodScopingClosure;
volatile Object impl;

private static final HostAccess EMPTY = new HostAccess(null, null, null, null, null, null, null, false, false, false, false, false, false, false, false, false, false, null, null);
Expand Down Expand Up @@ -437,30 +436,27 @@ boolean allowsAccess(AnnotatedElement member) {
return false;
}

Predicate<Executable> getMethodScopingClosure() {
if (methodScopingClosure == null) {
methodScopingClosure = new Predicate<Executable>() {
public boolean test(Executable e) {
if (!methodScopingDefault) {
return false;
}
if (disableMethodScoping != null) {
if (disableMethodScoping.contains(e)) {
return false;
}
}
if (disableMethodScopingAnnotations != null) {
for (Class<? extends Annotation> ann : disableMethodScopingAnnotations) {
if (e.getAnnotation(ann) != null) {
return false;
}
}
}
return true;
boolean isMethodScoped(Executable e) {
if (!isMethodScopingEnabled()) {
return false;
}
if (disableMethodScoping != null) {
if (disableMethodScoping.contains(e)) {
return false;
}
}
if (disableMethodScopingAnnotations != null) {
for (Class<? extends Annotation> ann : disableMethodScopingAnnotations) {
if (e.getAnnotation(ann) != null) {
return false;
}
};
}
}
return methodScopingClosure;
return true;
}

boolean isMethodScopingEnabled() {
return methodScopingDefault;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ protected APIAccess() {

public abstract boolean allowsImplementation(HostAccess access, Class<?> type);

public abstract boolean isMethodScopingEnabled(HostAccess access);

public abstract boolean isMethodScoped(HostAccess access, Executable e);

public abstract boolean isArrayAccessible(HostAccess access);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ public void testStoreAndPin() {
test.invokeMember("storeValueAndPin", test);
assertTrue(o.value.isHostObject());

// host object
test.invokeMember("storeValueAndPin", ProxyObject.fromMap(new HashMap<>()));
assertTrue(o.value.isProxyObject());

test.invokeMember("storeMapAndPin", proxy);
// maps can be pinned too
assertEquals("42", o.map.get("cafe"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@

import com.oracle.truffle.api.TruffleFile;
import com.oracle.truffle.api.TruffleOptions;
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.host.HostAdapterFactory.AdapterResult;
Expand All @@ -61,6 +62,8 @@ public class HostLanguageService extends AbstractHostService {

final HostLanguage language;

@CompilationFinal private boolean methodScopingEnabled;

HostLanguageService(AbstractPolyglotImpl polyglot, HostLanguage language) {
super(polyglot);
this.language = language;
Expand All @@ -75,6 +78,7 @@ public void initializeHostContext(Object internalContext, Object receiver, HostA
}
language.initializeHostAccess(hostAccess, useCl);
context.initialize(internalContext, useCl, clFilter, hostCLAllowed, hostLookupAllowed);
this.methodScopingEnabled = language.api.isMethodScopingEnabled(hostAccess);
}

@Override
Expand Down Expand Up @@ -213,7 +217,10 @@ public boolean isHostProxy(Object value) {
return HostProxy.isProxyGuestObject(obj);
}

private static Object unwrapIfScoped(Object obj) {
private Object unwrapIfScoped(Object obj) {
if (!methodScopingEnabled) {
return obj;
}
Object o = obj;
if (o instanceof ScopedObject) {
o = ((ScopedObject) o).unwrapForGuest();
Expand Down

0 comments on commit d33989b

Please sign in to comment.