Permalink
Browse files

Improve construction of Java arrays and add type.new_array.

  • Loading branch information...
1 parent fc5de3e commit a121b62db206de48e9561cd2e01c508853aa9bf8 @headius headius committed Dec 12, 2012
@@ -1,21 +1,53 @@
-require 'benchmark'
-require 'java'
+require 'perfer'
-(ARGV[0] || 1).to_i.times {
- Benchmark.bm(30) {|bm|
- bm.report("control, ruby array") {
- a = [1,2,3]
- 1_000_000.times {
- a[0]; a[0]; a[0]; a[0]; a[0]
- a[0]; a[0]; a[0]; a[0]; a[0]
- }
- }
- bm.report("java array") {
- a = [1,2,3].to_java :int
- 1_000_000.times {
- a[0]; a[0]; a[0]; a[0]; a[0]
- a[0]; a[0]; a[0]; a[0]; a[0]
- }
- }
- }
-}
+
+Perfer.session 'Java array operations' do |s|
+ s.iterate("access ruby array") do |n|
+ a = [1,2,3]
+ n.times do
+ a[0]; a[0]; a[0]; a[0]; a[0]
+ a[0]; a[0]; a[0]; a[0]; a[0]
+ end
+ end
+
+ s.iterate("access java array") do |n|
+ a = [1,2,3].to_java :int
+ n.times do
+ a[0]; a[0]; a[0]; a[0]; a[0]
+ a[0]; a[0]; a[0]; a[0]; a[0]
+ end
+ end
+
+ s.iterate("construct ruby array") do |n|
+ n.times do
+ a = []; a = []; a = []; a = []; a = []
+ a = []; a = []; a = []; a = []; a = []
+ end
+ end
+
+ s.iterate("construct java array slow") do |n|
+ int = Java::int
+ n.times do
+ int[0].new; int[0].new; int[0].new; int[0].new
+ int[0].new; int[0].new; int[0].new; int[0].new
+ end
+ end
+
+ s.iterate("construct java array medium") do |n|
+ int_ary = Java::int[0]
+ n.times do
+ int_ary.new; int_ary.new; int_ary.new; int_ary.new
+ int_ary.new; int_ary.new; int_ary.new; int_ary.new
+ end
+ end
+
+ if Java::int.respond_to? :new_array
+ s.iterate("construct java array new_array") do |n|
+ int_ary = Java::int[0]
+ n.times do
+ int_ary.new; int_ary.new; int_ary.new; int_ary.new
+ int_ary.new; int_ary.new; int_ary.new; int_ary.new
+ end
+ end
+ end
+end
@@ -22,6 +22,10 @@
public class ArrayJavaProxy extends JavaProxy {
private final JavaUtil.JavaConverter converter;
+ public ArrayJavaProxy(Ruby runtime, RubyClass klazz, Object ary) {
+ this(runtime, klazz, ary, JavaUtil.getJavaConverter(ary.getClass().getComponentType()));
+ }
+
public ArrayJavaProxy(Ruby runtime, RubyClass klazz, Object ary, JavaUtil.JavaConverter converter) {
super(runtime, klazz, ary);
this.converter = converter;
@@ -55,8 +55,10 @@ public IRubyObject op_aref(ThreadContext context, IRubyObject[] sizes) {
@JRubyMethod(name = "new")
public IRubyObject _new(ThreadContext context) {
+ Ruby runtime = context.runtime;
Object array = Array.newInstance(elementClass, dimensions);
- return Java.wrapJavaObject(context.runtime, array);
+
+ return new ArrayJavaProxy(runtime, Java.getProxyClassForObject(runtime, array), array);
}
private void aggregateDimensions(IRubyObject[] sizes) {
@@ -6,6 +6,7 @@
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
+import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
@@ -19,6 +20,7 @@
import org.jruby.RubyClass;
import org.jruby.RubyHash;
import org.jruby.RubyHash.Visitor;
+import org.jruby.RubyInteger;
import org.jruby.RubyMethod;
import org.jruby.RubyModule;
import org.jruby.RubyObject;
@@ -147,6 +149,16 @@ public static IRubyObject op_aref(ThreadContext context, IRubyObject recv, IRuby
return Java.get_proxy_class(javaClass, RuntimeHelpers.invoke(context, javaClass, "array_class"));
}
}
+
+ @JRubyMethod(meta = true)
+ public static IRubyObject new_array(ThreadContext context, IRubyObject recv, IRubyObject arg0) {
+ JavaClass javaClass = (JavaClass)RuntimeHelpers.invoke(context, recv, "java_class");
+ RubyClass proxyClass = (RubyClass)recv;
+ Class componentType = javaClass.javaClass();
+
+ // construct new array proxy (ArrayJavaProxy)
+ return new ArrayJavaProxy(context.runtime, proxyClass, Array.newInstance(componentType, (int)((RubyInteger)arg0.convertToInteger()).getLongValue()));
+ }
@JRubyMethod(name = "__persistent__=", meta = true)
public static IRubyObject persistent(IRubyObject cls, IRubyObject value) {

0 comments on commit a121b62

Please sign in to comment.