Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use of +: Stream operator to create long stream results in stack overflow when stream is examined #4697

Closed
scabug opened this issue Jun 15, 2011 · 2 comments
Assignees

Comments

@scabug
Copy link

scabug commented Jun 15, 2011

Typing this into the REPL:

var st = Stream(0)
for (i <- 1 to 10000) st = i +: st
st.take(10000).print

results in:

    ....

7360, 7359, 7358, 7357, 7356, 7355, 7354, 7353, 7352, 7351, 7350, 7349, 7348, 7347, 7346, 7345java.lang.StackOverflowError
at scala.collection.immutable.List.toStream(List.scala:245)
at scala.collection.immutable.List$$anonfun$toStream$1.apply(List.scala:246)
at scala.collection.immutable.List$$anonfun$toStream$1.apply(List.scala:246)
at scala.collection.immutable.Stream$Cons.tail(Stream.scala:630)
at scala.collection.immutable.Stream$Cons.tail(Stream.scala:622)
at scala.collection.immutable.Stream$$anonfun$flatMap$1.apply(Stream.scala:217)
at scala.collection.immutable.Stream$$anonfun$flatMap$1.apply(Stream.scala:217)
at scala.collection.immutable.Stream.append(Stream.scala:80)
at scala.collection.immutable.Stream$$anonfun$append$1.apply(Stream.scala:80)
at scala.collection.immutable.Stream$$anonfun$append$1.apply(Stream.scala:80)
....

Creating a stream of the same size with Stream.cons works fine:

var st = Stream(0)
for (i <- 1 to 10000) { val st2 = st; st = Stream.cons(i,st2) }
st.take(10000).print

I haven't traipsed through the builder mechanism to figure out what's going on with +:, but I would have hoped it could construct a less fragile stream.

@scabug
Copy link
Author

scabug commented Jun 15, 2011

Imported From: https://issues.scala-lang.org/browse/SI-4697?orig=1
Reporter: archontophoenix
Affected Versions: 2.9.0

@scabug
Copy link
Author

scabug commented Jun 21, 2011

Commit Message Bot (anonymous) said:
(extempore in r25130) Routed Stream.+: through cons to avoid some truly pathological
performance. Closes #4697, no review.

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

No branches or pull requests

2 participants