Skip to content

Commit 78a3211

Browse files
author
Antoine Brunner
committed
Try yet another tweak
1 parent fc6a694 commit 78a3211

File tree

3 files changed

+33
-71
lines changed

3 files changed

+33
-71
lines changed

bench-run/src/main/scala/dotty/tools/benchmarks/tuples/Concat.scala

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,10 @@ class Concat {
2626
val size2 = sizes.split(' ')(1).toInt
2727
tuple1 = tupleOfSize(size1)
2828
tuple2 = tupleOfSize(size2)
29-
array1 = Array.fill(size1)("elem")
30-
array2 = Array.fill(size2)("elem")
3129
}
3230

3331
@Benchmark
3432
def tupleConcat(): Tuple = {
3533
DynamicTuple.dynamicConcat(tuple1, tuple2)
3634
}
37-
38-
@Benchmark
39-
def arrayConcat(): Array[Object] = {
40-
DynamicTuple.concat$Array(array1, array2)
41-
}
4235
}

bench-run/src/main/scala/dotty/tools/benchmarks/tuples/Cons.scala

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,10 @@ class Cons {
1616

1717
for (i <- 1 to size)
1818
tuple = "elem" *: tuple
19-
20-
array = Array.fill(size)("elem")
2119
}
2220

2321
@Benchmark
2422
def tupleCons(): Tuple = {
2523
DynamicTuple.dynamicCons("elem", tuple)
2624
}
27-
28-
@Benchmark
29-
def arrayCons(): Array[Object] = {
30-
DynamicTuple.cons$Array("elem", array)
31-
}
3225
}

library/src/scala/runtime/DynamicTuple.scala

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

