Review for CR 6728865 : Improved heuristics for Collections.disjoint() [updated]

Ulf Zibis Ulf.Zibis at gmx.de
Thu Dec 23 22:02:58 UTC 2010


Am 23.12.2010 22:24, schrieb Ulf Zibis:
> Aren't the explanation comments from my last example clear enough and more fluently readable?

For clarification:

         // collections to iterate and examine containment on
         Collection<?> iterate = c1;
         Collection<?> contains = c2;

         // performance optimization cases
         if (c1 instanceof Set) {
             // As mere Collection's contains() impl predictably performs worse
             // than Set's (< O(N/2)), iterate on c2.
             iterate = c2;
             contains = c1;
         } else if (!(c2 instanceof Set)) {
             // Both are mere collections.
             int c1size = c1.size();
             int c2size = c2.size();
             if (c1size == 0 || c2size == 0) {
                 // At least one collection is empty. Nothing will match.
                 return true;
             } else if (c1size > c2size) {
                 // Iterate over smaller collection.
                 // E.g. if c1 contains 3 elements and c2 contains 50 elements and
                 // assuming contains() requires ceiling O(N/2) comparisons then
                 // checking for all c1 elements in c2 would require 75 comparisons
                 // vs. all c2 elements in c1 would require 100.
                 iterate = c2;
                 contains = c1;
             }
         }

-Ulf




More information about the core-libs-dev mailing list