[hibernate-dev] Re: Regarding lazy initialization of ArrayList

Christian Beikov <[email protected]>
Newsgroups gmane.comp.java.hibernate.devel
Message-ID <[email protected]>
Hey Nathan,

I like the idea in general and but you have to be careful with changing 
this as I assume this was done to save memory by creating the list, only 
when at least one element would be added to the list. I guess if we 
always initialize the list, we'd have to do something like the following 
in the end `this.activeFactoryNames = activeFactoryNames.isEmpty() ? 
Collections.emptyList() : activeFactoryNames` which is probably not that 
bad, assuming we probably already do something similar.

I don't think anyone would object to such an improvement regarding NPE 
safety as long as the final memory consumption stays the same. It would 
be a pity though if we unnecessarily created lots of empty ArrayLists.

Am 18.08.2020 um 22:18 schrieb Nathan Xu:
> This is my first message posted here after I finally succeeded in 
> subscribing to Hibernate dev mail list.
>
> As well known, we utilized the following 'lazy initialization' pattern 
> extensively in our codebase to save memory:
>
> if ( activeFactoryNames == null ) {
>      activeFactoryNames = new ArrayList<>();
> }
> However, even since JDK7, ArrayList has gone through refactoring to 
> keep from allocating
> memory for 10 elements in advance, if the instance was created by 
> default constructor and the allocation of memory
> is only done when some element is really added in future. Namely, 
> ArrayList has the lazy initialization feature built-in.
> E.g., see 
> https://stackoverflow.com/questions/33688753/jdk-api-documentation-is-incorrect-for-arraylist-constructor-is-that-a-bug 
>
> for details. You can also browse the source code of ArrayList to confirm.
> Screen Shot 2020-08-18 at 4.06.58 PM.png
> Needless to say, the lazy initialization pattern has serious NPE issue 
> if it is not coded carefully (which is tedious and error-prone per se).
> As an active Hibernate v6 contributor, I am troubled by such NPE issue 
> again and again.
> Is it a good timing to get rid of the above lazy initialization 
> pattern for now?
> Hopefully my message is good food for thought. No finger pointing. 
> Just curious.
>
> _______________________________________________
> hibernate-dev mailing list -- [email protected]
> To unsubscribe send an email to [email protected]
> %(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s
_______________________________________________
hibernate-dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.