-
-
Notifications
You must be signed in to change notification settings - Fork 925
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TL;DR define_method defined methods will execute ~2x faster in common…
… cases. This optimization will convert define_method from being a block and fully promoting it to a method (only for JIT now). Once we unify InterpretedIRMethod and MixedModeIRMethod we can enable this for full builds in -X-C as well. This ended up have more issues than anticipated. block_given? in particular refers to the block outside the define_method block and not to any block passed to that newly defined method. This required the creation of a new Method type DefineMethodMethod. The biggest limitation to this optimization is that if the block for define_method references any variables in a parent scope then we will not perform the optimization. This is mildly undesirable and for read-only lvar access I think we can hoist the value from the parent scope and pass it into our converted method. This seemed like a ton of extra work so it is a blue sky future. There will be a future set of commit(s) to make this optimization trigger more often. Right now if the new method has contained closures which acceess lvars outside of their current scope then we do not perform this opt. The next round of optimization will involve calculating whether nested closures can escape the define_method only. Before: Calculating ------------------------------------- define_method w/ capture 56.284k i/100ms def 104.909k i/100ms define_method 63.224k i/100ms ------------------------------------------------- define_method w/ capture 1.115M (± 5.8%) i/s - 5.572M def 3.409M (± 9.3%) i/s - 16.890M define_method 1.086M (± 4.8%) i/s - 5.437M After: system ~/work/jruby master * 1412% jruby ../snippets/define_method3.rb Calculating ------------------------------------- define_method w/ capture 58.167k i/100ms def 108.644k i/100ms define_method 92.163k i/100ms ------------------------------------------------- define_method w/ capture 1.171M (± 6.4%) i/s - 5.875M def 3.241M (± 8.1%) i/s - 16.079M define_method 2.795M (± 8.7%) i/s - 13.917M MRI: system ~/work/jruby master * 1408% mri22 ../snippets/define_method3.rb Calculating ------------------------------------- define_method w/ capture 46.709k i/100ms def 63.690k i/100ms define_method 46.901k i/100ms ------------------------------------------------- define_method w/ capture 832.886k (± 6.4%) i/s - 4.157M def 1.732M (± 7.8%) i/s - 8.662M define_method 856.637k (± 6.6%) i/s - 4.268M
- 9.4.12.0
- 9.4.11.0
- 9.4.10.0
- 9.4.9.0
- 9.4.8.0
- 9.4.7.0
- 9.4.6.0
- 9.4.5.0
- 9.4.4.0
- 9.4.3.0
- 9.4.2.0
- 9.4.1.0
- 9.4.0.0
- 9.3.15.0
- 9.3.14.0
- 9.3.13.0
- 9.3.12.0
- 9.3.11.0
- 9.3.10.0
- 9.3.9.0
- 9.3.8.0
- 9.3.7.0
- 9.3.6.0
- 9.3.5.0
- 9.3.4.0
- 9.3.3.0
- 9.3.2.0
- 9.3.1.0
- 9.3.0.0
- 9.2.21.0
- 9.2.20.1
- 9.2.20.0
- 9.2.19.0
- 9.2.18.0
- 9.2.17.0
- 9.2.16.0
- 9.2.15.0
- 9.2.14.0
- 9.2.13.0
- 9.2.12.0
- 9.2.11.1
- 9.2.11.0
- 9.2.10.0
- 9.2.9.0
- 9.2.8.0
- 9.2.7.0
- 9.2.6.0
- 9.2.5.0
- 9.2.4.1
- 9.2.4.0
- 9.2.3.0
- 9.2.2.0
- 9.2.1.0
- 9.2.0.0
- 9.1.17.0
- 9.1.16.0
- 9.1.15.0
- 9.1.14.0
- 9.1.13.0
- 9.1.12.0
- 9.1.11.0
- 9.1.10.0
- 9.1.9.0
- 9.1.8.0
- 9.1.7.0
- 9.1.6.0
- 9.1.5.0
- 9.1.4.0
- 9.1.3.0
- 9.1.2.0
- 9.1.1.0
- 9.1.0.0
- 9.0.5.0
- 9.0.4.0
- 9.0.3.0
Showing
11 changed files
with
178 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.jruby.ast; | ||
|
||
import org.jruby.parser.StaticScope; | ||
|
||
/** | ||
* Methods and blocks both implement these. | ||
*/ | ||
public interface DefNode { | ||
/** | ||
* Gets the argsNode. | ||
* @return Returns a Node | ||
*/ | ||
ArgsNode getArgsNode(); | ||
|
||
/** | ||
* Get the static scoping information. | ||
* | ||
* @return the scoping info | ||
*/ | ||
StaticScope getScope(); | ||
|
||
/** | ||
* Gets the body of this class. | ||
* | ||
* @return the contents | ||
*/ | ||
Node getBodyNode(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
core/src/main/java/org/jruby/internal/runtime/methods/DefineMethodMethod.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package org.jruby.internal.runtime.methods; | ||
|
||
import org.jruby.RubyModule; | ||
import org.jruby.ir.IRScope; | ||
import org.jruby.runtime.Block; | ||
import org.jruby.runtime.ThreadContext; | ||
import org.jruby.runtime.Visibility; | ||
import org.jruby.runtime.builtin.IRubyObject; | ||
|
||
/** | ||
* Supports optimization for define_method. block that defined_method sees happens to be the block | ||
* passed to the scope where the define_method originally was called...not the block potentially passed to | ||
* the newly defined method. | ||
*/ | ||
public class DefineMethodMethod extends MixedModeIRMethod { | ||
|
||
private Block capturedBlock; | ||
|
||
public DefineMethodMethod(IRScope method, Visibility visibility, RubyModule implementationClass, Block capturedBlock) { | ||
super(method, visibility, implementationClass); | ||
|
||
this.capturedBlock = capturedBlock; | ||
} | ||
|
||
@Override | ||
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject[] args, Block block) { | ||
return super.call(context, self, clazz, name, args, capturedBlock); | ||
} | ||
|
||
@Override | ||
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, Block block) { | ||
return super.call(context, self, clazz, name, capturedBlock); | ||
} | ||
|
||
@Override | ||
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject arg0, Block block) { | ||
return super.call(context, self, clazz, name, arg0, capturedBlock); | ||
} | ||
|
||
@Override | ||
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject arg0, IRubyObject arg1, Block block) { | ||
return super.call(context, self, clazz, name, arg0, arg1, capturedBlock); | ||
} | ||
|
||
@Override | ||
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject arg0, IRubyObject arg1, IRubyObject arg2, Block block) { | ||
return super.call(context, self, clazz, name, arg0, arg1, arg2, capturedBlock); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters