Skip to content

Permute indy invoke paths to reduce uncached stack#8410

Closed
headius wants to merge 1 commit intojruby:9.5-devfrom
headius:permute_indy_invokes
Closed

Permute indy invoke paths to reduce uncached stack#8410
headius wants to merge 1 commit intojruby:9.5-devfrom
headius:permute_indy_invokes

Conversation

@headius
Copy link
Member

@headius headius commented Nov 5, 2024

Passing all uncached calls through the varargs form simplifies maintenance, but requires an indirect dispatch for the first invocation. This adds several frames to the stack for such uncached calls. The patch here permutes the InvokeSite.invoke methods so they can directly dispatch to the to-be-cached handle, reducing the number of stack frames in a backtrace.

This is unlikely to reduce actual stack consumption due to the unoptimized MethodHandle and LambdaForm between the invokeExact call and the target method.

Example trace before and after:

BEFORE:

	at DashE.RUBY$method$baz$3(-e:1)
	at DashE.RUBY$method$baz$3$__VARARGS__(-e:1)
	at org.jruby.dist/org.jruby.internal.runtime.methods.CompiledIRMethod.call(CompiledIRMethod.java:139)
	at org.jruby.dist/org.jruby.ir.targets.indy.InvokeSite.performIndirectCall(InvokeSite.java:735)
	at org.jruby.dist/org.jruby.ir.targets.indy.InvokeSite.invoke(InvokeSite.java:680)
	at DashE.RUBY$method$bar$2(-e:1)
	at DashE.RUBY$method$bar$2$__VARARGS__(-e:1)
	at org.jruby.dist/org.jruby.internal.runtime.methods.CompiledIRMethod.call(CompiledIRMethod.java:139)
	at org.jruby.dist/org.jruby.ir.targets.indy.InvokeSite.performIndirectCall(InvokeSite.java:735)
	at org.jruby.dist/org.jruby.ir.targets.indy.InvokeSite.invoke(InvokeSite.java:680)
	at DashE.RUBY$method$foo$1(-e:1)
	at DashE.RUBY$method$foo$1$__VARARGS__(-e:1)
	at org.jruby.dist/org.jruby.internal.runtime.methods.CompiledIRMethod.call(CompiledIRMethod.java:139)
	at org.jruby.dist/org.jruby.ir.targets.indy.InvokeSite.performIndirectCall(InvokeSite.java:735)
	at org.jruby.dist/org.jruby.ir.targets.indy.InvokeSite.invoke(InvokeSite.java:680)
	at DashE.RUBY$block$\=\^main\_$0(-e:1)

AFTER:

	at DashE.️❤ def baz #3(-e:1)
	at org.jruby.dist/org.jruby.ir.targets.indy.InvokeSite.invoke(InvokeSite.java:709)
	at DashE.️❤ def bar #2(-e:1)
	at org.jruby.dist/org.jruby.ir.targets.indy.InvokeSite.invoke(InvokeSite.java:709)
	at DashE.️❤ def foo #1(-e:1)
	at org.jruby.dist/org.jruby.ir.targets.indy.InvokeSite.invoke(InvokeSite.java:709)
	at DashE.️❤ {} \=\^main\_ #0(-e:1)

Passing all uncached calls through the varargs form simplifies
maintenance, but requires an indirect dispatch for the first
invocation. This adds several frames to the stack for such uncached
calls. The patch here permutes the InvokeSite.invoke methods so
they can directly dispatch to the to-be-cached handle, reducing the
number of stack frames in a backtrace.

This is unlikely to reduce actual stack consumption due to the
unoptimized MethodHandle and LambdaForm between the invokeExact
call and the target method.
@headius headius added this to the JRuby 10.0.0.0 milestone Nov 5, 2024
@headius
Copy link
Member Author

headius commented Nov 5, 2024

