From ca1b79afd914a5f55b2a9d5fc17a721ddd3c6f6c Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Wed, 24 May 2023 16:43:50 +0200 Subject: [PATCH] Share single InteropLibrary among all resolveInterop calls --- .../expression/builtin/meta/TypeOfNode.java | 82 ++++++++++++------- 1 file changed, 51 insertions(+), 31 deletions(-) diff --git a/engine/runtime/src/main/java/org/enso/interpreter/node/expression/builtin/meta/TypeOfNode.java b/engine/runtime/src/main/java/org/enso/interpreter/node/expression/builtin/meta/TypeOfNode.java index 7d47ba79f841e..a9438ecdd8877 100644 --- a/engine/runtime/src/main/java/org/enso/interpreter/node/expression/builtin/meta/TypeOfNode.java +++ b/engine/runtime/src/main/java/org/enso/interpreter/node/expression/builtin/meta/TypeOfNode.java @@ -1,14 +1,5 @@ package org.enso.interpreter.node.expression.builtin.meta; -import com.oracle.truffle.api.CompilerDirectives; -import com.oracle.truffle.api.dsl.Cached; -import com.oracle.truffle.api.dsl.Fallback; -import com.oracle.truffle.api.dsl.GenerateUncached; -import com.oracle.truffle.api.dsl.Specialization; -import com.oracle.truffle.api.interop.InteropLibrary; -import com.oracle.truffle.api.interop.UnsupportedMessageException; -import com.oracle.truffle.api.library.CachedLibrary; -import com.oracle.truffle.api.nodes.Node; import org.enso.interpreter.dsl.AcceptsError; import org.enso.interpreter.dsl.BuiltinMethod; import org.enso.interpreter.runtime.EnsoContext; @@ -22,6 +13,17 @@ import org.enso.interpreter.runtime.library.dispatch.TypesLibrary; import org.enso.interpreter.runtime.number.EnsoBigInteger; +import com.oracle.truffle.api.CompilerDirectives; +import com.oracle.truffle.api.dsl.Cached; +import com.oracle.truffle.api.dsl.Cached.Shared; +import com.oracle.truffle.api.dsl.Fallback; +import com.oracle.truffle.api.dsl.GenerateUncached; +import com.oracle.truffle.api.dsl.Specialization; +import com.oracle.truffle.api.interop.InteropLibrary; +import com.oracle.truffle.api.interop.UnsupportedMessageException; +import com.oracle.truffle.api.library.CachedLibrary; +import com.oracle.truffle.api.nodes.Node; + @BuiltinMethod( type = "Meta", name = "type_of", @@ -108,18 +110,24 @@ Object doAny(Object value) { abstract static class WithoutType extends Node { abstract Object execute(Object value); - @Specialization(guards = {"findInterop(value, interop).isArray()"}) - Type doPolyglotArray(Object value, @CachedLibrary(limit = "3") InteropLibrary interop) { + @Specialization(guards = {"resolveInterop(value, interop).isArray()"}) + Type doPolyglotArray( + Object value, + @CachedLibrary(limit = "3") @Shared("resolveInterop") InteropLibrary interop) { return EnsoContext.get(this).getBuiltins().array(); } - @Specialization(guards = {"findInterop(value, interop).isString()"}) - Type doPolyglotString(Object value, @CachedLibrary(limit = "3") InteropLibrary interop) { + @Specialization(guards = {"resolveInterop(value, interop).isString()"}) + Type doPolyglotString( + Object value, + @CachedLibrary(limit = "3") @Shared("resolveInterop") InteropLibrary interop) { return EnsoContext.get(this).getBuiltins().text(); } - @Specialization(guards = {"findInterop(value, interop).isNumber()"}) - Type doPolyglotNumber(Object value, @CachedLibrary(limit = "3") InteropLibrary interop) { + @Specialization(guards = {"resolveInterop(value, interop).isNumber()"}) + Type doPolyglotNumber( + Object value, + @CachedLibrary(limit = "3") @Shared("resolveInterop") InteropLibrary interop) { Builtins builtins = EnsoContext.get(this).getBuiltins(); if (interop.fitsInInt(value)) { return builtins.number().getInteger(); @@ -130,37 +138,49 @@ Type doPolyglotNumber(Object value, @CachedLibrary(limit = "3") InteropLibrary i } } - @Specialization(guards = {"findInterop(value, interop).isDateTime()"}) - Type doDateTime(Object value, @CachedLibrary(limit = "3") InteropLibrary interop) { + @Specialization(guards = {"resolveInterop(value, interop).isDateTime()"}) + Type doDateTime( + Object value, + @CachedLibrary(limit = "3") @Shared("resolveInterop") InteropLibrary interop) { return EnsoContext.get(this).getBuiltins().dateTime(); } - @Specialization(guards = {"findInterop(value, interop).isTimeZone()"}) - Type doTimeZone(Object value, @CachedLibrary(limit = "3") InteropLibrary interop) { + @Specialization(guards = {"resolveInterop(value, interop).isTimeZone()"}) + Type doTimeZone( + Object value, + @CachedLibrary(limit = "3") @Shared("resolveInterop") InteropLibrary interop) { EnsoContext ctx = EnsoContext.get(this); return ctx.getBuiltins().timeZone(); } - @Specialization(guards = {"findInterop(value, interop).isDate()"}) - Type doDate(Object value, @CachedLibrary(limit = "3") InteropLibrary interop) { + @Specialization(guards = {"resolveInterop(value, interop).isDate()"}) + Type doDate( + Object value, + @CachedLibrary(limit = "3") @Shared("resolveInterop") InteropLibrary interop) { EnsoContext ctx = EnsoContext.get(this); return ctx.getBuiltins().date(); } - @Specialization(guards = {"findInterop(value, interop).isTime()"}) - Type doTime(Object value, @CachedLibrary(limit = "3") InteropLibrary interop) { + @Specialization(guards = {"resolveInterop(value, interop).isTime()"}) + Type doTime( + Object value, + @CachedLibrary(limit = "3") @Shared("resolveInterop") InteropLibrary interop) { EnsoContext ctx = EnsoContext.get(this); return ctx.getBuiltins().timeOfDay(); } - @Specialization(guards = "findInterop(value, interop).isDuration()") - Type doDuration(Object value, @CachedLibrary(limit = "3") InteropLibrary interop) { + @Specialization(guards = "resolveInterop(value, interop).isDuration()") + Type doDuration( + Object value, + @CachedLibrary(limit = "3") @Shared("resolveInterop") InteropLibrary interop) { EnsoContext ctx = EnsoContext.get(this); return ctx.getBuiltins().duration(); } - @Specialization(guards = {"findInterop(value, interop).isMetaObject()"}) - Object doMetaObject(Object value, @CachedLibrary(limit = "3") InteropLibrary interop) { + @Specialization(guards = {"resolveInterop(value, interop).isMetaObject()"}) + Object doMetaObject( + Object value, + @CachedLibrary(limit = "3") @Shared("resolveInterop") InteropLibrary interop) { try { return interop.getMetaObject(value); } catch (UnsupportedMessageException e) { @@ -181,11 +201,11 @@ Object doAny(Object value) { this); } - static InteropType findInterop(Object value, InteropLibrary interop) { - return InteropType.find(value, interop); + static Interop resolveInterop(Object value, InteropLibrary interop) { + return Interop.resolve(value, interop); } - enum InteropType { + enum Interop { NONE, STRING, NUMBER, @@ -197,7 +217,7 @@ enum InteropType { DURATION, META_OBJECT; - static InteropType find(Object value, InteropLibrary interop) { + static Interop resolve(Object value, InteropLibrary interop) { if (interop.isString(value)) { return STRING; }