Github user SolidWallOfCode commented on a diff in the pull request:

    https://github.com/apache/trafficserver/pull/286#discussion_r38672458
  
    --- Diff: lib/ts/Vec.h ---
    @@ -964,41 +965,71 @@ qsort_Vec(C *left, C *right, bool (*lt)(C, C))
           }
         }
       } else {
    -    C *i = left + 1, *j = right - 1;
    -    C x = *left;
    -    for (;;) {
    -      while (lt(x, *j))
    -        j--;
    -      while (i < j && lt(*i, x))
    -        i++;
    -      if (i >= j)
    -        break;
    -      C t = *i;
    -      *i = *j;
    -      *j = t;
    -      i++;
    -      j--;
    +    C *center = left + ((right - left) / 2);
    +    C median;
    +
    +    // find the median
    +    if (lt(*center, *left)){  // order left and center
    +      swap(center, left);
    +    }
    +    if (lt(*(right-1), *left)){       // order left and right
    +      swap(right-1, left);
    +    }
    +    if (lt(*(right-1), *center)){  // order right and center
    +      swap((right-1), center);
    +    }
    +    swap(center, right-2);      // stash the median one from the right for 
now
    +    median = *(right-2);        // and that's the median of the 3
    +
    +    // now partition, pivoting on the median value
    +    // l ptr is +1 b/c we already put the lowest in there, ignore it for 
now
    +    // r ptr is -2 b/c we already put the biggest and the median there
    +    C *l = left + 1, *r = right-2;
    +
    +    // move l and r until they have something to do
    +    while (lt(median, *(r-1))) {
    --- End diff --
    
    Note the original code doesn't check the bounds because we know the `lt` 
check will stop before `l < r` in any case (since !(median < median).


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to