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

Commit 12ee956

Browse files
committed
Update according to review comment
1 parent f86e69d commit 12ee956

File tree

2 files changed

+30
-21
lines changed

2 files changed

+30
-21
lines changed

benchmarks/time/src/main/scala/strawman/collection/immutable/ImmutableArrayBenchmark.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,16 @@ class ImmutableArrayBenchmark {
219219
}
220220
}
221221

222+
@Benchmark
223+
@OperationsPerInvocation(100)
224+
def access_slice_empty(bh: Blackhole): Unit = {
225+
var i = 0
226+
while (i < 100) {
227+
bh.consume(xs.slice(size, size - size / (i + 1)))
228+
i += 1
229+
}
230+
}
231+
222232
@Benchmark
223233
@OperationsPerInvocation(1000)
224234
def transform_updateLast(bh: Blackhole): Unit = {

collections/src/main/scala/strawman/collection/immutable/ImmutableArray.scala

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,11 @@ object ImmutableArray extends StrictOptimizedClassTagSeqFactory[ImmutableArray]
188188
case that: ofRef[_] => Arrays.equals(unsafeArray.asInstanceOf[Array[AnyRef]], that.unsafeArray.asInstanceOf[Array[AnyRef]])
189189
case _ => super.equals(that)
190190
}
191-
override def slice(from: Int, until: Int): ImmutableArray[T] = {
191+
override def slice(from: Int, until: Int): ImmutableArray.ofRef[T] = {
192192
val lo = scala.math.max(from, 0)
193193
val hi = scala.math.min(until, length)
194194
if (lo >= hi)
195-
emptyImpl
195+
emptyImpl.asInstanceOf[ofRef[T]]
196196
else
197197
new ofRef(Arrays.copyOfRange[T](unsafeArray, lo, hi))
198198
}
@@ -213,7 +213,7 @@ object ImmutableArray extends StrictOptimizedClassTagSeqFactory[ImmutableArray]
213213
val lo = scala.math.max(from, 0)
214214
val hi = scala.math.min(until, length)
215215
if (lo >= hi)
216-
emptyImpl
216+
new ofByte(Array.emptyByteArray)
217217
else
218218
new ofByte(Arrays.copyOfRange(unsafeArray, lo, hi))
219219
}
@@ -230,11 +230,11 @@ object ImmutableArray extends StrictOptimizedClassTagSeqFactory[ImmutableArray]
230230
case that: ofShort => Arrays.equals(unsafeArray, that.unsafeArray)
231231
case _ => super.equals(that)
232232
}
233-
override def slice(from: Int, until: Int): ImmutableArray[Short] = {
233+
override def slice(from: Int, until: Int): ImmutableArray.ofShort = {
234234
val lo = scala.math.max(from, 0)
235235
val hi = scala.math.min(until, length)
236236
if (lo >= hi)
237-
emptyImpl
237+
new ofShort(Array.emptyShortArray)
238238
else
239239
new ofShort(Arrays.copyOfRange(unsafeArray, lo, hi))
240240
}
@@ -251,11 +251,11 @@ object ImmutableArray extends StrictOptimizedClassTagSeqFactory[ImmutableArray]
251251
case that: ofChar => Arrays.equals(unsafeArray, that.unsafeArray)
252252
case _ => super.equals(that)
253253
}
254-
override def slice(from: Int, until: Int): ImmutableArray[Char] = {
254+
override def slice(from: Int, until: Int): ImmutableArray.ofChar = {
255255
val lo = scala.math.max(from, 0)
256256
val hi = scala.math.min(until, length)
257257
if (lo >= hi)
258-
emptyImpl
258+
new ofChar(Array.emptyCharArray)
259259
else
260260
new ofChar(Arrays.copyOfRange(unsafeArray, lo, hi))
261261
}
@@ -272,11 +272,11 @@ object ImmutableArray extends StrictOptimizedClassTagSeqFactory[ImmutableArray]
272272
case that: ofInt => Arrays.equals(unsafeArray, that.unsafeArray)
273273
case _ => super.equals(that)
274274
}
275-
override def slice(from: Int, until: Int): ImmutableArray[Int] = {
275+
override def slice(from: Int, until: Int): ImmutableArray.ofInt = {
276276
val lo = scala.math.max(from, 0)
277277
val hi = scala.math.min(until, length)
278278
if (lo >= hi)
279-
emptyImpl
279+
new ofInt(Array.emptyIntArray)
280280
else
281281
new ofInt(Arrays.copyOfRange(unsafeArray, lo, hi))
282282
}
@@ -293,11 +293,11 @@ object ImmutableArray extends StrictOptimizedClassTagSeqFactory[ImmutableArray]
293293
case that: ofLong => Arrays.equals(unsafeArray, that.unsafeArray)
294294
case _ => super.equals(that)
295295
}
296-
override def slice(from: Int, until: Int): ImmutableArray[Long] = {
296+
override def slice(from: Int, until: Int): ImmutableArray.ofLong = {
297297
val lo = scala.math.max(from, 0)
298298
val hi = scala.math.min(until, length)
299299
if (lo >= hi)
300-
emptyImpl
300+
new ofLong(Array.emptyLongArray)
301301
else
302302
new ofLong(Arrays.copyOfRange(unsafeArray, lo, hi))
303303
}
@@ -314,11 +314,11 @@ object ImmutableArray extends StrictOptimizedClassTagSeqFactory[ImmutableArray]
314314
case that: ofFloat => Arrays.equals(unsafeArray, that.unsafeArray)
315315
case _ => super.equals(that)
316316
}
317-
override def slice(from: Int, until: Int): ImmutableArray[Float] = {
317+
override def slice(from: Int, until: Int): ImmutableArray.ofFloat = {
318318
val lo = scala.math.max(from, 0)
319319
val hi = scala.math.min(until, length)
320320
if (lo >= hi)
321-
emptyImpl
321+
new ofFloat(Array.emptyFloatArray)
322322
else
323323
new ofFloat(Arrays.copyOfRange(unsafeArray, lo, hi))
324324
}
@@ -335,11 +335,11 @@ object ImmutableArray extends StrictOptimizedClassTagSeqFactory[ImmutableArray]
335335
case that: ofDouble => Arrays.equals(unsafeArray, that.unsafeArray)
336336
case _ => super.equals(that)
337337
}
338-
override def slice(from: Int, until: Int): ImmutableArray[Double] = {
338+
override def slice(from: Int, until: Int): ImmutableArray.ofDouble = {
339339
val lo = scala.math.max(from, 0)
340340
val hi = scala.math.min(until, length)
341341
if (lo >= hi)
342-
emptyImpl
342+
new ofDouble(new Array[Double](0))
343343
else
344344
new ofDouble(Arrays.copyOfRange(unsafeArray, lo, hi))
345345
}
@@ -356,11 +356,11 @@ object ImmutableArray extends StrictOptimizedClassTagSeqFactory[ImmutableArray]
356356
case that: ofBoolean => Arrays.equals(unsafeArray, that.unsafeArray)
357357
case _ => super.equals(that)
358358
}
359-
override def slice(from: Int, until: Int): ImmutableArray[Boolean] = {
359+
override def slice(from: Int, until: Int): ImmutableArray.ofBoolean = {
360360
val lo = scala.math.max(from, 0)
361361
val hi = scala.math.min(until, length)
362362
if (lo >= hi)
363-
emptyImpl
363+
new ofBoolean(Array.emptyBooleanArray)
364364
else
365365
new ofBoolean(Arrays.copyOfRange(unsafeArray, lo, hi))
366366
}
@@ -377,18 +377,17 @@ object ImmutableArray extends StrictOptimizedClassTagSeqFactory[ImmutableArray]
377377
case that: ofUnit => unsafeArray.length == that.unsafeArray.length
378378
case _ => super.equals(that)
379379
}
380-
override def slice(from: Int, until: Int): ImmutableArray[Unit] = {
380+
override def slice(from: Int, until: Int): ImmutableArray.ofUnit = {
381381
// cant use
382382
// new ofUnit(util.Arrays.copyOfRange[Unit](array, from, until)) - Unit is special and doesnt compile
383383
// cant use util.Arrays.copyOfRange[Unit](repr, from, until) - Unit is special and doesnt compile
384384
val lo = scala.math.max(from, 0)
385385
val hi = scala.math.min(until, length)
386386
val slicedLength = hi - lo
387387
if (slicedLength <= 0)
388-
emptyImpl
388+
new ofUnit(new Array[Unit](0))
389389
else {
390-
val res = new Array[Unit](slicedLength)
391-
new ofUnit(res)
390+
new ofUnit(new Array[Unit](slicedLength))
392391
}
393392
}
394393
}

0 commit comments

Comments
 (0)