noporpoise / sort_r Goto Github PK
View Code? Open in Web Editor NEWPortable qsort_r / qsort_s
Portable qsort_r / qsort_s
Hi,
I'm using this code in https://github.com/behdad/harfbuzz/, and our fuzzers have found a timeout for us, which I tracked down to this code performing slow. Upon inspection, it's clear that if all array elements are the same, the code performs at its worst case. The median-pivot method helps with the case of array being mostly sorted or reverse-sorted, but doesn't help all-the-same.
Given that elements equal to the pivot can be partitioned on either side, I'm going to modify the code to use that property to choose a more central breaking point. I'll report on my progress. Thanks.
In this context, _SORT_R_BSD means qsort_r from FreeBSD,
so it must be defined only for FreeBSD, its forks (DragonFly) and having userland from it (MacOS X).
So I'm using this repo and it's great! But I tried compiling my project on alpine linux recently, which uses musl, which apparently doesn't provide an implementation of qsort_r
. They also don't define any macros AFAIK that let you detect musl.
I'll see if I can think of a nice way to fix it, but it seems like it might just require a flag. I would just #undef
some macros to hack it in but none of these look terribly safe to #undef
:
#if (defined __APPLE__ || defined __MACH__ || defined __DARWIN__ || \
defined __FreeBSD__ || defined __DragonFly__)
# define _SORT_R_BSD
#elif (defined _GNU_SOURCE || defined __gnu_hurd__ || defined __GNU__ || \
defined __linux__ || defined __MINGW32__ || defined __GLIBC__)
# define _SORT_R_LINUX
#elif (defined _WIN32 || defined _WIN64 || defined __WINDOWS__)
# define _SORT_R_WINDOWS
# undef _SORT_R_INLINE
# define _SORT_R_INLINE __inline
#else
/* Using our own recursive quicksort sort_r_simple() */
#endif
OS: Windows 10
Compiler: TDM-GCC-64
Compilation fails with the below error -
sort_r.h:288: undefined reference to `qsort_r'
I was able to fix it by removing the check for __MINGW32__
in the _SORT_R_LINUX
definition
Move sort_r_arg_swap and sort_r_data definition to #else block created on line 58 solves the problem.
Hi again, at the 83 line:
qsort_s(*base, nel, width, &sort_r_arg_swap, &tmp);
perhaps it supposed to be:
qsort_s(base, nel, width, &sort_r_arg_swap, &tmp);
I have not tested it in this configuration yet. I hope I'm right.
...and I thanks - your code is better then mine I used to use before…
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.