-
Notifications
You must be signed in to change notification settings - Fork 21
HashMap getOrElseUpdate changed behaviour in 2.12.1 if the callback modifies the map #10187
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
Comments
Imported From: https://issues.scala-lang.org/browse/SI-10187?orig=1 |
@retronym said: I'm in favour of explicitly documenting the "don't mutate in the callback" restriction. Maybe this could be documented as a general note for mutable collections that results are undefined if they are mutated during queries or iteration. But I'd be happy to revert to the current behaviour in the 2.12.x series to be conservative. |
@Ichoran said: |
@szeiger said: Documenting the new behavior as the default in 2.13 (and reverting in 2.12) would be a natural choice. OTOH, getOrElseUpdate is much more convenient than doing the same thing manually, so it's understandable why people would want to use it even though they do not care about the improved performance (with the limitation of not being allowed to modify the map in the callback). The only obvious places in the Scala repo where the map is mutated while computing the default value are in ProdConsAnalyzerImpl.scala and SetMapConsistencyTest.scala, in both cases using an AnyRefMap. The latter is a regression test for #8213 where the new behavior that we now have in HashMap was classified as a bug and consequently fixed for AnyRefMap. For the sake of consistency I suggest that we revert the change and allow mutation. Since the motivation for #10049 was performance of groupBy, we could explore a more directed fix, e.g. overriding groupBy in HashMap and using a private copy of getOrElseUpdate with the new semantics. |
@retronym said: |
These used to both be true in 2.12.0 and earlier. The change to optimize
HashMap#getOrElseUpdate
in 2.12.1 uses stale values in theupdate
part.Note that Java 9 has explicitly specced
Map#computeIfAbsent
to outlaw mutation of the map in the callback.The text was updated successfully, but these errors were encountered: