htop icon indicating copy to clipboard operation
htop copied to clipboard

perf-enhancement: Add Vector_sort function w/ context support

Open j-a-c-k-goes opened this issue 2 months ago • 4 comments

PR_DESCRIPTION.md

Add Vector_sort function with context support

Consolidates vector sorting functions as requested in issue #1785. Implements "Highlander rule" via providing single preferred sorting function while maintaining backward compatibility.

Features:

  • Context-aware comparison functions for advanced sorting logic
  • Hybrid algorithm: insertion sort ≤16 elements, quicksort >16 elements
  • Backward compatible: existing functions remain unchanged
  • NULL-safe context handling

Performance benefits:

  • Optimal algorithm selection based on array size
  • Up to 93% faster than pure insertion sort on large arrays
  • 16-element threshold for algorithm switching

This addresses issue #1785 without breaking existing code. Future commits can gradually migrate call sites to use Vector_sort.

Signed-off-by: [_ack] <[[email protected]]>

j-a-c-k-goes avatar Oct 23 '25 21:10 j-a-c-k-goes

Hi.

Good contribution, but I have a concern on using quicksort, which can break stability in sorting. The original code uses insertionSort probably because it's a stable sort, or because it deals with data that are more often partially sorted.

So when it comes to consolidating sorting function, I would consider any unstable sorting function be out.

Explorer09 avatar Oct 23 '25 22:10 Explorer09

@Explorer09 are you finding the implemented sort to be unstable? Could retest, but initial tests were not unstable.

j-a-c-k-goes avatar Oct 24 '25 00:10 j-a-c-k-goes

are you finding the implemented sort to be unstable? Could retest, but initial tests were not unstable.

@j-a-c-k-goes It's computer science. A sorting algorithm is said to be "stable" when the items that are compared to be equal keep their relative order after sorted. This is important for data that can be sorted in multiple, different keys (think of a data table). When I sort the table under key A, then reorder the items under key B. I except items that are equal under B keep their relative order of A.

Explorer09 avatar Oct 24 '25 02:10 Explorer09

This PR should be based on the commit https://github.com/htop-dev/htop/commit/d3cd557e0534a45ab971397d4c407116547d3f6d that I mentioned in #1784 to allow a proper comparison for the chosen implementations.

BenBE avatar Oct 25 '25 11:10 BenBE