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

Add specialization of SequencedCollection methods to emptyList, singletonList and nCopies

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Unresolved
    • Icon: P4 P4
    • None
    • None
    • core-libs
    • None

      I noticed that the implementations of the lists returned by java.util.Collections emptyList, singletonList, and nCopies methods lack reversed() implementation, and instead rely on the default implementation, which creates a ReverseOrderListView wrapper. However, for these three lists, the reversed view is completely equivalent to the original list, so returning `this` from reversed() would be more optimal.

      Another thing that could be specialized is getFirst() and getLast() implementations of singletonList. They can simply return an element without extra checks. While extra checks could be eliminated by advanced JIT compiler with sufficient inlining, this specialization still might be helpful if inlining fails (e.g., due to polymorphic call site).

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

              Created:
              Updated: