• Stars
    star
    1,571
  • Rank 29,799 (Top 0.6 %)
  • Language
    Rust
  • Created almost 2 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Rust implementation of Glidesort, my stable adaptive quicksort/mergesort hybrid sorting algorithm.

Glidesort

Glidesort is a novel stable sorting algorithm that combines the best-case behavior of Timsort-style merge sorts for pre-sorted data with the best-case behavior of pattern-defeating quicksort for data with many duplicates. It is a comparison-based sort supporting arbitrary comparison operators, and while exceptional on data with patterns it is also very fast for random data.

For sorting n elements with k distinct values glidesort has the following characteristics by default:

Best    Average     Worst       Memory      Stable      Deterministic
n       n log k     n log n     n / 8       Yes         Yes

Glidesort can use as much (up to n) or as little extra memory as you want. If given only O(1) memory the average and worst case become O(n (log n)^2), however in practice its performance is great for all but the most skewed data size / auxiliary space ratios. The default is to allocate up to n elements worth of data, unless this exceeds 1 MiB, in which case we scale this down to n / 2 elements worth of data up until 1 GiB after which glidesort uses n / 8 memory.

Benchmark

Performance varies a lot from machine to machine and dataset to dataset, so your mileage will vary. Nevertheless, an example benchmark from a 2021 Apple M1 machine comparing against [T]::sort and [T]::sort_unstable for various input distributions of u64:

Performance graph

Compiled with rustc 1.69.0-nightly (11d96b593) using --release --features unstable and lto = "thin".

Usage

Use cargo add glidesort and replace a.sort() with glidesort::sort(&mut a). A similar process works for sort_by and sort_by_key.

Glidesort exposes two more families of sorting functions. glidesort::sort_with_buffer(&mut a, buf) asks you to pass a &mut [MaybeUninit<T>] buffer which it will then (exclusively) use as auxiliary space to sort the elements. glidesort::sort_in_vec(&mut v) behaves like normal glidesort but will allocate its auxiliary space at the end of the passed Vec<T>. This allows future sorting calls to re-use the same space and reduce allocations. Both these families also support the _by and _by_key interface.

Visualization

This visualization focuses on demonstrating the advanced merging techniques in glidesort:

glidesort_merge_example.mp4

This visualization shows how glidesort is adaptive to both pre-existing runs as well as many duplicates together:

glidesort_adaptiveness_example.mp4

Note that both visualizations have different small sorting thresholds and auxiliary memory parameters to show the techniques in action on a smaller scale.

Technique overview

If you prefer I also have a recorded talk I gave at FOSDEM 2023 that gives a high level overview of glidesort:

Talk recording preview

Glidesort uses a novel main loop based on powersort. Powersort is similar to Timsort, using heuristics to find a good order of stably merging sorted runs. Like powersort it does a linear scan over the input, recognizing any ascending or strictly descending sequences. However, unlike powersort it does not eagerly sort sequences that are considered unordered into small sorted blocks. Instead it processes them as-is, unsorted. This process produces logical runs, which may be sorted or unsorted.

Glidesort repeatedly uses a logical merge operation on these logical runs, as powersort would. In a logical merge unsorted runs are simply concatenated into larger unsorted runs. Sorted runs are also concatenated into double sorted runs. Only when merging a sorted and unsorted run finally the unsorted run is sorted using stable quicksort, and when merging double sorted runs glidesort uses interleaved ping-pong merges.

Using this novel hybrid approach glidesort can take advantage of arbitrary sorted runs in the data as well as process data with many duplicate items faster similar to pattern-defeating quicksort.

Stable merging

Glidesort merges multiple sorted runs at the same time, and interleaves their merging loops for better memory-level and instruction-level parallelism as well as hiding data dependencies. For similar reasons it also interleaves independent left-to-right and right-to-left merging loops as bidirectional merges, which are a generalization of quadsorts parity merges. Merging multiple runs at the same time also lets glidesort use ping-pong merging, avoiding unnecessary memcpy calls by using the implicit copy you get from an out-of-place merge. All merging loops are completely branchless, making it fast for random data as well.

Glidesort further uses binary searches to split up large merge operations into smaller merge operations that it then performs at the same time using instruction-level parallelism. This splitting procedure also allows glidesort to use arbitrarily small amounts of memory, as it can choose to split a merge repeatedly until it fits in our scratch space to process.

Stable quicksort

Yes, stable quicksort. Wikipedia will outright tell you that quicksort is unstable, or at least all efficient implementations are. That simply isn't true, all it needs is auxiliary memory. Credit to Igor van den Hoven's fluxsort for demonstrating that stable quicksort can be efficient in practice.

