Details
-
Bug
-
Status: Resolved
-
Minor
-
Resolution: Won't Fix
-
None
-
None
-
None
-
None
Description
continuing discussion from SOLR-14579
consider the following
mapObject.computeIfAbsent(key, o -> new HashMap<>());
vs.
mapObject.computeIfAbsent(key, Utils.NEW_HASHMAP_FUN)
The first code fragment is executed as following
s.computeIfAbsent(key, new Function() { @Override public Object apply(String key) { return new HashMap<>(); } }
So, there are two problems with this
- A new anonymous inner class is created for that lambda. This one extra class becomes a part of your binary
- a new instance of that class is created everytime the computeIfAbsent() method is invoked, irrespective of whether the value is absent for that key or not. Now imagine that method getting called millions of times and creating millions of such objects for no reason
OTOH
when I use Utils.NEW_HASHMAP_FUN
- Only a single anonymous class is created for the entire codebase
- Only single instance of that object is created in the VM
Ideally, we should go all over the codebase and remove such lambdas
Attachments
Issue Links
- relates to
-
SOLR-14579 Comment SolrJ 'Utils' generic map functions
- Closed
- links to