diff options
author | Jason Zaugg <jzaugg@gmail.com> | 2015-10-29 16:49:22 +1000 |
---|---|---|
committer | Jason Zaugg <jzaugg@gmail.com> | 2016-01-29 16:16:16 +1000 |
commit | df0d105f90816960b711c35a3289ff460295d1cc (patch) | |
tree | a478b92f5ae83d308a97f7f7ccd60c5e38b339ac /src/library/scala/runtime/LambdaDeserialize.java | |
parent | 8a761a313bfe3d79afefed1622b69a850636d732 (diff) | |
download | scala-df0d105f90816960b711c35a3289ff460295d1cc.tar.gz scala-df0d105f90816960b711c35a3289ff460295d1cc.tar.bz2 scala-df0d105f90816960b711c35a3289ff460295d1cc.zip |
Use invokedynamic for structural calls, symbol literals, lamba ser.
The previous encodings created static fields in the enclosing class
to host caches. However, this isn't an option once emit code in default
methods of interfaces, as Java interfaces don't allow private static
fields.
We could continue to emit fields, and make them public when added to
traits.
Or, as chosen in this commit, we can emulate a call-site specific
static field by using invokedynamic: when the call site is linked,
our bootstrap methid can perform one-time computation, and we can
capture the result in the CallSite.
To implement this, I've allowed encoding of arbitrary invokedynamic
calls in ApplyDynamic.
The encoding is:
ApplyDynamic(
NoSymbol.newTermSymbol(TermName("methodName")).setInfo(invokedType)
Literal(Constant(bootstrapMethodSymbol)) :: (
Literal(Constant(staticArg0)) :: Literal(Constant(staticArgN)) :: Nil
) :::
(dynArg0 :: dynArgN :: Nil)
)
So far, static args may be `MethodType`, numeric or string literals, or
method symbols, all of which can be converted to constant pool entries.
`MethodTypes` are transformed to the erased JVM type and are converted
to descriptors as String constants.
I've taken advantage of this for symbol literal caching and
for the structural call site cache.
I've also included a test case that shows how a macro could target this
(albeit using private APIs) to cache compiled regexes.
I haven't managed to use this for LambdaMetafactory yet, not sure
if the facility is general enough.
Diffstat (limited to 'src/library/scala/runtime/LambdaDeserialize.java')
-rw-r--r-- | src/library/scala/runtime/LambdaDeserialize.java | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/library/scala/runtime/LambdaDeserialize.java b/src/library/scala/runtime/LambdaDeserialize.java new file mode 100644 index 0000000000..e239debf25 --- /dev/null +++ b/src/library/scala/runtime/LambdaDeserialize.java @@ -0,0 +1,29 @@ +package scala.runtime; + + +import java.lang.invoke.*; +import java.util.Arrays; +import java.util.HashMap; + +public final class LambdaDeserialize { + + private MethodHandles.Lookup lookup; + private final HashMap<String, MethodHandle> cache = new HashMap<>(); + private final LambdaDeserializer$ l = LambdaDeserializer$.MODULE$; + + private LambdaDeserialize(MethodHandles.Lookup lookup) { + this.lookup = lookup; + } + + public Object deserializeLambda(SerializedLambda serialized) { + return l.deserializeLambda(lookup, cache, serialized); + } + + public static CallSite bootstrap(MethodHandles.Lookup lookup, String invokedName, + MethodType invokedType) throws Throwable { + MethodType type = MethodType.fromMethodDescriptorString("(Ljava/lang/invoke/SerializedLambda;)Ljava/lang/Object;", lookup.getClass().getClassLoader()); + MethodHandle deserializeLambda = lookup.findVirtual(LambdaDeserialize.class, "deserializeLambda", type); + MethodHandle exact = deserializeLambda.bindTo(new LambdaDeserialize(lookup)).asType(invokedType); + return new ConstantCallSite(exact); + } +} |