[ 
https://issues.apache.org/jira/browse/LANG-1643?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Alexander van de Rovaart updated LANG-1643:
-------------------------------------------
    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:
{code:java}
    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
    }
{code}

!image-2021-02-04-11-45-47-437.png!
 !screenshot-1.png! 

  was:
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}} become obsolete

 

So the code sample of the constructor would be:
{code:java}
    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
    }
{code}

!image-2021-02-04-11-45-47-437.png!
 !screenshot-1.png! 


> Initialize Arraylist in EqualsBuilder with size 1
> -------------------------------------------------
>
>                 Key: LANG-1643
>                 URL: https://issues.apache.org/jira/browse/LANG-1643
>             Project: Commons Lang
>          Issue Type: Improvement
>          Components: lang.builder.*
>         Environment: {code}
> :~$ java -version
> openjdk version "11.0.9.1" 2020-11-04
> OpenJDK Runtime Environment (build 11.0.9.1+1-Ubuntu-0ubuntu1.20.04)
> OpenJDK 64-Bit Server VM (build 11.0.9.1+1-Ubuntu-0ubuntu1.20.04, mixed mode, 
> sharing)
> :~$ uname -a
> Linux ubuntu 5.8.0-41-generic #46~20.04.1-Ubuntu SMP Mon Jan 18 17:52:23 UTC 
> 2021 x86_64 x86_64 x86_64 GNU/Linux
> Apache commons lang3 version 3.11
> {code}
>            Reporter: Alexander van de Rovaart
>            Priority: Minor
>              Labels: EqualsBuilder, improvement
>         Attachments: image-2021-02-04-11-45-47-437.png, screenshot-1.png
>
>
> 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:
> {code:java}
>     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
>     }
> {code}
> !image-2021-02-04-11-45-47-437.png!
>  !screenshot-1.png! 



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

Reply via email to