Skip to content

Commit 77a4496

Browse files
author
Antoine Brunner
committed
Optimized some branches in take and drop
1 parent 68750ea commit 77a4496

File tree

1 file changed

+19
-22
lines changed

1 file changed

+19
-22
lines changed

library/src/scala/runtime/DynamicTuple.scala

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -270,47 +270,44 @@ object DynamicTuple {
270270

271271
type Result = Take[This, N]
272272

273-
val arr = (self: Any) match {
274-
case xxl: TupleXXL =>
275-
xxl.elems.asInstanceOf[Array[Object]].take(actualN)
276-
case _ =>
277-
if (actualN == 0)
278-
Array.emptyObjectArray
279-
else {
273+
if (actualN == 0) ().asInstanceOf[Result]
274+
else {
275+
val arr = (self: Any) match {
276+
case xxl: TupleXXL =>
277+
xxl.elems.asInstanceOf[Array[Object]].take(actualN)
278+
case _ =>
280279
val arr = new Array[Object](actualN)
281280
self.asInstanceOf[Product].productIterator.asInstanceOf[Iterator[Object]]
282281
.copyToArray(arr, 0, actualN)
283282
arr
284-
}
285-
}
283+
}
286284

287-
dynamicFromIArray(arr.asInstanceOf[IArray[Object]]).asInstanceOf[Result]
285+
dynamicFromIArray(arr.asInstanceOf[IArray[Object]]).asInstanceOf[Result]
286+
}
288287
}
289288

290289
def dynamicDrop[This <: Tuple, N <: Int](self: This, n: N): Drop[This, N] = {
291290
if (n < 0) throw new IndexOutOfBoundsException(n.toString)
292291
val size = self.size
293292
val actualN = Math.min(n, size)
293+
val rem = size - actualN
294294

295295
type Result = Drop[This, N]
296296

297-
val arr = (self: Any) match {
298-
case xxl: TupleXXL =>
299-
xxl.elems.asInstanceOf[Array[Object]].drop(actualN)
300-
case _ =>
301-
val rem = size - actualN
302-
303-
if (rem == 0)
304-
Array.emptyObjectArray
305-
else {
297+
if (rem == 0) ().asInstanceOf[Result]
298+
else {
299+
val arr = (self: Any) match {
300+
case xxl: TupleXXL =>
301+
xxl.elems.asInstanceOf[Array[Object]].drop(actualN)
302+
case _ =>
306303
val arr = new Array[Object](rem)
307304
self.asInstanceOf[Product].productIterator.asInstanceOf[Iterator[Object]]
308305
.drop(actualN).copyToArray(arr, 0, rem)
309306
arr
310-
}
311-
}
307+
}
312308

313-
dynamicFromIArray(arr.asInstanceOf[IArray[Object]]).asInstanceOf[Result]
309+
dynamicFromIArray(arr.asInstanceOf[IArray[Object]]).asInstanceOf[Result]
310+
}
314311
}
315312

316313
def dynamicSplitAt[This <: Tuple, N <: Int](self: This, n: N): Split[This, N] = {

0 commit comments

Comments
 (0)