Skip to content

Replace JoinIterator & improve ConcatIterator#5033

Merged
adriaanm merged 1 commit intoscala:2.12.xfrom
szeiger:issue/9623-2.12
Mar 30, 2016
Merged

Replace JoinIterator & improve ConcatIterator#5033
adriaanm merged 1 commit intoscala:2.12.xfrom
szeiger:issue/9623-2.12

Conversation

@szeiger
Copy link
Contributor

@szeiger szeiger commented Mar 11, 2016

This is a follow-up to #4937 (the "SI-9623 fix" mentioned below). Review by @Ichoran.

The new ConcatIterator requires only one extra lightweight wrapper
object (cons cell) to be allocated compared to JoinIterator. All
additional concatenations are then done in place with one cons cell per
appended iterator.

Running 1000000 iterations of the following benchmark for LHS recursion:

def lhs(n: Int) =
  (1 to n).foldLeft(Iterator.empty: Iterator[Int])((res, _) => res ++ Iterator(1)).sum

On 2.12.x before SI-9623 fix:

$ ../scala/build-sbt/quick/bin/scala -J-Xmx1024M -nc concatit.scala
1000000: 555ms
1000000: 344ms
1000000: 397ms
1000000: 309ms
1000000: 290ms
1000000: 283ms
1000000: 282ms
1000000: 281ms
1000000: 290ms
1000000: 279ms

With SI-9623 fix:

$ ../scala/build-sbt/quick/bin/scala -J-Xmx1024M -nc concatit.scala
1000000: 610ms
1000000: 324ms
1000000: 387ms
1000000: 315ms
1000000: 296ms
1000000: 300ms
1000000: 341ms
1000000: 294ms
1000000: 291ms
1000000: 281ms

With this version:

$ ../scala/build-sbt/quick/bin/scala -J-Xmx1024M -nc concatit.scala
1000000: 362ms
1000000: 162ms
1000000: 140ms
1000000: 150ms
1000000: 110ms
1000000: 57ms
1000000: 79ms
1000000: 109ms
1000000: 120ms
1000000: 49ms

And for RHS recursion:

def rhs(n: Int) =
  (1 to n).foldLeft(Iterator.empty: Iterator[Int])((res, _) => Iterator(1) ++ res).sum

On 2.12.x before SI-9623 fix:

StackOverflowError

With SI-9623 fix:

StackOverflowError

With this version:

$ ../scala/build-sbt/quick/bin/scala -J-Xmx1024M -nc concatit.scala
1000000: 3156ms
1000000: 1536ms
1000000: 1240ms
1000000: 1575ms
1000000: 439ms
1000000: 706ms
1000000: 1043ms
1000000: 1211ms
1000000: 515ms
1000000: 314ms

@scala-jenkins scala-jenkins added this to the 2.12.0-M4 milestone Mar 11, 2016
The new `ConcatIterator` requires only one extra lightweight wrapper
object (cons cell) to be allocated compared to `JoinIterator`. All
additional concatenations are then done in place with one cons cell per
appended iterator.

Running 1000000 iterations of the following benchmark for LHS recursion:

```
def lhs(n: Int) =
  (1 to n).foldLeft(Iterator.empty: Iterator[Int])((res, _) => res ++ Iterator(1)).sum
```

On 2.12.x before SI-9623 fix:

```
$ ../scala/build-sbt/quick/bin/scala -J-Xmx1024M -nc concatit.scala
1000000: 555ms
1000000: 344ms
1000000: 397ms
1000000: 309ms
1000000: 290ms
1000000: 283ms
1000000: 282ms
1000000: 281ms
1000000: 290ms
1000000: 279ms
```

With SI-9623 fix:

```
$ ../scala/build-sbt/quick/bin/scala -J-Xmx1024M -nc concatit.scala
1000000: 610ms
1000000: 324ms
1000000: 387ms
1000000: 315ms
1000000: 296ms
1000000: 300ms
1000000: 341ms
1000000: 294ms
1000000: 291ms
1000000: 281ms
```

With this version:

```
$ ../scala/build-sbt/quick/bin/scala -J-Xmx1024M -nc concatit.scala
1000000: 362ms
1000000: 162ms
1000000: 140ms
1000000: 150ms
1000000: 110ms
1000000: 57ms
1000000: 79ms
1000000: 109ms
1000000: 120ms
1000000: 49ms
```

And for RHS recursion:

```
def rhs(n: Int) =
  (1 to n).foldLeft(Iterator.empty: Iterator[Int])((res, _) => Iterator(1) ++ res).sum
```

On 2.12.x before SI-9623 fix:

```
StackOverflowError
```

With SI-9623 fix:

```
StackOverflowError
```

With this version:

```
$ ../scala/build-sbt/quick/bin/scala -J-Xmx1024M -nc concatit.scala
1000000: 3156ms
1000000: 1536ms
1000000: 1240ms
1000000: 1575ms
1000000: 439ms
1000000: 706ms
1000000: 1043ms
1000000: 1211ms
1000000: 515ms
1000000: 314ms
```
@adriaanm
Copy link
Contributor

Since this was reviewed before, and the followup comment regarding nulling was implemented, I'm going to merge this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants