Why is Arrays.fill() not used in HashMap.clear() anymore?

后端 未结 5 546
太阳男子
太阳男子 2021-01-30 16:02

I noticed something strange in the implementation of HashMap.clear(). This is how it looked in OpenJDK 7u40:

public void clear() {
    modCount++;
          


        
5条回答
  •  無奈伤痛
    2021-01-30 16:16

    I'm going to shoot in the dark here...

    My guess is that it might have been changed in order to prepare the ground for Specialization (aka generics over primitive types). Maybe (and I insist on maybe), this change is meant to make transition to Java 10 easier, in the event of specialization being part of the JDK.

    If you look at the State of the Specialization document, Language restrictions section, it says the following:

    Because any type variables can take on value as well as reference types, the type checking rules involving such type variables (henceforth, "avars"). For example, for an avar T:

    • Cannot convert null to a variable whose type is T
    • Cannot compare T to null
    • Cannot convert T to Object
    • Cannot convert T[] to Object[]
    • ...

    (Emphasis is mine).

    And ahead in the Specializer transformations section, it says:

    When specializing an any-generic class, the specializer is going to perform a number of transformations, most localized, but some requiring a global view of a class or method, including:

    • ...
    • Type variable substitution and name mangling is performed on the signatures of all methods
    • ...

    Later on, near the end of the document, in the Further investigation section, it says:

    While our experiments have proven that specialization in this manner is practical, much more investigation is needed. Specifically, we need to perform a number of targeted experiments aimed at any-fying core JDK libraries, specifically Collections and Streams.


    Now, regarding the change...

    If the Arrays.fill(Object[] array, Object value) method is going to be specialized, then its signature should change to Arrays.fill(T[] array, T value). However this case is specifically listed in the (already mentioned) Language restrictions section (it would violate the emphasized items). So maybe someone decided that it would be better to not use it from the HashMap.clear() method, especially if value is null.

提交回复
热议问题