-
-
Notifications
You must be signed in to change notification settings - Fork 925
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Utilize packed 1-2 elt arrays more places in the codebase.
This commit includes a pass over our code to improve places where we are creating one or two-element arrays via the IRubyObject[] paths, so they will use packed arrays when possible. I did this survey by applying the patch (below) and running ruby/spec, various gem commands, and starting up and hitting a blank rails application. The pattern used for most of these changes is as follows: * Allocate a new "blank" array with RubyArray.newBlankArray(size). * Popululate at least the first 'size' elements. This is necessary since the packed arrays can only represent exactly one or two elements, and must be unpacked to represent other sizes. Most cases found in the survey were updated to this new pattern and now will create packed forms. Notable exceptions that I skipped (usually due to algorithms that had unpredictable sizing) were: * RubyString.enumerate[chars,codepoints] - Because of the variable width of characters in multibyte, this logic was difficult to connect up to an "exactly this size" array. It should be possible to use packed arrays for 7-bit cases, or to be smarter about small strings with one or two codepoints, but I have not attempted that here. * RubyArray.values_at - Because the incoming arguments may not represent exactly how many items will be pulled from the array (e.g if you pass in a Range). * RubyStruct.values_at - Same reasons as for RubyArray.values_at. * SelectExecutor's construction of result arrays - Because some or all of the expected channels may not be ready, it would require more logic to right-size the arrays. This is a heavily- hit piece of logic and such an improvement may be warranted, but there's significantly more overhead in SelectExecutor that needs a rewrite before packed arrays would show any significant improvement. The patch mentioned above follows. ```diff diff --git a/core/src/main/java/org/jruby/RubyArray.java b/core/src/main/java/org/jruby/RubyArray.java index d2bc419..180af8f 100644 --- a/core/src/main/java/org/jruby/RubyArray.java +++ b/core/src/main/java/org/jruby/RubyArray.java @@ -160,26 +160,30 @@ public class RubyArray extends RubyObject implements List, RandomAccess { /** rb_ary_new2 * */ public static final RubyArray newArray(final Ruby runtime, final long len) { + if (len == 1 || len == 2) Thread.dumpStack(); checkLength(runtime, len); return newArray(runtime, (int)len); } public static final RubyArray newArrayLight(final Ruby runtime, final long len) { + if (len == 1 || len == 2) Thread.dumpStack(); checkLength(runtime, len); return newArrayLight(runtime, (int)len); } public static final RubyArray newArray(final Ruby runtime, final int len) { + if (len == 1 || len == 2) Thread.dumpStack(); RubyArray array = new RubyArray(runtime, len); Helpers.fillNil(array.values, 0, len, runtime); return array; } public static final RubyArray newArrayLight(final Ruby runtime, final int len) { + if (len == 1 || len == 2) Thread.dumpStack(); RubyArray array = new RubyArray(runtime, len, false); Helpers.fillNil(array.values, 0, len, runtime); return array; } ```
Showing
11 changed files
with
189 additions
and
107 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
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
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