pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/jruby/jruby/pull/8862

/assets/global-0bd78641c0a1f3e0.css" /> respond_to?(<symbol>) => true optimization by headius · Pull Request #8862 · jruby/jruby · GitHub
Skip to content

respond_to?(<symbol>) => true optimization#8862

Draft
headius wants to merge 9 commits intojruby:masterfrom
headius:respond_to_optz
Draft

respond_to?(<symbol>) => true optimization#8862
headius wants to merge 9 commits intojruby:masterfrom
headius:respond_to_optz

Conversation

@headius
Copy link
Copy Markdown
Member

@headius headius commented Jun 7, 2025

This optimizes the common case of respond_to? with a literal symbol. When the method is "naturally" respond_to? => true then that result can be cached behind the usual guards. This makes the success path essentially free.

The logic here has a lot of copied behavior from elsewhere and utilizes some now-static methods from InvokeSite, so it could be refactored along with the rest of indy logic to be much simpler.

@headius headius added this to the JRuby 10.0.1.0 milestone Jun 7, 2025
@headius
Copy link
Copy Markdown
Member Author

headius commented Jun 7, 2025

This could be expanded to make false results also constant by additionally checking if respond_to_missing? is the built-in version (which has no special logic). In that case, the method is "naturally" undefined, and without either it being defined or a respond_to_missing? being added, that would not change.

@headius headius force-pushed the respond_to_optz branch 2 times, most recently from 939f4c1 to d368cf1 Compare June 9, 2025 00:43
@headius
Copy link
Copy Markdown
Member Author

headius commented Jun 9, 2025

This needs some refactoring to work properly. I hack one fix in and break something else. On pause for the moment until I can get some time to rework indy call sites for more composability.

@headius headius marked this pull request as draft June 9, 2025 08:53
@headius headius modified the milestones: JRuby 10.0.2.0, JRuby 10.0.3.0 Aug 6, 2025
@headius headius marked this pull request as ready for review October 29, 2025 18:53
This optimizes the common case of respond_to? with a literal
symbol. When the method is "naturally" respond_to? => true then
that result can be cached behind the usual guards. This makes the
success path essentially free.

The logic here has a lot of copied behavior from elsewhere and
utilizes some now-static methods from InvokeSite, so it could be
refactored along with the rest of indy logic to be much simpler.

There's a lot of duplicated code here from other call site forms,
demonstrating the need for a refactor of all indy binding logic.
We can cache a true result only if respond_to? is the built-in
version and it would return true. We can cache a false result if
respond_to? is built-in and returns false and respond_to_missing?
is not define or both are undefined. All other cases must dispatch
through slow-path logic (normal dynamic invocation of respond_to?).
@headius headius marked this pull request as draft October 30, 2025 07:48
@headius
Copy link
Copy Markdown
Member Author

headius commented Oct 30, 2025

Back to draft with recent refactoring and some missing safety guards like preventing it from binding every class it ever sees:

$ jruby -w -J-XX:+UnlockDiagnosticVMOptions -J-XX:+ShowHiddenFrames -e "loop { Class.new.new.respond_to?(:foo) }"
Error: Your application used more stack memory than the safety cap of 2048K.
Specify -J-Xss####k to increase it (#### = cap size in KB).
java.lang.StackOverflowError
	at java.base/java.util.Objects.equals(Objects.java:62)
	at java.base/jdk.internal.util.StrongReferenceKey.equals(StrongReferenceKey.java:69)
	at java.base/java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:952)
	at java.base/jdk.internal.util.ReferencedKeyMap.getNoCheckStale(ReferencedKeyMap.java:208)
	at java.base/jdk.internal.util.ReferencedKeyMap.get(ReferencedKeyMap.java:201)
	at java.base/jdk.internal.util.ReferencedKeySet.get(ReferencedKeySet.java:164)
	at java.base/java.lang.invoke.MethodType.makeImpl(MethodType.java:408)
	at java.base/java.lang.invoke.MethodType.methodType(MethodType.java:378)
	at java.base/java.lang.invoke.MethodType.methodType(MethodType.java:248)
	at org.jruby.dist/com.headius.invokebinder.transform.Permute.down(Permute.java:54)
	at org.jruby.dist/com.headius.invokebinder.Binder.add(Binder.java:305)
	at org.jruby.dist/com.headius.invokebinder.Binder.<init>(Binder.java:136)
	at org.jruby.dist/com.headius.invokebinder.Binder.permute(Binder.java:1142)
	at org.jruby.dist/com.headius.invokebinder.SmartBinder.permute(SmartBinder.java:297)
	at org.jruby.dist/com.headius.invokebinder.SmartBinder.permute(SmartBinder.java:311)
	at org.jruby.dist/org.jruby.ir.targets.indy.InvokeSite.testTarget(InvokeSite.java:1478)
	at org.jruby.dist/org.jruby.ir.targets.indy.RespondToSite.typeCheck(RespondToSite.java:202)
	at org.jruby.dist/org.jruby.ir.targets.indy.RespondToSite.guardAndSetTarget(RespondToSite.java:113)
	at org.jruby.dist/org.jruby.ir.targets.indy.RespondToSite.respondToFallback(RespondToSite.java:103)
	at java.base/java.lang.invoke.DirectMethodHandle$Holder.invokeVirtual(DirectMethodHandle$Holder)
	at java.base/java.lang.invoke.LambdaForm$MH/0x0000380003038800.invoke(LambdaForm$MH)
	at java.base/java.lang.invoke.DelegatingMethodHandle$Holder.delegate(DelegatingMethodHandle$Holder)
	at java.base/java.lang.invoke.LambdaForm$MH/0x0000380003032000.guard(LambdaForm$MH)
	at java.base/java.lang.invoke.DelegatingMethodHandle$Holder.delegate(DelegatingMethodHandle$Holder)
	at java.base/java.lang.invoke.LambdaForm$MH/0x0000380003032000.guard(LambdaForm$MH)
	at java.base/java.lang.invoke.DelegatingMethodHandle$Holder.delegate(DelegatingMethodHandle$Holder)
	at java.base/java.lang.invoke.LambdaForm$MH/0x0000380003032000.guard(LambdaForm$MH)
	at java.base/java.lang.invoke.DelegatingMethodHandle$Holder.delegate(DelegatingMethodHandle$Holder)
	at java.base/java.lang.invoke.LambdaForm$MH/0x0000380003032000.guard(LambdaForm$MH)
	at java.base/java.lang.invoke.DelegatingMethodHandle$Holder.delegate(DelegatingMethodHandle$Holder)
	at java.base/java.lang.invoke.LambdaForm$MH/0x0000380003032000.guard(LambdaForm$MH)
...

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

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy