Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8357938

vectorize type tests when performing type-narrowing array copy

XMLWordPrintable

      When `System::arraycopy` is presented with arrays of different types, it must perform extra type checks. If the destination type turns out to be narrower than the source type, we execute the copy in scalar mode, so we can perform a type check (akin to `checkcast`) on each element as it is transferred.

      It seems to me that we can do better in the common case where the copy does NOT throw, and also where the references being copied are all one class, or null. (This is guaranteed when the target type is a final class like String, and it is also true when the source array is homogeneous.)

      How do we do better? Vectorize the copy, and vectorize the type checking as well.

      1. Start with a scalar loop, noting runs of the same class.
      2. If N elements have the same class K (N is tunable), go to the vector loop.
      3. Vector loop: Load M elements (maybe M=N, maybe not) and do a "gather" operation to load all of their classes.
      4. Speculatively check that all classes are equals to K.
      5. If speculation fails, go back to scalar loop.
      6. Store the vector full of references to the destination. (We know this is correct because the scalar loop witnessed successful storage of K.)

      Nulls need to be handled specially; for speculative type testing a null needs to "look like a K".

      If the scalar loop sees N nulls in a row, it might go to a speculative loop which is equipped to store only nulls. This handles the case of copying long sequences of nulls from one array to another, even if there is type narrowing (e.g., the destination is String when the source is Object).

            Unassigned Unassigned
            jrose John Rose
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated: