Zio: StackOverflowError on traversing a Chunk resulting from concatenating others

Created on 4 Nov 2019  路  4Comments  路  Source: zio/zio

Hello , I am new to ZIO .
When i was trying to solves @jdegoes "zio-intro-game" exercises , i encountered a StackOverflowError when playing with Chunk in CatIncremental
In my implementation i was folding/concatenating multiple chunks and StackOverflowError occurred when traversing the resulting chunk .
i made the example below to illustrate the problem

object TestChunk extends App {
  val chunkOk = Chunk.fromIterable(1 to 10000)
  println(chunkOk.fold(0)(_ + _)) // OK

  val chunkKo = (1 to 10000).foldLeft(Chunk[Int]())(_ ++ Chunk.single(_))
  println(chunkKo.fold(0)(_ + _)) // KO StackOverflowError
}

The stack trace error

Exception in thread "main" java.lang.StackOverflowError
    at zio.Chunk$Concat.apply(Chunk.scala:873)
    at zio.Chunk$Concat.apply(Chunk.scala:873)
    at zio.Chunk$Concat.apply(Chunk.scala:873)
         ...

i will try to figure out a fix.

chunk documentation

Most helpful comment

@arism @regiskuckaertz Yes, Chunk's Concat is not stack safe under repeated concatenations currently. It can be made safe by using an explicit stack when traversing the chunk, at the expense of lots of performance probably.

We should document this.

All 4 comments

It works for me on the latest master 馃

Sorry but i still have the same error on the master .
i am using adoptopenjdk ( i tried version 8 and 11 ) with default stack settings( -Xss 1024)
PS : the problem is due to Chunk$Concat.apply witch is not tail recursive .

@arism @regiskuckaertz Yes, Chunk's Concat is not stack safe under repeated concatenations currently. It can be made safe by using an explicit stack when traversing the chunk, at the expense of lots of performance probably.

We should document this.

Resolved by #3754.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

adamgfraser picture adamgfraser  路  4Comments

evis picture evis  路  3Comments

jdegoes picture jdegoes  路  3Comments

jdegoes picture jdegoes  路  4Comments

jdegoes picture jdegoes  路  4Comments