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

Consecutive calls of MapLike::filterKeys causes StackOverflowError #9864

Closed
scabug opened this issue Jul 21, 2016 · 2 comments
Closed

Consecutive calls of MapLike::filterKeys causes StackOverflowError #9864

scabug opened this issue Jul 21, 2016 · 2 comments

Comments

@scabug
Copy link

scabug commented Jul 21, 2016

I'm not sure if this even a bug, or just a feature. However:

This code will throw StackOverflowError

var filtered  = Map("1" -> 2)
for(i <- 1 to 10000) {
  filtered = filtered.filterKeys(s => s != "2")
}
println(filtered.get("1"))

This would not:

 var filtered  = Map("1" -> 2)
 val add = Map("2" -> 1)
 for(i <- 1 to 10000) {
    filtered = filtered.filterKeys(_ != "2") ++ add
 }
println(filtered.get("1"))

This will throw as well:

var filtered  = Map("1" -> 2)
val add = Map()
for(i <- 1 to 10000) {
  filtered = filtered.filterKeys(_ != "2") ++ add
}
println(filtered.get("1"))
@scabug
Copy link
Author

scabug commented Jul 21, 2016

Imported From: https://issues.scala-lang.org/browse/SI-9864?orig=1
Reporter: Dmitry Roenko
See #4776, #751

@scabug
Copy link
Author

scabug commented Aug 2, 2016

@SethTisue said:
This is a consequence of filterKeys being lazy, so the lambdas just stack up. I would suggest using filter here since there is no need for the lazy behavior.

If it's surprising that filterKeys is lazy, that's covered by #4776 (and, a while earlier, #751). As for the problem that stacking up lazy operations tends to result in eventual stack overflows, that's a pervasive issue in Scala collections and in Scala generally; ultimately we have the lack of tail calls on the JVM to thank.

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

No branches or pull requests

1 participant