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

map.updated returns unnecessary new instance #5139

Closed
scabug opened this issue Nov 2, 2011 · 7 comments
Closed

map.updated returns unnecessary new instance #5139

scabug opened this issue Nov 2, 2011 · 7 comments
Labels
Milestone

Comments

@scabug
Copy link

scabug commented Nov 2, 2011

`scala> val m=Map(1->2)
m: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2)

scala> val n=m.updated(1,2)
n: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2)

scala> m eq n
res16: Boolean = false

scala> m == n
res17: Boolean = true`

You would expect updated to only create a new instance if the value is indeed a different value than what is already in the map. This should be pretty easy to implement by checking for reference equality in the updated0 implementation methods of HashMap, HashMap1 and HashTrieMap.

@scabug
Copy link
Author

scabug commented Nov 2, 2011

Imported From: https://issues.scala-lang.org/browse/SI-5139?orig=1
Reporter: @rklaehn
Assignee: @rklaehn
Affected Versions: 2.9.1
See #4398
Attachments:

@scabug
Copy link
Author

scabug commented Nov 4, 2011

@rklaehn said:
Here is a modified HashMap from scala.collections.immutable that fixes the behavior, and a small test.

I'm not sure what to do about the .asInstanceOf[AnyRef]. Since HashMap1 and HashTrieMap are not specialized, it should not matter since the values will be boxed anyway, right?

@scabug
Copy link
Author

scabug commented Nov 5, 2011

@rklaehn said:
Map.remove (- operator) also is affected. Fixing this should also be just a one line fix.

scala> val a=Map((1 to 10).map(x=>x->x):_*)
a: scala.collection.immutable.Map[Int,Int] = Map(5 -> 5, 10 -> 10, 1 -> 1, 6 -> 6, 9 -> 9, 2 -> 2, 7 -> 7, 3 -> 3, 8 -> 8, 4 -> 4)

scala> val b=a-10000
b: scala.collection.immutable.Map[Int,Int] = Map(5 -> 5, 10 -> 10, 1 -> 1, 6 -> 6, 9 -> 9, 2 -> 2, 7 -> 7, 3 -> 3, 8 -> 8, 4 -> 4)

scala> a eq b
res36: Boolean = false

scala> a == b
res37: Boolean = true

@scabug
Copy link
Author

scabug commented Feb 4, 2012

@dcsobral said:
So, if I assign an AST tree to an existing key in a map, it will traverse all AST just to check whether it is equal or not to the exiting value? That looks extremely suboptimal to me. Or are you just doing reference equality checks? I tried to check what your patch does, but you did not submit a patch -- you submitted the whole edited file. Don't do that, please -- diff was invented 40 years ago, so there's no reason not to use it.

@scabug
Copy link
Author

scabug commented Feb 4, 2012

@rklaehn said:
Hi Daniel. I am just doing a reference equality check. The changes are pretty similar to what was done on scala.collection.immutable.Set in response to #4398. Sorry about not submitting a patch.

@scabug
Copy link
Author

scabug commented Aug 9, 2012

@rklaehn said:
This was fixed in the process of fixing #5879 (which is not yet completely fixed, but anyway).

So I think this can be closed.

@scabug
Copy link
Author

scabug commented Aug 9, 2012

@rklaehn said:
Fixed by rework of updated0 in #5879

@scabug scabug closed this as completed Aug 9, 2012
@scabug scabug added the library label Apr 7, 2017
@scabug scabug added this to the 2.10.0-M6 milestone Apr 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant