Uploaded image for project: 'Commons Lang'
  1. Commons Lang
  2. LANG-1643

Initialize ArrayList in EqualsBuilder constructor with size 1

    XMLWordPrintableJSON

    Details

    • Type: Improvement
    • Status: Open
    • Priority: Minor
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: lang.builder.*
    • Environment:

      Description

      In the constructor the EqualsBuilder the variabele bypassReflectionClasses is initialized with an new ArrayList() and immidiately after that a vaue is added. This will result in an ArrayList.grow() because the contructor of ArrayList will initialize is with a zero length array. See attached screenshots for a stacktrace from a flight recording.

      My suggestion would be to initialize the arraylist with a length of 1 (or the default of 10 that is used in java.util.ArrayList when it is initially grown on the first add.) So the calls to ArrayList.grow and ArrayList.newCapacity and Arrays.copyOf won't be called when calling the add function after construction.

       

      So the code sample of the constructor would be:

          public EqualsBuilder() {
              // set up default classes to bypass reflection for
              bypassReflectionClasses = new ArrayList<>(1);
              bypassReflectionClasses.add(String.class); //hashCode field being lazy but not transient
          }
      


        Attachments

        1. screenshot-1.png
          14 kB
          Alexander van de Rovaart
        2. image-2021-02-04-11-45-47-437.png
          14 kB
          Alexander van de Rovaart

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              avdrovaart Alexander van de Rovaart
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated: