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

Make Stream.withFilter.{map,flatMap} run in constant stack space #1167

Merged
merged 5 commits into from Aug 22, 2012

Conversation

Blaisorblade
Copy link
Contributor

The included test currently fails because map and flatMap do not
run in constant stack space on a stream returned by Stream.withFilter,
as I reported here:
https://groups.google.com/d/msg/scala-language/WqJR38REXnk/saaSiDdmyqoJ

Fix the problem and add a simple testcase.

Note that the stack space consumed when producing an element of this stream is
proportional to the number of elements failing the test before the next
success. The stack space consumed to produce the stream itself is the
space needed to produce the first element, that is, is proportional to
the number of failures before the first success.

Review by @axel22.

The included test currently fails because `map` and `flatMap` do not
run in constant stack space on a stream returned by `Stream.withFilter`,
as I reported here:
https://groups.google.com/d/msg/scala-language/WqJR38REXnk/saaSiDdmyqoJ

Fix the problem and add a simple testcase.

Note that the stack space consumed when producing an element of this stream is
proportional to the number of elements failing the test before the next
success. The stack space consumed to produce the stream itself is the
space needed to produce the first element, that is, is proportional to
the number of failures before the first success.
assert(resMap1.isEmpty)
assert(resFMap1.isEmpty)
println(resMap1)
println(resFMap1)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second thought, I could remove these lines and have no output at all. I'll push a new commit if this is preferred.

@scala-jenkins
Copy link

Started jenkins job pr-rangepos at https://scala-webapps.epfl.ch/jenkins/job/pr-rangepos/249/

@scala-jenkins
Copy link

Started jenkins job pr-scala-testsuite-linux-opt at https://scala-webapps.epfl.ch/jenkins/job/pr-scala-testsuite-linux-opt/956/

assert(resFMap1.isEmpty)
println(resMap1)
println(resFMap1)
//This will cause a stack overflow
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I meant "with the previous implementation".

No need to check the output - checking programmatically that the
produced streams are empty is enough.
)
*/
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commenting out code instead of removing it was not my intention. Will fix.

@scala-jenkins
Copy link

jenkins job pr-scala-testsuite-linux-opt: Success - https://scala-webapps.epfl.ch/jenkins/job/pr-scala-testsuite-linux-opt/956/

@scala-jenkins
Copy link

jenkins job pr-rangepos: Success - https://scala-webapps.epfl.ch/jenkins/job/pr-rangepos/249/

@Blaisorblade
Copy link
Contributor Author

PLS REBUILD ALL

@Blaisorblade
Copy link
Contributor Author

Note: the test results are, strictly speaking, not valid any more, even though the changes are minor. I would have deleted the comments, as required by the pull request policy, but I don't have permissions for that.

@scala-jenkins
Copy link

Started jenkins job pr-rangepos at https://scala-webapps.epfl.ch/jenkins/job/pr-rangepos/255/

@scala-jenkins
Copy link

Started jenkins job pr-scala-testsuite-linux-opt at https://scala-webapps.epfl.ch/jenkins/job/pr-scala-testsuite-linux-opt/962/

@scala-jenkins
Copy link

jenkins job pr-rangepos: Success - https://scala-webapps.epfl.ch/jenkins/job/pr-rangepos/255/

@scala-jenkins
Copy link

jenkins job pr-scala-testsuite-linux-opt: Success - https://scala-webapps.epfl.ch/jenkins/job/pr-scala-testsuite-linux-opt/962/

@axel22
Copy link
Member

axel22 commented Aug 22, 2012

I believe this would benefit from additional correctness test cases such as calling withFilter and map/flatMap on the:

  • empty stream
  • one-element stream
  • two-element stream

In all cases, the predicate for withFilter should a) always return true, b) always return false, c) _ % 2 == 0, d) _ % 3 == 0.
In all cases, the result should be compared against the expected result to see if the correct stream elements are returned.

After that, LGTM.

@Blaisorblade
Copy link
Contributor Author

I wrote the test case you asked for - however, the "expected" result is computed by converting the stream to a Seq (and checking that the result is right) and using filter, map and flatMap on Seq. I thus assume that those functions on Seq are are tested independently. If that's a problem I could list instead the results explicitly, even though that would make additional testcases harder to add. Or I could additionally print the results (and have a corresponding .check file).

@Blaisorblade
Copy link
Contributor Author

PLS REBUILD ALL

@scala-jenkins
Copy link

Started jenkins job pr-rangepos at https://scala-webapps.epfl.ch/jenkins/job/pr-rangepos/266/

@scala-jenkins
Copy link

Started jenkins job pr-scala-testsuite-linux-opt at https://scala-webapps.epfl.ch/jenkins/job/pr-scala-testsuite-linux-opt/973/

@axel22
Copy link
Member

axel22 commented Aug 22, 2012

LGTM

@scala-jenkins
Copy link

jenkins job pr-scala-testsuite-linux-opt: Success - https://scala-webapps.epfl.ch/jenkins/job/pr-scala-testsuite-linux-opt/973/

@scala-jenkins
Copy link

jenkins job pr-rangepos: Success - https://scala-webapps.epfl.ch/jenkins/job/pr-rangepos/266/

gkossakowski added a commit that referenced this pull request Aug 22, 2012
Make Stream.withFilter.{map,flatMap} run in constant stack space
@gkossakowski gkossakowski merged commit 6e344bc into scala:2.10.x Aug 22, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants