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


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

URL: http://github.com/jruby/jruby/commit/5e917ce1caef0c2cb082a2b1456762659870441d

/> Generate smaller bytecode when using Ruby type on implemented Java I… · jruby/jruby@5e917ce · GitHub
Skip to content

Commit 5e917ce

Browse files
Generate smaller bytecode when using Ruby type on implemented Java Interface + Benchmark
1 parent aefd8a8 commit 5e917ce

3 files changed

Lines changed: 110 additions & 10 deletions

File tree

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package org.jruby.benchmark;
2+
3+
import java.util.concurrent.ThreadLocalRandom;
4+
import java.util.concurrent.TimeUnit;
5+
import org.jruby.Ruby;
6+
import org.jruby.RubyFixnum;
7+
import org.jruby.RubyInstanceConfig;
8+
import org.jruby.runtime.builtin.IRubyObject;
9+
import org.openjdk.jmh.annotations.Benchmark;
10+
import org.openjdk.jmh.annotations.BenchmarkMode;
11+
import org.openjdk.jmh.annotations.Fork;
12+
import org.openjdk.jmh.annotations.Measurement;
13+
import org.openjdk.jmh.annotations.Mode;
14+
import org.openjdk.jmh.annotations.OperationsPerInvocation;
15+
import org.openjdk.jmh.annotations.OutputTimeUnit;
16+
import org.openjdk.jmh.annotations.Scope;
17+
import org.openjdk.jmh.annotations.State;
18+
import org.openjdk.jmh.annotations.Warmup;
19+
import org.openjdk.jmh.infra.Blackhole;
20+
21+
@Warmup(iterations = 3, time = 500, timeUnit = TimeUnit.MILLISECONDS)
22+
@Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
23+
@Fork(1)
24+
@BenchmarkMode(Mode.Throughput)
25+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
26+
@State(Scope.Thread)
27+
public class JavaInterfaceBenchmark {
28+
29+
private static final int INVOCATIONS = 200_000_000;
30+
31+
private static final Ruby RUBY = initRuby();
32+
33+
private static final RubyFixnum LEFT = RUBY.newFixnum(ThreadLocalRandom.current().nextInt());
34+
35+
private static final RubyFixnum RIGHT = RUBY.newFixnum(ThreadLocalRandom.current().nextInt());
36+
37+
private static final JavaInterfaceBenchmark.Summation JAVA_SUMMER =
38+
new JavaInterfaceBenchmark.Summation() {
39+
@Override
40+
public IRubyObject sum(final RubyFixnum left, final RubyFixnum right) {
41+
return left.op_plus(RUBY.getCurrentContext(), right);
42+
}
43+
};
44+
45+
public interface Summation {
46+
IRubyObject sum(RubyFixnum left, RubyFixnum right);
47+
}
48+
49+
@Benchmark
50+
@OperationsPerInvocation(INVOCATIONS)
51+
public void benchHalfRubyVersion(final Blackhole blackhole) {
52+
blackhole.consume(
53+
RUBY.executeScript(
54+
"org.jruby.benchmark.JavaInterfaceBenchmark.doRun(RubySummation.new)"
55+
,"benchHalfRubyVersion")
56+
);
57+
}
58+
59+
@Benchmark
60+
@OperationsPerInvocation(INVOCATIONS)
61+
public void benchJavaVersion(final Blackhole blackhole) {
62+
blackhole.consume(doRun(JAVA_SUMMER));
63+
}
64+
65+
public static IRubyObject doRun(final JavaInterfaceBenchmark.Summation summer) {
66+
IRubyObject sum = null;
67+
for (int i = 0; i < INVOCATIONS; ++i) {
68+
sum = summer.sum(LEFT, RIGHT);
69+
}
70+
return sum;
71+
}
72+
73+
private static Ruby initRuby() {
74+
final RubyInstanceConfig config = new RubyInstanceConfig();
75+
config.setCompileMode(RubyInstanceConfig.CompileMode.FORCE);
76+
final Ruby ruby = Ruby.newInstance(config);
77+
ruby.executeScript(
78+
new StringBuilder()
79+
.append("class RubySummation\n")
80+
.append("\tinclude org.jruby.benchmark.JavaInterfaceBenchmark::Summation\n")
81+
.append('\n')
82+
.append("\tdef sum(a, b)\n")
83+
.append("\t\ta + b\n")
84+
.append("\tend\n")
85+
.append("end")
86+
.toString(), "initRuby"
87+
);
88+
return ruby;
89+
}
90+
}

