Uploaded image for project: 'Solr'
  1. Solr
  2. SOLR-14586

replace the second function parameter in Map#computeIfAbsent with static vars

    XMLWordPrintableJSON

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

          Activity

            People

              Unassigned Unassigned
              noble.paul Noble Paul
              Votes:
              0 Vote for this issue
              Watchers:
              7 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 20m
                  20m