Skip to content

Commit

Permalink
Cache resolution of symbol for Any
Browse files Browse the repository at this point in the history
Otherwise things can go horribly slow.
Closes #6523.
  • Loading branch information
hubertp committed May 4, 2023
1 parent 7f9ed71 commit 317ef5c
Showing 1 changed file with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ Object doFunctionalDispatch(
Object self,
Object[] arguments,
@CachedLibrary(limit = "10") TypesLibrary typesLibrary,
@Cached MethodResolverNode methodResolverNode) {
@Cached MethodResolverNode methodResolverNode,
@Cached("anyFunction(symbol)") Function anyFun) {

Type selfTpe = typesLibrary.getType(self);
Function function = methodResolverNode.expectNonNull(self, selfTpe, symbol);
Expand All @@ -122,22 +123,25 @@ Object doFunctionalDispatch(
// and the method is invoked statically, i.e. type of self is the eigentype,
// then we want to disambiguate method resolution by always resolved to the one in Any.
if (where instanceof MethodRootNode node && typeCanOverride(node, EnsoContext.get(this))) {
Function anyFun = symbol.getScope().lookupMethodDefinition(EnsoContext.get(this).getBuiltins().any(), symbol.getName());
if (anyFun != null) {
function = anyFun;
}
}
return invokeFunctionNode.execute(function, frame, state, arguments);
}

Function anyFunction(UnresolvedSymbol symbol) {
Type any = EnsoContext.get(this).getBuiltins().any();
return symbol.getScope().lookupMethodDefinition(any, symbol.getName());
}

private boolean typeCanOverride(MethodRootNode node, EnsoContext ctx) {
Type methodOwnerType = node.getType();
Builtins builtins = ctx.getBuiltins();
Type any = builtins.any();
Type warning = builtins.warning();
Type panic = builtins.panic();
return methodOwnerType.isEigenType()

&& builtins.nothing() != methodOwnerType
&& any.getEigentype() != methodOwnerType
&& panic.getEigentype() != methodOwnerType
Expand Down

0 comments on commit 317ef5c

Please sign in to comment.