Welcome to the list Nathan!

I would be cautious in removing such lazy initialization patterns: even when the array is not immediately allocated, we still often have reasons to not want to allocate the wrapping object.

While of course each such choice relates to a specific context, so this should not be taken as a golden rule nor enforced globally, there are several examples of large classes in Hibernate ORM which *might* need a significant amount of collections to be initialized, but only to deal with very specific corner cases, sometimes only in combination with obscure configurations.
Combining such considerations about instances which are very rarely used with further considerations such as how wide-spread this library is used in the world, and how "hot" some of these code paths are, then I feel we are responsible to make these as efficient as possible. A NPE is certainly something we need to avoid but it's a concern which we can address with just a little bit of care.

I'll also remind that most of such patterns have been introduced only after a problem was noted during profiling; in case of new code I'm occasionally guilty of writing code in such style on first shot, but I generally do so only when I think it's worth it - as I would agree that code readability and ease of maintenance has a value as well.

Finally, remember one nice hint I got from the OpenJDK engineers: a null-check is most often free in terms of code efficiency.

Thanks


On Tue, 18 Aug 2020 at 21:18, Nathan Xu <nathan.qingyang.xu@gmail.com> wrote:
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 -- hibernate-dev@lists.jboss.org
To unsubscribe send an email to hibernate-dev-leave@lists.jboss.org
%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s