@@ -6,14 +6,6 @@ object DynamicTuple {
6
6
inline val MaxSpecialized = 22
7
7
inline private val XXL = MaxSpecialized + 1
8
8
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
-
17
9
def dynamicToArray (self : Tuple ): Array [Object ] = (self : Any ) match {
18
10
case self : Unit => Array .emptyObjectArray
19
11
case self : TupleXXL => self.toArray
@@ -254,16 +246,15 @@ object DynamicTuple {
254
246
TupleXXL .fromIArray(arr.asInstanceOf [IArray [Object ]]).asInstanceOf [H *: This ]
255
247
}
256
248
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)
262
252
}
263
253
264
254
def dynamicConcat [This <: Tuple , That <: Tuple ](self : This , that : That ): Concat [This , That ] = {
265
255
type Result = Concat [This , That ]
266
256
257
+ // If either of the tuple is empty, we can leave early
267
258
(self : Any ) match {
268
259
case self : Unit => return that.asInstanceOf [Result ]
269
260
case _ =>
@@ -280,7 +271,8 @@ object DynamicTuple {
280
271
case xxl : TupleXXL =>
281
272
System .arraycopy(xxl.elems, 0 , array, offset, tuple.size)
282
273
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)
284
276
}
285
277
286
278
copyToArray(self, arr, 0 )
@@ -359,11 +351,9 @@ object DynamicTuple {
359
351
}
360
352
}
361
353
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)
367
357
}
368
358
369
359
def dynamicApply [This <: NonEmptyTuple , N <: Int ] (self : This , n : Int ): Elem [This , N ] = {
@@ -375,7 +365,8 @@ object DynamicTuple {
375
365
res.asInstanceOf [Result ]
376
366
}
377
367
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 ] = {
379
370
val arr = new Array [Object ](size)
380
371
var i = 0
381
372
while (i < size) {
@@ -388,7 +379,13 @@ object DynamicTuple {
388
379
def dynamicZip [This <: Tuple , T2 <: Tuple ](t1 : This , t2 : T2 ): Zip [This , T2 ] = {
389
380
if (t1.size == 0 || t2.size == 0 ) return ().asInstanceOf [Zip [This , T2 ]]
390
381
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 ]]
392
389
}
393
390
394
391
def specialCaseMap [This <: Tuple , F [_]](self : This , f : [t] => t => F [t]): Map [This , F ] = {
@@ -448,7 +445,9 @@ object DynamicTuple {
448
445
type Result = Map [This , F ]
449
446
(self : Any ) match {
450
447
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 ]
452
451
case _ =>
453
452
specialCaseMap(self, f)
454
453
}
0 commit comments