9-
def to$Array(it: Iterator[Any], n: Int, arr: Array[Object], offset: Int): Unit = {
9+
def itToArray(it: Iterator[Any], size: Int, dest: Array[Object], offset: Int): Unit = {
1010
var i = 0
11-
while (i < n) {
12-
arr(offset + i) = it.next().asInstanceOf[Object]
11+
while (i < size) {
12+
dest(offset + i) = it.next().asInstanceOf[Object]
1313
i += 1
1414
}
1515
}
1616

17-
def cons$Array[H](x: H, elems: Array[Object]): Array[Object] = {
18-
val elems1 = new Array[Object](elems.length + 1)
19-
elems1(0) = x.asInstanceOf[Object]
20-
System.arraycopy(elems, 0, elems1, 1, elems.length)
21-
elems1
22-
}
23-
24-
def concat$Array(a1: Array[Object], a2: Array[Object]): Array[Object] = {
25-
val result = new Array[Object](a1.length + a2.length)
26-
System.arraycopy(a1, 0, result, 0, a1.length)
27-
System.arraycopy(a2, 0, result, a1.length, a2.length)
28-
result
29-
}
30-
31-
def dynamicFromIterator[T <: Tuple](it: Iterator[Object], size: Int): T = size match {
32-
case 1 => Tuple1(it.next()).asInstanceOf[T]
33-
case 2 => Tuple2(it.next(), it.next()).asInstanceOf[T]
34-
case 3 => Tuple3(it.next(), it.next(), it.next()).asInstanceOf[T]
35-
case 4 => Tuple4(it.next(), it.next(), it.next(), it.next()).asInstanceOf[T]
36-
case 5 => Tuple5(it.next(), it.next(), it.next(), it.next(), it.next()).asInstanceOf[T]
37-
case 6 => Tuple6(it.next(), it.next(), it.next(), it.next(), it.next(), it.next()).asInstanceOf[T]
38-
case 7 => Tuple7(it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next()).asInstanceOf[T]
39-
case 8 => Tuple8(it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next()).asInstanceOf[T]
40-
case 9 => Tuple9(it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next()).asInstanceOf[T]
41-
case 10 => Tuple10(it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next()).asInstanceOf[T]
42-
case 11 => Tuple11(it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next()).asInstanceOf[T]
43-
case 12 => Tuple12(it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next()).asInstanceOf[T]
44-
case 13 => Tuple13(it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next()).asInstanceOf[T]
45-
case 14 => Tuple14(it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next()).asInstanceOf[T]
46-
case 15 => Tuple15(it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next()).asInstanceOf[T]
47-
case 16 => Tuple16(it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next()).asInstanceOf[T]
48-
case 17 => Tuple17(it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next()).asInstanceOf[T]
49-
case 18 => Tuple18(it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next()).asInstanceOf[T]
50-
case 19 => Tuple19(it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next()).asInstanceOf[T]
51-
case 20 => Tuple20(it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next()).asInstanceOf[T]
52-
case 21 => Tuple21(it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next()).asInstanceOf[T]
53-
case 22 => Tuple22(it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next()).asInstanceOf[T]
54-
case _ =>
55-
val arr: Array[Object] = new Array[Object](size)
56-
var i = 0
57-
while (i < size) {
58-
arr(i) = it.next()
59-
i += 1
60-
}
61-
TupleXXL.fromIArray(arr.asInstanceOf[IArray[Object]]).asInstanceOf[T]
62-
}
63-
6417
def dynamicFromArray[T <: Tuple](xs: Array[Object]): T = xs.length match {
6518
case 0 => ().asInstanceOf[T]
6619
case 1 => Tuple1(xs(0)).asInstanceOf[T]
@@ -280,8 +233,16 @@ object DynamicTuple {
280233
Tuple21(x, self._1, self._2, self._3, self._4, self._5, self._6, self._7, self._8, self._9, self._10, self._11, self._12, self._13, self._14, self._15, self._16, self._17, self._18, self._19, self._20)
281234
case self: Tuple21[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _] =>
282235
Tuple22(x, self._1, self._2, self._3, self._4, self._5, self._6, self._7, self._8, self._9, self._10, self._11, self._12, self._13, self._14, self._15, self._16, self._17, self._18, self._19, self._20, self._21)
283-
case _ =>
284-
dynamicFromIterator[Result](consIterator(x, self).asInstanceOf[Iterator[Object]], self.size + 1)
236+
case self: Tuple22[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _] =>
237+
val arr = new Array[Object](23)
238+
itToArray(self.asInstanceOf[Product].productIterator, 22, arr, 1)
239+
arr(0) = x.asInstanceOf[Object]
240+
dynamicFromIArray[Result](arr.asInstanceOf[IArray[Object]])
241+
case xxl: TupleXXL =>
242+
val arr = new Array[Object](self.size + 1)
243+
System.arraycopy(xxl.elems, 0, arr, 1, self.size)
244+
arr(0) = x.asInstanceOf[Object]
245+
dynamicFromIArray[Result](arr.asInstanceOf[IArray[Object]])
285246
}
286247
res.asInstanceOf[Result]
287248
}
@@ -296,7 +257,22 @@ object DynamicTuple {
296257
case that: Unit => return self.asInstanceOf[Result]
297258
case _ =>
298259
}
299-
dynamicFromIterator[Result](concatIterator(self, that).asInstanceOf[Iterator[Object]], self.size + that.size)
260+
261+
val arr = new Array[Object](self.size + that.size)
262+
263+
if (self.size <= 22) {
264+
itToArray(self.asInstanceOf[Product].productIterator, self.size, arr, 0)
265+
} else {
266+
System.arraycopy(self.asInstanceOf[TupleXXL].elems, 0, arr, 0, self.size)
267+
}
268+
269+
if (that.size <= 22) {
270+
itToArray(that.asInstanceOf[Product].productIterator, that.size, arr, 0)
271+
} else {
272+
System.arraycopy(that.asInstanceOf[TupleXXL].elems, 0, arr, self.size, that.size)
273+
}
274+
275+
dynamicFromIArray[Result](arr.asInstanceOf[IArray[Object]]).asInstanceOf[Result]
300276
}
301277

302278
def dynamicSize[This <: Tuple](self: This): Size[This] = (self: Any) match {
@@ -351,10 +327,10 @@ object DynamicTuple {
351327
Tuple20(self._2, self._3, self._4, self._5, self._6, self._7, self._8, self._9, self._10, self._11, self._12, self._13, self._14, self._15, self._16, self._17, self._18, self._19, self._20, self._21)
352328
case self: Tuple22[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _] =>
353329
Tuple21(self._2, self._3, self._4, self._5, self._6, self._7, self._8, self._9, self._10, self._11, self._12, self._13, self._14, self._15, self._16, self._17, self._18, self._19, self._20, self._21, self._22)
354-
case _ =>
355-
val it = self.asInstanceOf[Product].productIterator.asInstanceOf[Iterator[Object]]
356-
it.next()
357-
dynamicFromIterator(it, self.size - 1)
330+
case xxl: TupleXXL =>
331+
val arr = new Array[Object](self.size - 1)
332+
System.arraycopy(xxl.elems, 1, arr, 0, self.size - 1)
333+
dynamicFromIArray[Result](arr.asInstanceOf[IArray[Object]])
358334
}
359335
res.asInstanceOf[Result]
360336
}

0 commit comments

Comments
 (0)