Glidesort uses a novel bidirectional stable partitioning method that interleaves a left-to-right partition scan with a right-to-left partition scan for greater memory-level parallelism and hiding data dependencies. Partitioning is done entirely branchlessly (if the comparison operator is), giving consistent performance on all data.

License

Glidesort is dual-licensed under the Apache License, Version 2.0 and the MIT license.

More Repositories

1

pdqsort

Pattern-defeating quicksort.
C++
2,334
star
2

slotmap

Slotmap data structure for Rust
Rust
1,104
star
3

ed25519

Portable C implementation of Ed25519, a high-speed high-security public-key signature system.
C
487
star
4

polymur-hash

The PolymurHash universal hash function.
C
316
star
5

dev-on-windows

An opiniated guide to set up a development environment on Windows.
226
star
6

matt-parker-five-letter-clique

Rust
45
star
7

devector

Resizable contiguous sequence container with fast appends on either end.
C++
37
star
8

recursive

Easy recursion in Rust, without stack overflows.
Rust
28
star
9

num-ord

A wrapper type for cross-type numeric comparisons.
Rust
27
star
10

peekread

Rust crate for making Read streams peekable.
Rust
26
star
11

ReducePing

ReducePing is a small utility to tune the "TcpAckFrequency" setting of Windows to get better latency in TCP networked games.
C
20
star
12

aoc2022

My Advent of Code 2022 solutions, in Rust.
Rust
20
star
13

golf-cpu

Reference implementation of the GOLF CPU.
Python
17
star
14

pyglfw

Python bindings for GLFW
C
16
star
15

PyGG2

A Python rewrite of Gang Garrison 2
Python
14
star
16

pyth5

Clean-sheet rewrite of Pyth.
Python
13
star
17

bitwise-binary-search

Accompanying code for https://orlp.net/blog/bitwise-binary-search/.
C++
12
star
18

dotfiles

My dotfiles.
HTML
10
star
19

pygrafix

pygrafix is a Python/Cython hardware-accelerated 2D graphics library.
C
10
star
20

xcharter

XCharter font build.
Python
10
star
21

vim-bunlink

A replacement for :bdelete that decouples the concept of 'deleting a buffer' from 'closing a window'.
Vim Script
9
star
22

libop

My personal C++ library.
Objective-C
8
star
23

aoc2023

My Advent of Code 2023 solutions, in Rust.
Rust
7
star
24

vim-quick-replace

A quick find/replace plugin for Vim.
Vim Script
6
star
25

secudht

A secure design and implementation of the Kademlia DHT.
C
6
star
26

sum-bench

This is the code accompanying https://orlp.net/blog/taming-float-sums/.
Rust
6
star
27

multilive

Multiple poe.trade live searches at once.
JavaScript
5
star
28

iwyu

A small utility that helps you include the right C++ headers.
Python
4
star
29

aoc2021

My Advent of Code 2021 solutions, in Rust.
Rust
4
star
30

commonc

Various common C algorithms and things
C
3
star
31

stable-alloc-shim

A stable Rust shim for the unstable Allocator API.
Rust
3
star
32

synth

A real-time self-hosting MIDI software synth written in Rust from scratch.
Rust
3
star
33

qcon

Quake-style console for windows.
AutoHotkey
3
star
34

ncUI

A lightweight user interface for World of Warcraft - DEAD
Lua
3
star
35

deps

deps is a minimalistic building system for any process which consists of smaller processes that depend on each other.
Python
2
star
36

euler

My solutions for Project Euler.
C++
1
star
37

osrs-fragment-calc

OSRS fragment set calculator
HTML
1
star
38

hades-boons

Python
1
star
39

picture-in-picture

Java
1
star
40

Robotics2020-Final

This repository hosts my final project for the 2020 Robotics course at Leiden university.
JavaScript
1
star
41

boost-win-builds

Some Windows builds for Boost.
Shell
1
star
42

lolpriority

Little tool for Leage of Legends, automatically puts the LoLClient.exe process on low priority when the in-game client is open for extra performance.
C
1
star
43

amazons

Web-based Game of the Amazons
CSS
1
star
44

distris

Distributed socializing.
C++
1
star
45

p

C++
1
star
46

poe-trade-qol

Quality of life for PoE's trade site.
JavaScript
1
star
47

poedps

A Path of Exile weapon DPS calculator with optional crafting.
JavaScript
1
star
48

StrongholdCoords

An application for finding out the exact coordinates of end portal frames in Minecraft seeds.
Java
1
star
49

unite-git-repo

A source for Unite.vim that lists all files from the git repository root.
Vim Script
1
star
50

pyflat

pyflat is a Python hardware-accelerated 2D graphics library.
C
1
star