Skip to content
This repository was archived by the owner on Dec 22, 2021. It is now read-only.

Optimize slice operation in ImmutableArray #354

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,16 @@ class ImmutableArrayBenchmark {
}
}

@Benchmark
@OperationsPerInvocation(100)
def access_slice_empty(bh: Blackhole): Unit = {
var i = 0
while (i < 100) {
bh.consume(xs.slice(size, size - size / (i + 1)))
i += 1
}
}

@Benchmark
@OperationsPerInvocation(1000)
def transform_updateLast(bh: Blackhole): Unit = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import scala.reflect.ClassTag
import scala.runtime.ScalaRunTime
import scala.Predef.intWrapper
import java.util.Arrays
import java.lang.System

/**
* An immutable array.
Expand Down Expand Up @@ -103,7 +104,7 @@ sealed abstract class ImmutableArray[+A]

override def dropRight(n: Int): ImmutableArray[A] = ImmutableArray.unsafeWrapArray(new ArrayOps(unsafeArray).dropRight(n))

override def slice(from: Int, until: Int): ImmutableArray[A] = ImmutableArray.unsafeWrapArray(new ArrayOps(unsafeArray).slice(from, until))
override def slice(from: Int, until: Int): ImmutableArray[A]

override def tail: ImmutableArray[A] = ImmutableArray.unsafeWrapArray(new ArrayOps(unsafeArray).tail)

Expand Down Expand Up @@ -187,6 +188,14 @@ object ImmutableArray extends StrictOptimizedClassTagSeqFactory[ImmutableArray]
case that: ofRef[_] => Arrays.equals(unsafeArray.asInstanceOf[Array[AnyRef]], that.unsafeArray.asInstanceOf[Array[AnyRef]])
case _ => super.equals(that)
}
override def slice(from: Int, until: Int): ImmutableArray.ofRef[T] = {
val lo = scala.math.max(from, 0)
val hi = scala.math.min(until, length)
if (lo >= hi)
emptyImpl.asInstanceOf[ofRef[T]]
else
new ofRef(Arrays.copyOfRange[T](unsafeArray, lo, hi))
}
}

@SerialVersionUID(3L)
Expand All @@ -200,6 +209,14 @@ object ImmutableArray extends StrictOptimizedClassTagSeqFactory[ImmutableArray]
case that: ofByte => Arrays.equals(unsafeArray, that.unsafeArray)
case _ => super.equals(that)
}
override def slice(from: Int, until: Int): ImmutableArray[Byte] = {
val lo = scala.math.max(from, 0)
val hi = scala.math.min(until, length)
if (lo >= hi)
new ofByte(Array.emptyByteArray)
else
new ofByte(Arrays.copyOfRange(unsafeArray, lo, hi))
}
}

@SerialVersionUID(3L)
Expand All @@ -213,6 +230,14 @@ object ImmutableArray extends StrictOptimizedClassTagSeqFactory[ImmutableArray]
case that: ofShort => Arrays.equals(unsafeArray, that.unsafeArray)
case _ => super.equals(that)
}
override def slice(from: Int, until: Int): ImmutableArray.ofShort = {
val lo = scala.math.max(from, 0)
val hi = scala.math.min(until, length)
if (lo >= hi)
new ofShort(Array.emptyShortArray)
else
new ofShort(Arrays.copyOfRange(unsafeArray, lo, hi))
}
}

@SerialVersionUID(3L)
Expand All @@ -226,6 +251,14 @@ object ImmutableArray extends StrictOptimizedClassTagSeqFactory[ImmutableArray]
case that: ofChar => Arrays.equals(unsafeArray, that.unsafeArray)
case _ => super.equals(that)
}
override def slice(from: Int, until: Int): ImmutableArray.ofChar = {
val lo = scala.math.max(from, 0)
val hi = scala.math.min(until, length)
if (lo >= hi)
new ofChar(Array.emptyCharArray)
else
new ofChar(Arrays.copyOfRange(unsafeArray, lo, hi))
}
}

@SerialVersionUID(3L)
Expand All @@ -239,6 +272,14 @@ object ImmutableArray extends StrictOptimizedClassTagSeqFactory[ImmutableArray]
case that: ofInt => Arrays.equals(unsafeArray, that.unsafeArray)
case _ => super.equals(that)
}
override def slice(from: Int, until: Int): ImmutableArray.ofInt = {
val lo = scala.math.max(from, 0)
val hi = scala.math.min(until, length)
if (lo >= hi)
new ofInt(Array.emptyIntArray)
else
new ofInt(Arrays.copyOfRange(unsafeArray, lo, hi))
}
}

@SerialVersionUID(3L)
Expand All @@ -252,6 +293,14 @@ object ImmutableArray extends StrictOptimizedClassTagSeqFactory[ImmutableArray]
case that: ofLong => Arrays.equals(unsafeArray, that.unsafeArray)
case _ => super.equals(that)
}
override def slice(from: Int, until: Int): ImmutableArray.ofLong = {
val lo = scala.math.max(from, 0)
val hi = scala.math.min(until, length)
if (lo >= hi)
new ofLong(Array.emptyLongArray)
else
new ofLong(Arrays.copyOfRange(unsafeArray, lo, hi))
}
}

@SerialVersionUID(3L)
Expand All @@ -265,6 +314,14 @@ object ImmutableArray extends StrictOptimizedClassTagSeqFactory[ImmutableArray]
case that: ofFloat => Arrays.equals(unsafeArray, that.unsafeArray)
case _ => super.equals(that)
}
override def slice(from: Int, until: Int): ImmutableArray.ofFloat = {
val lo = scala.math.max(from, 0)
val hi = scala.math.min(until, length)
if (lo >= hi)
new ofFloat(Array.emptyFloatArray)
else
new ofFloat(Arrays.copyOfRange(unsafeArray, lo, hi))
}
}

@SerialVersionUID(3L)
Expand All @@ -278,6 +335,14 @@ object ImmutableArray extends StrictOptimizedClassTagSeqFactory[ImmutableArray]
case that: ofDouble => Arrays.equals(unsafeArray, that.unsafeArray)
case _ => super.equals(that)
}
override def slice(from: Int, until: Int): ImmutableArray.ofDouble = {
val lo = scala.math.max(from, 0)
val hi = scala.math.min(until, length)
if (lo >= hi)
new ofDouble(new Array[Double](0))
else
new ofDouble(Arrays.copyOfRange(unsafeArray, lo, hi))
}
}

@SerialVersionUID(3L)
Expand All @@ -291,6 +356,14 @@ object ImmutableArray extends StrictOptimizedClassTagSeqFactory[ImmutableArray]
case that: ofBoolean => Arrays.equals(unsafeArray, that.unsafeArray)
case _ => super.equals(that)
}
override def slice(from: Int, until: Int): ImmutableArray.ofBoolean = {
val lo = scala.math.max(from, 0)
val hi = scala.math.min(until, length)
if (lo >= hi)
new ofBoolean(Array.emptyBooleanArray)
else
new ofBoolean(Arrays.copyOfRange(unsafeArray, lo, hi))
}
}

@SerialVersionUID(3L)
Expand All @@ -304,5 +377,18 @@ object ImmutableArray extends StrictOptimizedClassTagSeqFactory[ImmutableArray]
case that: ofUnit => unsafeArray.length == that.unsafeArray.length
case _ => super.equals(that)
}
override def slice(from: Int, until: Int): ImmutableArray.ofUnit = {
// cant use
// new ofUnit(util.Arrays.copyOfRange[Unit](array, from, until)) - Unit is special and doesnt compile
// cant use util.Arrays.copyOfRange[Unit](repr, from, until) - Unit is special and doesnt compile
val lo = scala.math.max(from, 0)
val hi = scala.math.min(until, length)
val slicedLength = hi - lo
if (slicedLength <= 0)
new ofUnit(new Array[Unit](0))
else {
new ofUnit(new Array[Unit](slicedLength))
}
}
}
}