primitive-sort: Sort primitive arrays

[ bsd3, library, software ] [ Propose Tags ]

This library provides a stable sorting algorithm for primitive arrays. The algorithm currently uses mergesort on large chunks and switches to insertion sort on small chunks. There are also novel improvements to increase the performance if the input array is already mostly sorted.

[Skip to Readme]


[Index] [Quick Jump]


Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees


Versions [RSS],,,,,,
Change log
Dependencies base (>=0.4.16 && <5), contiguous (>=0.6 && <0.7), primitive (>=0.6.4 && <0.10) [details]
License BSD-3-Clause
Copyright 2018 Andrew Martin
Author Andrew Martin
Category software
Home page
Bug tracker
Source repo head: git clone git://
Uploaded by l3c_amartin at 2024-02-07T17:02:59Z
Reverse Dependencies 1 direct, 3 indirect [details]
Downloads 1261 total (18 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2024-02-07 [all 1 reports]

Readme for primitive-sort-

[back to package description]


Sorting of contiguous data structures. Implementation uses mergesort and switches to insertion sort once the arrays are small. Some of the benchmark results on a Intel Xeon CPU E3-1505M v5 2.80GHz (GHC 8.10.4):

contiguous/Int8/unsorted/mini      time  91.59 ns  
contiguous/Int8/unsorted/tiny      time  1.236 μs  
contiguous/Int8/unsorted/small     time  40.68 μs  
contiguous/Int8/unsorted/medium    time  614.1 μs  
contiguous/Int8/unsorted/large     time  6.580 ms  
contiguous/Int8/unsorted/gigantic  time  68.16 ms  

Results may vary across processors, but you observe results that are 10x slower, it's probably an issue with the compiler, and please open an issue.

GHC Specialization Problems

Sadly, the most permissive signature for sort causes GHC's specialization to break. This permissive signature is:

sort :: (Contiguous arr, Element arr a, Ord a) => arr a -> arr a

And the less permissive variant used by this library is:

sort :: (Prim a, Ord a) => PrimArray a -> PrimArray a

The latter works nicely with SPECIALIZE and INLINEABLE, but the former does not work at all with these. The issue is believed to be related to the use of an associated constraint type Element, which introduced coercions that might confuse the specializer. However, this has not been confirmed.