-
Notifications
You must be signed in to change notification settings - Fork 141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use insertion sort to sort short ByteString #123
Conversation
Wow. I didn't think anyone used this function! @watashi can I ask you in what sort of use case is this useful and that the performance matters? I'm really curious! In principle I have no objection (assuming tests are passing etc). I'll look at the code properly. Anyone else free to do so too of course. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sort
appears to be very well tested:
bytestring/tests/Properties.hs
Lines 1167 to 1177 in 95fe6bd
prop_sort1BB xs = sort xs == (P.unpack . P.sort . P.pack) xs | |
prop_sort2BB xs = (not (null xs)) ==> (P.head . P.sort . P.pack $ xs) == minimum xs | |
prop_sort3BB xs = (not (null xs)) ==> (P.last . P.sort . P.pack $ xs) == maximum xs | |
prop_sort4BB xs ys = | |
(not (null xs)) ==> | |
(not (null ys)) ==> | |
(P.head . P.sort) (P.append (P.pack xs) (P.pack ys)) == min (minimum xs) (minimum ys) | |
prop_sort5BB xs ys = | |
(not (null xs)) ==> | |
(not (null ys)) ==> | |
(P.last . P.sort) (P.append (P.pack xs) (P.pack ys)) == max (maximum xs) (maximum ys) |
Of course I'm also curious what sort
is used for, but that shouldn't be a reason to hold off merging any longer! :)
withForeignPtr input (\x -> insertionSort p (x `plusPtr` s) l) | ||
where | ||
-- threshold is where we find insertion sort still outperforms counting | ||
-- sort even for worst case. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to clarify, what is the worst case. Could you possibly commit benchmarks, which you used to derive this threshold?
Superseded by #267, closing. |
The constant overhead of counting sort for sorting short ByteString is quite a lot and insertion sort performs much better.
Here is the benchmark results of comparing insertion sort and counting sort when the length is 22. Insertion sort still outperforms counting sort even for worst test case (
"0ZYXWVUTSMLKJIHGFEDCBA"
).