diff options
author | George Leontiev <folone@gmail.com> | 2013-07-30 11:06:26 +0200 |
---|---|---|
committer | George Leontiev <folone@gmail.com> | 2013-08-08 20:38:24 +0200 |
commit | b741e8ada8d8ba716af791868ba1222cd74255e4 (patch) | |
tree | 06c6af67ecf12a97e730bf926398744f947af9f4 /src/library | |
parent | b145cb1c5d98cf7b01a145fa38c3e412e59f81b6 (diff) | |
download | scala-b741e8ada8d8ba716af791868ba1222cd74255e4.tar.gz scala-b741e8ada8d8ba716af791868ba1222cd74255e4.tar.bz2 scala-b741e8ada8d8ba716af791868ba1222cd74255e4.zip |
Make map2Conserve occupy constant stack space in spirit of SI-2411
I recently discovered a StackOverflowError, caused by this function:
https://gist.github.com/folone/7b2f2e2a16314ab28109
The circumstances are pretty extreme, still having a tail-recursive
function seems to be a good idea.
The function behaves the same way old function did (supported by tests),
which is not really how map2 behaves. I did not change this behavior to
not introduce any regression. I actually tried to make it behave like map2,
and it does introduce regression.
Diffstat (limited to 'src/library')
-rw-r--r-- | src/library/scala/collection/immutable/List.scala | 2 |
1 files changed, 2 insertions, 0 deletions
diff --git a/src/library/scala/collection/immutable/List.scala b/src/library/scala/collection/immutable/List.scala index c01694960c..b11368acdf 100644 --- a/src/library/scala/collection/immutable/List.scala +++ b/src/library/scala/collection/immutable/List.scala @@ -161,6 +161,8 @@ sealed abstract class List[+A] extends AbstractSeq[A] * @inheritdoc */ @inline final def mapConserve[B >: A <: AnyRef](f: A => B): List[B] = { + // Note to developers: there exists a duplication between this function and `reflect.internal.util.Collections#map2Conserve`. + // If any successful optimization attempts or other changes are made, please rehash them there too. @tailrec def loop(mapped: ListBuffer[B], unchanged: List[A], pending: List[A]): List[B] = if (pending.isEmpty) { |