-
-
Notifications
You must be signed in to change notification settings - Fork 700
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
Issue 6192 - std.algorithm.sort performance #3922
Conversation
My measurements on random arrays, almost sorted arrays, and sorted arrays all indicate good improvements. |
Took me a while to grok, but this is awesome. LGTM. |
Some measurements in https://issues.dlang.org/show_bug.cgi?id=6192 |
Here is some benchmarking code comparing 2.069 and master with this pull using
built with DMD 2.069
DMD with commit 31d75df
Updated: DMD with commit bc1a23b
|
Since this hasn't been pulled yet, I may as well share: It's possible to find the median of five in six comparisons. Better yet, somebody hardcoded all possible cases in this super elegant function. I also wrote my own function a while back which also partitions the elements by the median, still in six comparisons. |
@Xinok cool, let me adapt your function tomorrow for some testing. Partitioning (not sorting) by the median should be fine. |
I guess |
|
9b2dd54
to
bc1a23b
Compare
@andralex your recent change made the random array sort slightly slower, see my updated comment. |
Regarding "slightly slower", I'd always do such tests using GDC or LDC. The results are pretty much worthless otherwise, unless you are absolutely sure that the cause is an unavoidable algorithmic regression. |
@JackStouffer what is the pessimization? (Can I see the history of the comment?) @klickverbot well arguably improving performance with dmd is both good in and of itself, and an indicator of possible improvements on others. Also improvements in the algos used are likely to improve across the lot. |
@andralex I updated my comment again. Sorry, I shouldn't have overwritten my previous results.
|
I'm trying to get gdc to work, how do I override its paths? It always picks up its own phobos. |
Same question for ldc2... I tried -I/path/to/phobos, no avail. |
My tests also show a slight performance decrease in all scenarios when using Xinok's partition instead of Demuth's, so I'm getting back to Demuth. |
My speculation: the additional structuring brought about by Demuth's algo justifies the extra cost. |
You should also test on different sized arrays, if you are not already. :) |
There is -nophobos switch. However I'd just compile the sort function on it's own with the test. |
ping? guess we should move this forward |
What @ibuclaw said. Yes, I'd like to see some "real" measurements with gdc/ldc instead of dmd. Over the years I'd tended not to trust in dmd performance measurements, because generally gdc/ldc do it about 20-30% faster (sometimes more). |
@quickfur this is not a microoptimization, it mainly removes a provable pathological case. |
This PR changes the
|
@9il cool, could you please create a paste at dpaste.dzfl.pl with a complete benchmark and email it to me so I can look over it? |
Emailed. I hope that email is correct. |
@9il got it thx! |
Optimized sort, 4%-8% speed improvements
This feature will hopefully be deprecated soon.
[TRIVIAL] Remove last use of implicit string concatenation
add mapSlice and fix Issue 16501
[trivial] Added const to varibles in std.file that aren't modified
Add medianOf function
Fix Issue 16544 - Add File.reopen
reduce ndslice template bloat
…verloads in order to facilitate further improvements
workaround for Issue 16473
[Issue 16170] Partial Fix for Broken std.algorithm.sorting.partition
…irst five elements
So, I rebased and got this million commits in there.... will clean this crap up. Anyhow, now we can approach this PR differently because medianOf is available. I've also introduced a simple watermark-based regression buster. |
Restarted in #4826 |
std.sort has had a performance issue. The quicksort algorithm uses the median of three: first, middle, and last element.
Say an array is already sorted except for the last element, which is small (e.g. the same as the first element). In that case, the median of three will choose the first element as the pivot, i.e. an extremum, leading to a wasted pass. This is a common situation (e.g. append to an array then resort).
The proposed solution is to use median of five with a bit of randomness. Five elements are sorted in place using Demuth's algorithm (1956!) which is very advantageous because it only uses up to seven comparisons.