Skip to content

Commit f517abe

Browse files
author
Antoine Brunner
committed
Trying to improve tupleZip
1 parent 86ca325 commit f517abe

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

library/src/scala/runtime/DynamicTuple.scala

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -379,12 +379,20 @@ object DynamicTuple {
379379
res.asInstanceOf[Result]
380380
}
381381

382+
def zipIt(it1: Iterator[Any], it2: Iterator[Any], size: Int): IArray[Object] = {
383+
val arr = new Array[Object](size)
384+
var i = 0
385+
while (i < size) {
386+
arr(i) = (it1.next(), it2.next())
387+
i += 1
388+
}
389+
arr.asInstanceOf[IArray[Object]]
390+
}
391+
382392
def dynamicZip[This <: Tuple, T2 <: Tuple](t1: This, t2: T2): Zip[This, T2] = {
383-
if (t1.size == 0 || t2.size == 0) ().asInstanceOf[Zip[This, T2]]
384-
else Tuple.fromArray(
385-
t1.asInstanceOf[Product].productIterator.zip(
386-
t2.asInstanceOf[Product].productIterator).toArray // TODO use toIArray of Object to avoid double/triple array copy
387-
).asInstanceOf[Zip[This, T2]]
393+
if (t1.size == 0 || t2.size == 0) return ().asInstanceOf[Zip[This, T2]]
394+
val size = Math.min(t1.size, t2.size)
395+
Tuple.fromIArray(zipIt(t1.asInstanceOf[Product].productIterator, t2.asInstanceOf[Product].productIterator, size)).asInstanceOf[Zip[This, T2]]
388396
}
389397

390398
def specialCaseMap[This <: Tuple, F[_]](self: This, f: [t] => t => F[t]): Map[This, F] = {

0 commit comments

Comments
 (0)