Thread dump showing actual stack used for the more direct form (AFTER):

	at DashE.️❤ def baz #3(-e:1)
	at java.lang.invoke.DirectMethodHandle$Holder.invokeStatic(java.base@21/DirectMethodHandle$Holder)
	at java.lang.invoke.LambdaForm$MH/0x000000c0015ed000.invoke(java.base@21/LambdaForm$MH)
	at java.lang.invoke.Invokers$Holder.invokeExact_MT(java.base@21/Invokers$Holder)
	at org.jruby.ir.targets.indy.InvokeSite.invoke(org.jruby.dist/InvokeSite.java:709)
	at java.lang.invoke.LambdaForm$DMH/0x000000c0015e1c00.invokeVirtual(java.base@21/LambdaForm$DMH)
	at java.lang.invoke.LambdaForm$MH/0x000000c0015ec000.invoke(java.base@21/LambdaForm$MH)
	at java.lang.invoke.Invokers$Holder.linkToCallSite(java.base@21/Invokers$Holder)
	at DashE.️❤ def bar #2(-e:1)
	at java.lang.invoke.DirectMethodHandle$Holder.invokeStatic(java.base@21/DirectMethodHandle$Holder)
	at java.lang.invoke.LambdaForm$MH/0x000000c0015ed000.invoke(java.base@21/LambdaForm$MH)
	at java.lang.invoke.Invokers$Holder.invokeExact_MT(java.base@21/Invokers$Holder)
	at org.jruby.ir.targets.indy.InvokeSite.invoke(org.jruby.dist/InvokeSite.java:709)
	at java.lang.invoke.LambdaForm$DMH/0x000000c0015e1c00.invokeVirtual(java.base@21/LambdaForm$DMH)
	at java.lang.invoke.LambdaForm$MH/0x000000c0015ec000.invoke(java.base@21/LambdaForm$MH)
	at java.lang.invoke.Invokers$Holder.linkToCallSite(java.base@21/Invokers$Holder)
	at DashE.️❤ def foo #1(-e:1)
	at java.lang.invoke.DirectMethodHandle$Holder.invokeStatic(java.base@21/DirectMethodHandle$Holder)
	at java.lang.invoke.LambdaForm$MH/0x000000c0015ed000.invoke(java.base@21/LambdaForm$MH)
	at java.lang.invoke.Invokers$Holder.invokeExact_MT(java.base@21/Invokers$Holder)
	at org.jruby.ir.targets.indy.InvokeSite.invoke(org.jruby.dist/InvokeSite.java:709)
	at java.lang.invoke.LambdaForm$DMH/0x000000c0015e1c00.invokeVirtual(java.base@21/LambdaForm$DMH)
	at java.lang.invoke.LambdaForm$MH/0x000000c0015ec000.invoke(java.base@21/LambdaForm$MH)
	at java.lang.invoke.Invokers$Holder.linkToCallSite(java.base@21/Invokers$Holder)
	at DashE.️❤ {} \=\^main\_ #0(-e:1)

As described above, the MethodHandle guts are actually on the stack consuming stack space, so the change in this PR is mostly cosmetic affecting only user-facing stack traces.

@headius
Copy link
Member Author

headius commented Nov 5, 2024

Still some missing adaptation for SelfInvokeSite and others may be affected:

Caused by:
java.lang.NoSuchMethodException: no such method: org.jruby.ir.targets.indy.SelfInvokeSite.invoke(ThreadContext,IRubyObject,IRubyObject[])IRubyObject/invokeVirtual
  makeAccessException at java/lang/invoke/MemberName.java:915
        resolveOrFail at java/lang/invoke/MemberName.java:994
        resolveOrFail at java/lang/invoke/MethodHandles.java:3750
          findVirtual at java/lang/invoke/MethodHandles.java:2767
        invokeVirtual at com/headius/invokebinder/Binder.java:1521
   invokeVirtualQuiet at com/headius/invokebinder/Binder.java:1550
               <init> at org/jruby/ir/targets/indy/InvokeSite.java:629
               <init> at org/jruby/ir/targets/indy/SelfInvokeSite.java:25
            bootstrap at org/jruby/ir/targets/indy/SelfInvokeSite.java:44
               invoke at java/lang/invoke/BootstrapMethodInvoker.java:168
             makeSite at java/lang/invoke/CallSite.java:316
     linkCallSiteImpl at java/lang/invoke/MethodHandleNatives.java:274
         linkCallSite at java/lang/invoke/MethodHandleNatives.java:264
            ️❤ script at -e:1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant