Skip to content

Commit

Permalink
[Truffle] No need for a special branch in Array#[].
Browse files Browse the repository at this point in the history
* ArrayUtils.extractRange can handle start == end == source.length.
* Update wrong documentation (see assertions).
  • Loading branch information
eregon committed Jan 13, 2015
1 parent 2ce0e73 commit 66aec1f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 17 deletions.
13 changes: 0 additions & 13 deletions core/src/main/java/org/jruby/truffle/nodes/core/ArrayNodes.java
Expand Up @@ -642,7 +642,6 @@ public abstract static class IndexNode extends ArrayCoreMethodNode {
@Child protected AtNode atNode;

private final BranchProfile outOfBounds = BranchProfile.create();
private final BranchProfile indexAtEnd = BranchProfile.create();

public IndexNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
Expand All @@ -666,9 +665,6 @@ public Object sliceIntegerFixnum(RubyArray array, int start, int length) {
if (normalisedIndex < 0 || normalisedIndex > array.getSize() || length < 0) {
outOfBounds.enter();
return getContext().getCoreLibrary().getNilObject();
} else if (normalisedIndex == array.getSize()) {
indexAtEnd.enter();
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), null, 0);
} else {
final int end = Math.min(array.getSize(), normalisedIndex + length);

Expand All @@ -683,9 +679,6 @@ public Object sliceLongFixnum(RubyArray array, int start, int length) {
if (normalisedIndex < 0 || normalisedIndex > array.getSize() || length < 0) {
outOfBounds.enter();
return getContext().getCoreLibrary().getNilObject();
} else if (normalisedIndex == array.getSize()) {
indexAtEnd.enter();
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), null, 0);
} else {
final int end = Math.min(array.getSize(), normalisedIndex + length);

Expand All @@ -700,9 +693,6 @@ public Object sliceFloat(RubyArray array, int start, int length) {
if (normalisedIndex < 0 || normalisedIndex > array.getSize() || length < 0) {
outOfBounds.enter();
return getContext().getCoreLibrary().getNilObject();
} else if (normalisedIndex == array.getSize()) {
indexAtEnd.enter();
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), null, 0);
} else {
final int end = Math.min(array.getSize(), normalisedIndex + length);

Expand All @@ -717,9 +707,6 @@ public Object sliceObject(RubyArray array, int start, int length) {
if (normalisedIndex < 0 || normalisedIndex > array.getSize() || length < 0) {
outOfBounds.enter();
return getContext().getCoreLibrary().getNilObject();
} else if (normalisedIndex == array.getSize()) {
indexAtEnd.enter();
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), null, 0);
} else {
final int end = Math.min(array.getSize(), normalisedIndex + length);

Expand Down
Expand Up @@ -21,7 +21,7 @@ public abstract class ArrayUtils {
/**
* Extracts part of an array into a newly allocated Object[] array. Does not perform safety checks on parameters.
* @param source the source array whose values should be extracted
* @param start the start index, must be >= 0 and < source.length
* @param start the start index, must be >= 0 and <= source.length

This comment has been minimized.

Copy link
@eregon

eregon Jan 13, 2015

Author Member

@thomaswue Can you confirm this fix is right? (see the asserts below)
Of course this potentially create empty arrays but that is the caller responsibility to do something if it should be avoided.

This comment has been minimized.

Copy link
@thomaswue

thomaswue Jan 13, 2015

Contributor

Yes, this fix is correct. I fixed this in the code but forgot to update the comment.

* @param end the end index (exclusive), must be >= 0 and <= source.length and >= start
* @return a newly allocated array with the extracted elements and length (end - start)
*/
Expand All @@ -45,7 +45,7 @@ private static boolean checkExtractRangeArgs(int[] source, int start, int end) {
/**
* Extracts part of an array into a newly allocated Object[] array. Does not perform safety checks on parameters.
* @param source the source array whose values should be extracted
* @param start the start index, must be >= 0 and < source.length
* @param start the start index, must be >= 0 and <= source.length
* @param end the end index (exclusive), must be >= 0 and <= source.length and >= start
* @return a newly allocated array with the extracted elements and length (end - start)
*/
Expand All @@ -69,7 +69,7 @@ private static boolean checkExtractRangeArgs(long[] source, int start, int end)
/**
* Extracts part of an array into a newly allocated Object[] array. Does not perform safety checks on parameters.
* @param source the source array whose values should be extracted
* @param start the start index, must be >= 0 and < source.length
* @param start the start index, must be >= 0 and <= source.length
* @param end the end index (exclusive), must be >= 0 and <= source.length and >= start
* @return a newly allocated array with the extracted elements and length (end - start)
*/
Expand All @@ -93,7 +93,7 @@ private static boolean checkExtractRangeArgs(double[] source, int start, int end
/**
* Extracts part of an array into a newly allocated Object[] array. Does not perform safety checks on parameters.
* @param source the source array whose values should be extracted
* @param start the start index, must be >= 0 and < source.length
* @param start the start index, must be >= 0 and <= source.length
* @param end the end index (exclusive), must be >= 0 and <= source.length and >= start
* @return a newly allocated array with the extracted elements and length (end - start)
*/
Expand Down

0 comments on commit 66aec1f

Please sign in to comment.