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

Performance optimization of Arrays.asList().iterator()

XMLWordPrintable

    • b117
    • Verified

      Currently Arrays.asList() (Arrays.ArrayList) class inherits the iterator() method from AbstractList which is not specialized enough, performs unnecessary bookkeeping like modCount tracking and holds a reference to the Arrays.ArrayList object which currently makes Arrays.ArrayList object heap allocation unavoidable even if it's short-lived and used for single iteration. For example, consider the following code:

          public int sumArray() {
              return sum(Arrays.asList(arr));
          }

          public static int sum(Iterable<Integer> data) {
              int sum = 0;
              for (int item : data) sum+=item;
              return sum;
          }

      Here allocation of Arrays.ArrayList object is not optimized by JIT, so every call creates a heap object.

      Optimized implementation of Arrays.iterator() is possible which returns custom iterator object. It should work faster and don't allocate anything in the test above.

            tvaleev Tagir Valeev
            tvaleev Tagir Valeev
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: