Skip to content

Commit 3cabf84

Browse files
author
Antoine Brunner
committed
Replace itToArray by Iterator.copyToArray(...)
1 parent 6ee2edb commit 3cabf84

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

library/src/scala/runtime/DynamicTuple.scala

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,6 @@ object DynamicTuple {
66
inline val MaxSpecialized = 22
77
inline private val XXL = MaxSpecialized + 1
88

9-
def itToArray(it: Iterator[Any], size: Int, dest: Array[Object], offset: Int): Unit = {
10-
var i = 0
11-
while (i < size) {
12-
dest(offset + i) = it.next().asInstanceOf[Object]
13-
i += 1
14-
}
15-
}
16-
179
def dynamicToArray(self: Tuple): Array[Object] = (self: Any) match {
1810
case self: Unit => Array.emptyObjectArray
1911
case self: TupleXXL => self.toArray
@@ -254,16 +246,15 @@ object DynamicTuple {
254246
TupleXXL.fromIArray(arr.asInstanceOf[IArray[Object]]).asInstanceOf[H *: This]
255247
}
256248

257-
def dynamicCons[H, This <: Tuple](x: H, self: This): H *: This = {
258-
(self: Any) match {
259-
case xxl: TupleXXL => xxlCons(x, xxl)
260-
case _ => specialCaseCons(x, self)
261-
}
249+
def dynamicCons[H, This <: Tuple](x: H, self: This): H *: This = (self: Any) match {
250+
case xxl: TupleXXL => xxlCons(x, xxl)
251+
case _ => specialCaseCons(x, self)
262252
}
263253

264254
def dynamicConcat[This <: Tuple, That <: Tuple](self: This, that: That): Concat[This, That] = {
265255
type Result = Concat[This, That]
266256

257+
// If either of the tuple is empty, we can leave early
267258
(self: Any) match {
268259
case self: Unit => return that.asInstanceOf[Result]
269260
case _ =>
@@ -280,7 +271,8 @@ object DynamicTuple {
280271
case xxl: TupleXXL =>
281272
System.arraycopy(xxl.elems, 0, array, offset, tuple.size)
282273
case _ =>
283-
itToArray(tuple.asInstanceOf[Product].productIterator, tuple.size, array, offset)
274+
tuple.asInstanceOf[Product].productIterator.asInstanceOf[Iterator[Object]]
275+
.copyToArray(array, offset, tuple.size)
284276
}
285277

286278
copyToArray(self, arr, 0)
@@ -359,11 +351,9 @@ object DynamicTuple {
359351
}
360352
}
361353

362-
def dynamicTail[This <: NonEmptyTuple](self: This): Tail[This] = {
363-
(self: Any) match {
364-
case xxl: TupleXXL => xxlTail(xxl)
365-
case _ => specialCaseTail(self)
366-
}
354+
def dynamicTail[This <: NonEmptyTuple](self: This): Tail[This] = (self: Any) match {
355+
case xxl: TupleXXL => xxlTail(xxl)
356+
case _ => specialCaseTail(self)
367357
}
368358

369359
def dynamicApply[This <: NonEmptyTuple, N <: Int] (self: This, n: Int): Elem[This, N] = {
@@ -375,7 +365,8 @@ object DynamicTuple {
375365
res.asInstanceOf[Result]
376366
}
377367

378-
def zipIt(it1: Iterator[Any], it2: Iterator[Any], size: Int): IArray[Object] = {
368+
// Benchmarks showed that this is faster than doing (it1 zip it2).copyToArray(...)
369+
def zipIterators(it1: Iterator[Any], it2: Iterator[Any], size: Int): IArray[Object] = {
379370
val arr = new Array[Object](size)
380371
var i = 0
381372
while (i < size) {
@@ -388,7 +379,13 @@ object DynamicTuple {
388379
def dynamicZip[This <: Tuple, T2 <: Tuple](t1: This, t2: T2): Zip[This, T2] = {
389380
if (t1.size == 0 || t2.size == 0) return ().asInstanceOf[Zip[This, T2]]
390381
val size = Math.min(t1.size, t2.size)
391-
Tuple.fromIArray(zipIt(t1.asInstanceOf[Product].productIterator, t2.asInstanceOf[Product].productIterator, size)).asInstanceOf[Zip[This, T2]]
382+
Tuple.fromIArray(
383+
zipIterators(
384+
t1.asInstanceOf[Product].productIterator,
385+
t2.asInstanceOf[Product].productIterator,
386+
size
387+
)
388+
).asInstanceOf[Zip[This, T2]]
392389
}
393390

394391
def specialCaseMap[This <: Tuple, F[_]](self: This, f: [t] => t => F[t]): Map[This, F] = {
@@ -448,7 +445,9 @@ object DynamicTuple {
448445
type Result = Map[This, F]
449446
(self: Any) match {
450447
case xxl: TupleXXL =>
451-
TupleXXL.fromIArray(xxl.elems.asInstanceOf[Array[Object]].map(f[Object]).asInstanceOf[IArray[Object]]).asInstanceOf[Result]
448+
TupleXXL.fromIArray(
449+
xxl.elems.asInstanceOf[Array[Object]].map(f[Object]).asInstanceOf[IArray[Object]]
450+
).asInstanceOf[Result]
452451
case _ =>
453452
specialCaseMap(self, f)
454453
}

0 commit comments

Comments
 (0)