core/src/main/java/org/jruby/ir/passes/LocalOptimizationPass.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ private static void recordSimplification(Variable res, Operand val, Map<Operand,
4848

4949
// For all variables used by val, record a reverse mapping to let us track
5050
// Read-After-Write scenarios when any of these variables are modified.
51-
List<Variable> valVars = new ArrayList<Variable>();
51+
List<Variable> valVars = new ArrayList<>();
5252
val.addUsedVariables(valVars);
5353
for (Variable v: valVars) {
5454
List<Variable> x = simplificationMap.get(v);
5555
if (x == null) {
56-
x = new ArrayList<Variable>();
56+
x = new ArrayList<>();
5757
simplificationMap.put(v, x);
5858
}
5959
x.add(res);

core/src/main/java/org/jruby/java/codegen/RealClassGenerator.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import java.util.List;
3939
import java.util.Map;
4040
import java.util.Set;
41-
4241
import org.jruby.Ruby;
4342
import org.jruby.RubyBasicObject;
4443
import org.jruby.RubyClass;
@@ -47,21 +46,29 @@
4746
import org.jruby.compiler.impl.SkinnyMethodAdapter;
4847
import org.jruby.compiler.util.BasicObjectStubGenerator;
4948
import org.jruby.internal.runtime.methods.DynamicMethod;
50-
import org.jruby.internal.runtime.methods.UndefinedMethod;
5149
import org.jruby.javasupport.JavaUtil;
5250
import org.jruby.runtime.Block;
5351
import org.jruby.runtime.ThreadContext;
5452
import org.jruby.runtime.builtin.IRubyObject;
55-
import org.jruby.runtime.callsite.CacheEntry;
5653
import org.jruby.util.ClassDefiningClassLoader;
5754
import org.jruby.util.ClassDefiningJRubyClassLoader;
58-
import static org.jruby.util.CodegenUtils.*;
59-
6055
import org.jruby.util.Loader;
6156
import org.objectweb.asm.ClassWriter;
6257
import org.objectweb.asm.Label;
6358
import org.objectweb.asm.Type;
64-
import static org.objectweb.asm.Opcodes.*;
59+
60+
import static org.jruby.util.CodegenUtils.ci;
61+
import static org.jruby.util.CodegenUtils.getBoxType;
62+
import static org.jruby.util.CodegenUtils.p;
63+
import static org.jruby.util.CodegenUtils.params;
64+
import static org.jruby.util.CodegenUtils.prettyParams;
65+
import static org.jruby.util.CodegenUtils.sig;
66+
import static org.objectweb.asm.Opcodes.ACC_FINAL;
67+
import static org.objectweb.asm.Opcodes.ACC_PRIVATE;
68+
import static org.objectweb.asm.Opcodes.ACC_PUBLIC;
69+
import static org.objectweb.asm.Opcodes.ACC_STATIC;
70+
import static org.objectweb.asm.Opcodes.ACC_SUPER;
71+
import static org.objectweb.asm.Opcodes.V1_6;
6572

6673
/**
6774
* On fly .class generator (used for Ruby interface impls).
@@ -731,8 +738,8 @@ public static void coerceArgumentsToRuby(SkinnyMethodAdapter mv, Class[] paramTy
731738
mv.dup();
732739
mv.pushInt(i);
733740
// convert to IRubyObject
734-
mv.aload(rubyIndex);
735741
if (paramTypes[i].isPrimitive()) {
742+
mv.aload(rubyIndex);
736743
if (paramType == byte.class || paramType == short.class || paramType == char.class || paramType == int.class) {
737744
mv.iload(argIndex++);
738745
mv.invokestatic(p(JavaUtil.class), "convertJavaToRuby", sig(IRubyObject.class, Ruby.class, int.class));
@@ -751,9 +758,12 @@ public static void coerceArgumentsToRuby(SkinnyMethodAdapter mv, Class[] paramTy
751758
mv.iload(argIndex++);
752759
mv.invokestatic(p(JavaUtil.class), "convertJavaToRuby", sig(IRubyObject.class, Ruby.class, boolean.class));
753760
}
754-
} else {
761+
} else if (!IRubyObject.class.isAssignableFrom(paramType)) {
762+
mv.aload(rubyIndex);
755763
mv.aload(argIndex++);
756764
mv.invokestatic(p(JavaUtil.class), "convertJavaToUsableRubyObject", sig(IRubyObject.class, Ruby.class, Object.class));
765+
} else {
766+
mv.aload(argIndex++);
757767
}
758768
mv.aastore();
759769
}

0 commit comments

Comments
 (0)
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