Uploaded image for project: 'Flink'
  1. Flink
  2. FLINK-4860 Sort performance
  3. FLINK-4868

Insertion sort could avoid the swaps

    XMLWordPrintableJSON

Details

    • Sub-task
    • Status: Reopened
    • Not a Priority
    • Resolution: Unresolved
    • None
    • None
    • Runtime / Task

    Description

      This is about the fallback to insertion sort at the beginning of QuickSort.sortInternal. It is quite a hot code as it runs every time when we are at the bottom of the quick sort recursion tree.

      The inner loop does a series of swaps on adjacent elements for moving a block of several elements one slot to the right and inserting the ith element at the hole. However, it would be faster to first copy the ith element to a temp location, and then move the block of elements to the right without swaps, and then copy the original ith element to the hole.

      Moving the block of elements without swaps could be achieved by calling UNSAFE.copyMemory only once for every element (as opposed to the three calls in MemorySegment.swap currently being done).

      (Note that I'm not sure whether UNSAFE.copyMemory is like memmove or like memcpy, so I'm not sure if we can do the entire block of elements with maybe even one UNSAFE.copyMemory.)

      Note that the threshold for switching to the insertion sort could probably be increased after this.

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              ggevay Gábor Gévay
              Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

                Created:
                Updated: