[ 
https://issues.apache.org/jira/browse/SOLR-14586?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17142718#comment-17142718
 ] 

Noble Paul edited comment on SOLR-14586 at 6/23/20, 8:25 AM:
-------------------------------------------------------------

[~megancarey] putIfAbsent() is particularly bad. The value is computed before 
the method is called. If we have to choose between putifAbsent() & 
computeIfAbsent(), almost always chgoose computeifAbsent. The Function Object 
is cheap to construct, the real Object is usually far more expensive

[~nitsanw] 
*  static vars are always optimized irrespective of where it is used
*  direct lamda uses are inefficient in certain places. 
Now, do you expect a developer to run a perf benchmark every time he has to use 
a {{Map#computeIfAbsent()}} or we just create some best practices and people 
just follow them?
 


was (Author: noble.paul):
[~megancarey] {{putIfAbsent() }} is particularly bad. The value is computed 
before the method is called.

[~nitsanw] 
*  static vars are always optimized irrespective of where it is used
*  direct lamda uses are inefficient in certain places. 
Now, do you expect a developer to run a perf benchmark every time he has to use 
a {{Map#computeIfAbsent()}} or we just create some best practices and people 
just follow them?
 

> replace the second function parameter in Map#computeIfAbsent with static vars
> -----------------------------------------------------------------------------
>
>                 Key: SOLR-14586
>                 URL: https://issues.apache.org/jira/browse/SOLR-14586
>             Project: Solr
>          Issue Type: Bug
>      Security Level: Public(Default Security Level. Issues are Public) 
>            Reporter: Noble Paul
>            Assignee: Noble Paul
>            Priority: Minor
>          Time Spent: 10m
>  Remaining Estimate: 0h
>
> people misunderstand lambdas
>  let's look at a very innocuous looking piece of code
> {code:java}
> mapObject.computeIfAbsent(key, o -> new HashMap<>());
> {code}
> vs.
> {code:java}
> mapObject.computeIfAbsent(key, Utils.NEW_HASHMAP_FUN)
> {code}
>  
>  The first code fragment is executed as following
> {code:java}
> s.computeIfAbsent(key, new Function() {
>  @Override
>  public Object apply(String key) {
>  return new HashMap<>();
>  }
> }
> {code}
> 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



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org
For additional commands, e-mail: issues-h...@lucene.apache.org

Reply via email to