-
-
Notifications
You must be signed in to change notification settings - Fork 941
improve performance for Java "block-to-interface" conversion #9401
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kares
wants to merge
10
commits into
jruby:jruby-10.0
Choose a base branch
from
kares:proc-to-iface-slow-10.0
base: jruby-10.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
bf163f5
[refactor] fun interface method wout exception
kares 8fe48aa
draft: wire up "bare" block interface generation
kares c003a5e
assume functional interface with block generator
kares 20b0849
wrap up (contained) block-to-interface feature
kares 117c2bb
[test] more block-to-interface conversion specs
kares 194db41
[test] more spec coverage for primitives
kares 77cd570
[test] more coverage for (generic) proc-to-interface
kares c205081
[bench] simple block-to-interface benchmark
kares ec52250
[refactor] re-use result coercion + return
kares bfb3c62
[refactor] avoid boxing - re-use argument coercion
kares File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,61 @@ | ||
| require 'java' | ||
| require 'benchmark' | ||
|
|
||
| N = (ARGV[0] || 2 ** 21).to_i # default ~ 2_000_000 | ||
|
|
||
| EMPTY = java.util.Optional.empty | ||
| PRESENT = java.util.Optional.of('x') | ||
| PRESENT_INT = java.util.OptionalInt.of(11) | ||
| EMPTY_DOUBLE = java.util.OptionalDouble.empty | ||
|
|
||
| LIST = java.util.ArrayList.new | ||
| LIST.add(java.lang.Integer.new(0)) | ||
|
|
||
| EMPTY_MAP = java.util.HashMap.new | ||
|
|
||
| puts "----- single-threaded -----" | ||
| Benchmark.bmbm(36) do |bm| | ||
| bm.report("Supplier Optional#orElseGet") { N.times { EMPTY.orElseGet { 'y' } } } | ||
| bm.report("Function Optional#map") { N.times { PRESENT.map { |s| s } } } | ||
| bm.report("Predicate Optional#filter") { N.times { PRESENT.filter { |_| true } } } | ||
| bm.report("IntConsumer OptionalInt#ifPresent") { N.times { PRESENT_INT.ifPresent { |i| } } } | ||
| bm.report("DoubleSupplier OptionalDouble#orElseGet") { N.times { EMPTY_DOUBLE.orElseGet { 0.0 } } } | ||
| bm.report("BiFunction Map#compute") { N.times { EMPTY_MAP.compute(:key) { |k, v| nil } } } | ||
| bm.report("BiFunction Map#computeIfPresent") { N.times { EMPTY_MAP.computeIfPresent(1) { fail('never called') } } } | ||
| bm.report("ToLongFunction Comparator.comparingLong") { N.times { java.util.Comparator.comparingLong { |s| s.length } } } | ||
| bm.report("Comparator ArrayList#sort") { N.times { LIST.sort { |a, b| a <=> b } } } | ||
| end | ||
|
|
||
| puts | ||
| puts "----- multi-threaded (4 submitter threads, N tasks each) -----" | ||
| Benchmark.bmbm(36) do |bm| | ||
| bm.report("Consumer Optional#ifPresent (4 threads)") do | ||
| 4.times.map { Thread.new { (N / 4).times { PRESENT.ifPresent { |_| } } } }.each(&:join) | ||
| end | ||
| bm.report("Consumer Optional#ifPresent (16 threads)") do | ||
| 16.times.map { Thread.new { (N / 16).times { PRESENT.ifPresent { |_| } } } }.each(&:join) | ||
| end | ||
| bm.report("Consumer Optional#ifPresent (128 threads)") do | ||
| 128.times.map { Thread.new { (N / 128).times { PRESENT.ifPresent { |_| } } } }.each(&:join) | ||
| end | ||
|
|
||
| bm.report("IntConsumer OptionalInt#ifPresent (4 threads)") do | ||
| 4.times.map { Thread.new { (N / 4).times { PRESENT_INT.ifPresent { |i| } } } }.each(&:join) | ||
| end | ||
| bm.report("IntConsumer OptionalInt#ifPresent (128 threads)") do | ||
| 128.times.map { Thread.new { (N / 128).times { PRESENT_INT.ifPresent { |i| } } } }.each(&:join) | ||
| end | ||
| bm.report("IntConsumer OptionalInt#ifPresent (512 threads)") do | ||
| 512.times.map { Thread.new { (N / 512).times { PRESENT_INT.ifPresent { |i| } } } }.each(&:join) | ||
| end | ||
|
|
||
| bm.report("Comparator List#stream.sorted (16 threads)") do | ||
| 16.times.map { Thread.new { (N / 16).times { LIST.stream.sorted { |a, b| a <=> b } } } }.each(&:join) | ||
| end | ||
| bm.report("Comparator List#stream.sorted (128 threads)") do | ||
| 128.times.map { Thread.new { (N / 128).times { LIST.stream.sorted { |a, b| a <=> b } } } }.each(&:join) | ||
| end | ||
| bm.report("Comparator List#stream.sorted (512 threads)") do | ||
| 512.times.map { Thread.new { (N / 512).times { LIST.stream.sorted { |a, b| a <=> b } } } }.each(&:join) | ||
| end | ||
| end |
This file contains hidden or 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
248 changes: 248 additions & 0 deletions
248
core/src/main/java/org/jruby/java/codegen/BlockInterfaceGenerator.java
This file contains hidden or 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,248 @@ | ||
| package org.jruby.java.codegen; | ||
|
|
||
| import java.lang.reflect.Constructor; | ||
| import java.lang.reflect.Method; | ||
| import java.util.concurrent.ConcurrentMap; | ||
|
|
||
| import org.jruby.Ruby; | ||
| import org.jruby.RubyProc; | ||
| import org.jruby.compiler.impl.SkinnyMethodAdapter; | ||
| import org.jruby.java.proxies.BlockInterfaceTemplate; | ||
| import org.jruby.javasupport.Java; | ||
| import org.jruby.runtime.builtin.IRubyObject; | ||
| import org.jruby.util.ASM; | ||
| import org.jruby.util.ClassDefiningClassLoader; | ||
| import org.jruby.util.JRubyClassLoader; | ||
| import org.jruby.util.collections.ConcurrentWeakHashMap; | ||
| import org.objectweb.asm.ClassWriter; | ||
| import org.objectweb.asm.Type; | ||
|
|
||
| import static org.jruby.util.CodegenUtils.ci; | ||
| import static org.jruby.util.CodegenUtils.getBoxType; | ||
| import static org.jruby.util.CodegenUtils.p; | ||
| import static org.jruby.util.CodegenUtils.sig; | ||
| import static org.objectweb.asm.Opcodes.ACC_PUBLIC; | ||
| import static org.objectweb.asm.Opcodes.ACC_SUPER; | ||
| import static org.objectweb.asm.Opcodes.ACC_SYNTHETIC; | ||
| import static org.objectweb.asm.Opcodes.V11; | ||
|
|
||
| /** | ||
| * Generates a concrete subclass of {@link BlockInterfaceTemplate} for a Java interface, where | ||
| * every abstract method delegates straight into one of the inherited {@code __ruby_call} helpers. | ||
| * | ||
| * Default methods on the interface are intentionally <em>not</em> overridden, matching the pre-existing | ||
| * {@code convertProcToInterface} behavior where only abstract methods were backed by the proc. | ||
| * | ||
| * <p>For an interface such as: | ||
| * <pre>{@code | ||
| * public interface MyIface { | ||
| * int compute(int a, String b); | ||
| * default int compute2(int a, String b) { return compute(a, b) * 2; } | ||
| * } | ||
| * }</pre> | ||
| * the generated class is equivalent to: | ||
| * <pre>{@code | ||
| * public final class BlockInterfaceImpl$<hash> extends BlockInterfaceTemplate implements MyIface { | ||
| * public BlockInterfaceImpl$<hash>(RubyProc proc) { super(proc); } | ||
| * | ||
| * public int compute(int a, String b) { | ||
| * Object result = __ruby_call(Integer.TYPE, coerce(a), coerce(b)); | ||
| * return ((Number) result).intValue(); | ||
| * } | ||
| * // compute2 is not overridden — the interface default is used | ||
| * } | ||
| * }</pre> | ||
| */ | ||
| public final class BlockInterfaceGenerator { | ||
|
|
||
| /** | ||
| * Cache of generated {@link BlockInterfaceTemplate} subclass constructors, weakly keyed by the interface. | ||
| * | ||
| * <p> | ||
| * The generated class lives in the {@link JRubyClassLoader}, so the value never roots a classloader outside of | ||
| * JRuby itself; the only strong root held by a cache entry is the constructor (class pair) for the interface. | ||
| */ | ||
| static final ConcurrentMap<Class<?>, Constructor<? extends BlockInterfaceTemplate>> CACHE = new ConcurrentWeakHashMap<>(); | ||
|
|
||
| public static Constructor<? extends BlockInterfaceTemplate> fromCache(final Class<?> interfaceType) { | ||
| return CACHE.get(interfaceType); | ||
| } | ||
|
|
||
| /** | ||
| * @return the constructor, which is usable with any Ruby block targeting the same interface | ||
| */ | ||
| @SuppressWarnings("unchecked") | ||
| public static Constructor<? extends BlockInterfaceTemplate> getConstructor(final Ruby runtime, final Class<?> interfaceType) | ||
| throws ReflectiveOperationException { | ||
|
|
||
| var constructor = CACHE.get(interfaceType); | ||
| if (constructor != null) return constructor; | ||
|
|
||
| assert interfaceType.isInterface(); | ||
|
|
||
| final String implClassName = makeImplClassName(interfaceType); | ||
| final JRubyClassLoader loader = runtime.getJRubyClassLoader(); | ||
|
|
||
| synchronized (loader) { | ||
| constructor = CACHE.get(interfaceType); | ||
| if (constructor != null) return constructor; | ||
|
|
||
| Class<?> implClass; | ||
| try { | ||
| implClass = Class.forName(implClassName, true, loader); | ||
| } catch (ClassNotFoundException e) { | ||
| assert Java.getFunctionalInterfaceMethod(interfaceType) != null : "not a functional-interface: " + interfaceType; | ||
| implClass = defineImplClass(loader, interfaceType, implClassName); | ||
| } | ||
|
|
||
| constructor = (Constructor<? extends BlockInterfaceTemplate>) implClass.getConstructor(RubyProc.class); | ||
| CACHE.put(interfaceType, constructor); | ||
| return constructor; | ||
| } | ||
| } | ||
|
|
||
| private static String makeImplClassName(final Class<?> interfaceType) { | ||
| return "org.jruby.gen.BlockInterfaceImpl$" + interfaceType.getSimpleName() + Math.abs(interfaceType.hashCode()); | ||
| } | ||
|
|
||
| /** | ||
| * Emits the class header, the single {@code (RubyProc)} constructor, and one bridge method | ||
| * per abstract method collected from the interface. Equivalent to: | ||
| * <pre>{@code | ||
| * public final class <implClassName> | ||
| * extends BlockInterfaceTemplate | ||
| * implements <interfaceType> { ... } | ||
| * }</pre> | ||
| */ | ||
| private static Class<?> defineImplClass(final ClassDefiningClassLoader loader, | ||
| final Class<?> interfaceType, | ||
| final String implClassName) { | ||
| final ClassWriter cw = ASM.newClassWriter((ClassLoader) loader); | ||
| final String pathName = implClassName.replace('.', '/'); | ||
|
|
||
| cw.visit(V11, | ||
| ACC_PUBLIC | ACC_SUPER | ACC_SYNTHETIC, | ||
| pathName, | ||
| null, | ||
| p(BlockInterfaceTemplate.class), | ||
| new String[] { p(interfaceType) }); | ||
| cw.visitSource(pathName + ".gen", null); | ||
|
|
||
| defineConstructor(cw); | ||
| Method implMethod = Java.getFunctionalInterfaceMethod(interfaceType); | ||
| if (implMethod != null) defineBridgeMethod(cw, implMethod); | ||
|
|
||
| cw.visitEnd(); | ||
|
|
||
| final byte[] bytecode = cw.toByteArray(); | ||
| return loader.defineClass(implClassName, bytecode); | ||
| } | ||
|
|
||
| /** | ||
| * <pre>{@code | ||
| * public <implClass>(RubyProc proc) { super(proc); } | ||
| * }</pre> | ||
| */ | ||
| private static void defineConstructor(final ClassWriter cw) { | ||
| final SkinnyMethodAdapter init = new SkinnyMethodAdapter(cw, ACC_PUBLIC, "<init>", | ||
| sig(void.class, RubyProc.class), null, null); | ||
| init.start(); | ||
| init.aload(0); | ||
| init.aload(1); | ||
| init.invokespecial(p(BlockInterfaceTemplate.class), "<init>", sig(void.class, RubyProc.class)); | ||
| init.voidreturn(); | ||
| init.end(); | ||
| } | ||
|
|
||
| /** | ||
| * Emits a bridge method that calls {@code __ruby_call(returnType, rubyArgs...)} on the inherited | ||
| * {@link BlockInterfaceTemplate} and unboxes/casts the result to interface method's declared return type. | ||
| * | ||
| * <p>For a one-arg method {@code R accept(A a)}: | ||
| * <pre>{@code | ||
| * public R accept(A a) { | ||
| * IRubyObject result = __ruby_call(<returnClass>, coerce(a)); | ||
| * return (R) result.toJava(<returnClass>); | ||
| * } | ||
| * }</pre> | ||
| */ | ||
| private static void defineBridgeMethod(final ClassWriter cw, final Method method) { | ||
| final Class<?> returnType = method.getReturnType(); | ||
| final Class<?>[] paramTypes = method.getParameterTypes(); | ||
| final SkinnyMethodAdapter mv = new SkinnyMethodAdapter(cw, ACC_PUBLIC, method.getName(), | ||
| sig(returnType, paramTypes), null, null); | ||
|
|
||
| final int rubyIndex = nextLocalIndex(paramTypes); | ||
|
|
||
| mv.start(); | ||
| if (paramTypes.length > 0) { | ||
| mv.aload(0); | ||
| mv.getfield(p(BlockInterfaceTemplate.class), "runtime", ci(Ruby.class)); | ||
| mv.astore(rubyIndex); | ||
| } | ||
|
|
||
| // `this.__ruby_call(...)` - push receiver + the return-type class literal for every arity | ||
| mv.aload(0); | ||
| pushClassLiteral(mv, returnType); | ||
|
|
||
| switch (paramTypes.length) { | ||
| case 0: // __ruby_call(<returnClass>) | ||
| mv.invokevirtual(p(BlockInterfaceTemplate.class), "__ruby_call", sig(IRubyObject.class, Class.class)); | ||
| break; | ||
| case 1: // __ruby_call(<returnClass>, coerce(arg0)) | ||
| RealClassGenerator.coerceArgumentToRuby(mv, paramTypes[0], 1, rubyIndex); | ||
| mv.invokevirtual(p(BlockInterfaceTemplate.class), "__ruby_call", sig(IRubyObject.class, Class.class, IRubyObject.class)); | ||
| break; | ||
| case 2: // __ruby_call(<returnClass>, coerce(arg0), coerce(arg1)) | ||
| int argIndex = RealClassGenerator.coerceArgumentToRuby(mv, paramTypes[0], 1, rubyIndex); | ||
| RealClassGenerator.coerceArgumentToRuby(mv, paramTypes[1], argIndex, rubyIndex); | ||
| mv.invokevirtual(p(BlockInterfaceTemplate.class), "__ruby_call", sig(IRubyObject.class, Class.class, IRubyObject.class, IRubyObject.class)); | ||
| break; | ||
| default: // __ruby_call(<returnClass>, new IRubyObject[] { coerce(arg0), coerce(arg1), ... }) | ||
| RealClassGenerator.coerceArgumentsToRuby(mv, paramTypes, rubyIndex); | ||
| mv.invokevirtual(p(BlockInterfaceTemplate.class), "__ruby_call", sig(IRubyObject.class, Class.class, IRubyObject[].class)); | ||
| break; | ||
| } | ||
|
|
||
| emitReturn(mv, returnType); | ||
| mv.end(); | ||
| } | ||
|
|
||
| private static int nextLocalIndex(Class<?>[] paramTypes) { | ||
| int index = 1; | ||
| for (Class<?> paramType : paramTypes) { | ||
| index += RealClassGenerator.paramSlotSize(paramType); | ||
| } | ||
| return index; | ||
| } | ||
|
|
||
| /** | ||
| * Pushes the {@code Class<?>} literal for {@code type} on the stack. | ||
| * | ||
| * <ul> | ||
| * <li>primitive (non-void) → {@code Integer.TYPE}, {@code Long.TYPE}, ... | ||
| * <li>{@code void} → {@code Void.TYPE} | ||
| * <li>reference → {@code ldc <Type>.class} | ||
| * </ul> | ||
| */ | ||
| private static void pushClassLiteral(SkinnyMethodAdapter mv, Class<?> type) { | ||
| if (type.isPrimitive()) { | ||
| if (type == void.class) { | ||
| mv.getstatic(p(Void.class), "TYPE", ci(Class.class)); | ||
| } else { | ||
| mv.getstatic(p(getBoxType(type)), "TYPE", ci(Class.class)); | ||
| } | ||
| } else { | ||
| mv.ldc(Type.getType(type)); | ||
| } | ||
| } | ||
|
|
||
| private static void emitReturn(SkinnyMethodAdapter mv, Class<?> returnType) { | ||
| if (returnType == void.class) { | ||
| mv.pop(); | ||
| mv.voidreturn(); | ||
| } else { | ||
| RealClassGenerator.coerceResultAndReturn(mv, returnType); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Of course this stuff uses java.lang.reflect all over the place (both before and after this change) but we could be using MethodHandles for all of these and skip the overhead of a reflective invocation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tried unreflecting the constructor into a handle, no difference with the benchmarks mentioned in the PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The handle needs a static root of some kind, either a static final field (not possible here) or by using LambdaMetaFactory to generate an interface implementation. Without those it usually is not much faster than reflection, which also uses unrooted handles internally.