-
Notifications
You must be signed in to change notification settings - Fork 914
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8172 from headius/optimized_method_callee
Optimized __method__ and __callee__
- Loading branch information
Showing
20 changed files
with
337 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require 'benchmark/ips' | ||
|
||
def foo_method | ||
__method__ | ||
end | ||
|
||
def foo_callee | ||
__callee__ | ||
end | ||
|
||
alias bar_method foo_method | ||
alias bar_callee foo_callee | ||
|
||
Benchmark.ips do |bm| | ||
bm.report("__method__ same") do |i| | ||
while i > 0 | ||
i-=1 | ||
foo_method;foo_method;foo_method;foo_method;foo_method;foo_method;foo_method;foo_method;foo_method;foo_method | ||
end | ||
end | ||
|
||
bm.report("__method__ different") do |i| | ||
while i > 0 | ||
i-=1 | ||
bar_method;bar_method;bar_method;bar_method;bar_method;bar_method;bar_method;bar_method;bar_method;bar_method | ||
end | ||
end | ||
|
||
|
||
bm.report("__callee__ same") do |i| | ||
while i > 0 | ||
i-=1 | ||
foo_callee;foo_callee;foo_callee;foo_callee;foo_callee;foo_callee;foo_callee;foo_callee;foo_callee;foo_callee | ||
end | ||
end | ||
|
||
bm.report("__callee__ different") do |i| | ||
while i > 0 | ||
i-=1 | ||
bar_callee;bar_callee;bar_callee;bar_callee;bar_callee;bar_callee;bar_callee;bar_callee;bar_callee;bar_callee | ||
end | ||
end | ||
end |
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
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
70 changes: 70 additions & 0 deletions
70
core/src/main/java/org/jruby/ir/instructions/FrameNameCallInstr.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,70 @@ | ||
package org.jruby.ir.instructions; | ||
|
||
import org.jruby.RubySymbol; | ||
import org.jruby.ir.IRVisitor; | ||
import org.jruby.ir.Operation; | ||
import org.jruby.ir.operands.Variable; | ||
import org.jruby.ir.persistence.IRReaderDecoder; | ||
import org.jruby.ir.persistence.IRWriterEncoder; | ||
import org.jruby.ir.transformations.inlining.CloneInfo; | ||
import org.jruby.runtime.ThreadContext; | ||
import org.jruby.runtime.builtin.IRubyObject; | ||
import org.jruby.runtime.callsite.CacheEntry; | ||
import org.jruby.runtime.callsite.VariableCachingCallSite; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* A call to __method__ or __callee__ which can be optimized to use the frame method name directly. | ||
*/ | ||
public class FrameNameCallInstr extends NoOperandResultBaseInstr implements FixedArityInstr { | ||
private final VariableCachingCallSite frameNameSite; | ||
private final boolean callee; | ||
|
||
public FrameNameCallInstr(Variable result, String methodName) { | ||
super(Operation.FRAME_NAME_CALL, Objects.requireNonNull(result, "FrameNameCallInstr result is null")); | ||
|
||
this.frameNameSite = | ||
new VariableCachingCallSite(Objects.requireNonNull(methodName, "FrameNameCallInstr methodName is null")); | ||
|
||
this.callee = methodName.equals("__callee__"); | ||
} | ||
|
||
public String getMethodName() { | ||
return frameNameSite.getMethodName(); | ||
} | ||
|
||
@Override | ||
public Instr clone(CloneInfo ii) { | ||
return new FrameNameCallInstr(ii.getRenamedVariable(result), frameNameSite.getMethodName()); | ||
} | ||
|
||
public static FrameNameCallInstr decode(IRReaderDecoder d) { | ||
return new FrameNameCallInstr(d.decodeVariable(), d.decodeString()); | ||
} | ||
|
||
@Override | ||
public void encode(IRWriterEncoder e) { | ||
super.encode(e); | ||
e.encode(frameNameSite.getMethodName()); | ||
} | ||
|
||
public IRubyObject getFrameName(ThreadContext context, IRubyObject self, String compositeName) { | ||
CacheEntry frameNameEntry = frameNameSite.retrieveCache(self); | ||
|
||
if (!frameNameEntry.method.getRealMethod().isBuiltin()) { | ||
return frameNameSite.call(context, self, self); | ||
} | ||
|
||
if (compositeName == null) return context.nil; | ||
|
||
return callee ? | ||
RubySymbol.newCalleeSymbolFromCompound(context.runtime, compositeName): | ||
RubySymbol.newMethodSymbolFromCompound(context.runtime, compositeName); | ||
} | ||
|
||
@Override | ||
public void visit(IRVisitor visitor) { | ||
visitor.FrameNameCallInstr(this); | ||
} | ||
} |
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
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
Oops, something went wrong.