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
Performance Enhancement: HashMap creates a Some object on every lookup, even if using apply #4469
Comments
Imported From: https://issues.scala-lang.org/browse/SI-4469?orig=1
|
@magarciaEPFL said: It would be great if you could provide details about an implementation of |
@ijuma said:
I'm not the original reporter, but the usual way to do this is to have an internal method that returns null instead of None. Then get can wrap it and there is no allocation in apply. A bit ugly, but that's the price of efficiency in this case. Of course, if you want to support null keys, then it becomes a bit more complicated. See java.util.HashMap for how they do that. I'd say that supporting null keys in Maps is a bad idea and ConcurrentHashMap in Java does not support them. |
@paulp said: |
Commit Message Bot (anonymous) said: |
Every look-up in a HashMap (mutable or immutable) creates a Some object to wrap the result. Event when calling apply as:
the Some is created and then immediately removed. This means doing lots of HashMap look-ups in a loop will lead to GC needing to run and a significant slowdown.
It would be great if the HashMap (and maybe others) could override apply to not do any object creation and only create a Some when using get.
If you run:
and then:
And look at the output, you will see a huge number of totally unnecessary scala.Some objects being created.
The text was updated successfully, but these errors were encountered: