Skip to content

Commit

Permalink
Showing 2 changed files with 41 additions and 21 deletions.
61 changes: 40 additions & 21 deletions core/src/main/java/org/jruby/RubyArray.java
Original file line number Diff line number Diff line change
@@ -66,6 +66,7 @@
import org.jruby.util.Pack;
import org.jruby.util.RecursiveComparator;
import org.jruby.util.TypeConverter;
import org.jruby.util.cli.Options;
import org.jruby.util.io.EncodingUtils;

import java.io.IOException;
@@ -81,6 +82,7 @@

import static org.jruby.RubyEnumerator.enumeratorize;
import static org.jruby.RubyEnumerator.enumeratorizeWithSize;
import static org.jruby.runtime.Helpers.arrayOf;
import static org.jruby.runtime.Helpers.hashEnd;
import static org.jruby.runtime.Helpers.murmurCombine;
import static org.jruby.runtime.Visibility.PRIVATE;
@@ -97,6 +99,8 @@
public class RubyArray extends RubyObject implements List, RandomAccess {
public static final int DEFAULT_INSPECT_STR_SIZE = 10;

private static final boolean USE_PACKED_ARRAYS = Options.PACKED_ARRAYS.load();

public static RubyClass createArrayClass(Ruby runtime) {
RubyClass arrayc = runtime.defineClass("Array", runtime.getObject(), ARRAY_ALLOCATOR);
runtime.setArray(arrayc);
@@ -202,15 +206,15 @@ public static final RubyArray newArrayLight(final Ruby runtime) {
}

public static RubyArray newArray(Ruby runtime, IRubyObject obj) {
return new RubyArrayOneObject(runtime, obj);
return USE_PACKED_ARRAYS ? new RubyArrayOneObject(runtime, obj) : new RubyArray(runtime, arrayOf(obj));
}

public static RubyArray newArrayLight(Ruby runtime, IRubyObject obj) {
return new RubyArrayOneObject(runtime, obj);
return USE_PACKED_ARRAYS ? new RubyArrayOneObject(runtime, obj) : new RubyArray(runtime, arrayOf(obj));
}

public static RubyArray newArrayLight(Ruby runtime, IRubyObject car, IRubyObject cdr) {
return new RubyArrayTwoObject(runtime, car, cdr);
return USE_PACKED_ARRAYS ? new RubyArrayTwoObject(runtime, car, cdr) : new RubyArray(runtime, arrayOf(car, cdr));
}

public static RubyArray newArrayLight(Ruby runtime, IRubyObject... objs) {
@@ -221,15 +225,15 @@ public static RubyArray newArrayLight(Ruby runtime, IRubyObject... objs) {
*
*/
public static RubyArray newArray(Ruby runtime, IRubyObject car, IRubyObject cdr) {
return new RubyArrayTwoObject(runtime, car, cdr);
return USE_PACKED_ARRAYS ? new RubyArrayTwoObject(runtime, car, cdr) : new RubyArray(runtime, arrayOf(car, cdr));
}

public static RubyArray newArray(Ruby runtime, IRubyObject first, IRubyObject second, IRubyObject third) {
return new RubyArray(runtime, new IRubyObject[] {first, second, third});
return new RubyArray(runtime, arrayOf(first, second, third));
}

public static RubyArray newArray(Ruby runtime, IRubyObject first, IRubyObject second, IRubyObject third, IRubyObject fourth) {
return new RubyArray(runtime, new IRubyObject[] {first, second, third, fourth});
return new RubyArray(runtime, arrayOf(first, second, third, fourth));
}

public static RubyArray newEmptyArray(Ruby runtime) {
@@ -244,9 +248,11 @@ public static RubyArray newArray(Ruby runtime, IRubyObject[] args) {
case 0:
return newEmptyArray(runtime);
case 1:
return new RubyArrayOneObject(runtime, args[0]);
if (USE_PACKED_ARRAYS) return new RubyArrayOneObject(runtime, args[0]);
break;
case 2:
return new RubyArrayTwoObject(runtime, args[0], args[1]);
if (USE_PACKED_ARRAYS) return new RubyArrayTwoObject(runtime, args[0], args[1]);
break;
}
RubyArray arr = new RubyArray(runtime, new IRubyObject[args.length]);
System.arraycopy(args, 0, arr.values, 0, args.length);
@@ -262,9 +268,11 @@ public static RubyArray newArrayMayCopy(Ruby runtime, IRubyObject... args) {
case 0:
return newEmptyArray(runtime);
case 1:
return new RubyArrayOneObject(runtime, args[0]);
if (USE_PACKED_ARRAYS) return new RubyArrayOneObject(runtime, args[0]);
break;
case 2:
return new RubyArrayTwoObject(runtime, args[0], args[1]);
if (USE_PACKED_ARRAYS) return new RubyArrayTwoObject(runtime, args[0], args[1]);
break;
}
return newArrayNoCopy(runtime, args, 0, args.length);
}
@@ -293,9 +301,11 @@ public static RubyArray newArrayMayCopy(Ruby runtime, IRubyObject[] args, int st
case 0:
return newEmptyArray(runtime);
case 1:
return new RubyArrayOneObject(runtime, args[start]);
if (USE_PACKED_ARRAYS) return new RubyArrayOneObject(runtime, args[start]);
break;
case 2:
return new RubyArrayTwoObject(runtime, args[start], args[start + 1]);
if (USE_PACKED_ARRAYS) return new RubyArrayTwoObject(runtime, args[start], args[start + 1]);
break;
}
return newArrayNoCopy(runtime, args, start, length);
}
@@ -328,17 +338,25 @@ public static RubyArray newArray(Ruby runtime, Collection<? extends IRubyObject>
IRubyObject[] values = collection.toArray(new IRubyObject[collection.size()]);
switch (values.length) {
case 0: return newEmptyArray(runtime);
case 1: return new RubyArrayOneObject(runtime, values[0]);
case 2: return new RubyArrayTwoObject(runtime, values[0], values[1]);
case 1:
if (USE_PACKED_ARRAYS) return new RubyArrayOneObject(runtime, values[0]);
break;
case 2:
if (USE_PACKED_ARRAYS) return new RubyArrayTwoObject(runtime, values[0], values[1]);
break;
}
return new RubyArray(runtime, values);
}

public static RubyArray newArray(Ruby runtime, List<? extends IRubyObject> list) {
switch (list.size()) {
case 0: return newEmptyArray(runtime);
case 1: return new RubyArrayOneObject(runtime, list.get(0));
case 2: return new RubyArrayTwoObject(runtime, list.get(0), list.get(1));
case 1:
if (USE_PACKED_ARRAYS) return new RubyArrayOneObject(runtime, list.get(0));
break;
case 2:
if (USE_PACKED_ARRAYS) return new RubyArrayTwoObject(runtime, list.get(0), list.get(1));
break;
}
return new RubyArray(runtime, list.toArray(new IRubyObject[list.size()]));
}
@@ -4372,17 +4390,18 @@ public static RubyArray unmarshalFrom(UnmarshalStream input) throws IOException
* @return a RubyArray shaped for the given size
*/
public static RubyArray newBlankArray(Ruby runtime, int size) {
RubyArray result;
switch (size) {
case 0:
return newEmptyArray(runtime);
case 1:
return new RubyArrayOneObject(runtime, runtime.getNil());
if (USE_PACKED_ARRAYS) return new RubyArrayOneObject(runtime, runtime.getNil());
break;
case 2:
return new RubyArrayTwoObject(runtime, runtime.getNil(), runtime.getNil());
default:
return newArray(runtime, size);
if (USE_PACKED_ARRAYS) return new RubyArrayTwoObject(runtime, runtime.getNil(), runtime.getNil());
break;
}

return newArray(runtime, size);
}

@JRubyMethod(name = "try_convert", meta = true)
1 change: 1 addition & 0 deletions core/src/main/java/org/jruby/util/cli/Options.java
Original file line number Diff line number Diff line change
@@ -169,6 +169,7 @@ public class Options {
public static final Option<String> PREFERRED_PRNG = string(MISCELLANEOUS, "preferred.prng", "NativePRNGNonBlocking", "Maintain children static scopes to support scope dumping.");
public static final Option<Boolean> USE_FIXNUM_CACHE = bool(MISCELLANEOUS, "fixnum.cache", true, "Use a cache of low-valued Fixnum objects.");
public static final Option<Integer> FIXNUM_CACHE_RANGE = integer(MISCELLANEOUS, "fixnum.cache.size", 256, "Values to retrieve from Fixnum cache, in the range -X..(X-1).");
public static final Option<Boolean> PACKED_ARRAYS = bool(MISCELLANEOUS, "packed.arrays", true, "Toggle whether to use \"packed\" arrays for small tuples.");

public static final Option<Boolean> DEBUG_LOADSERVICE = bool(DEBUG, "debug.loadService", false, "Log require/load file searches.");
public static final Option<Boolean> DEBUG_LOADSERVICE_TIMING = bool(DEBUG, "debug.loadService.timing", false, "Log require/load parse+evaluate times.");

0 comments on commit d5f6fca

Please sign in to comment.