-
-
Notifications
You must be signed in to change notification settings - Fork 550
Use libc qsort() for Vector sorting #1784
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
Conversation
|
+1 for using qsort. |
The answer is much simpler and mundane than you can imagine: I was still learning C. :-) In fact, besides "scratching my itch for a better top", practicing C was the other motivation that 20+-years-younger me had for starting this project. That also explains other oddities from the early codebase, such as trying to avoid maintaining .h files by hand (which I found nonsensical), the Java-esque OOP collections, and my favorite, which I think I never told to anyone before: the utility module with functions for setting up drawing to the screen, "CRT", is named so as a nod to the one in Turbo Pascal. :-D Most importantly, I think all of those rookie oddities show how you don't have to be an experienced programmer to make a significant contribution to FOSS! |
|
@hishamhm Well. It's not a bad thing for contributing to OSS for trying to learn C. The question that interests me is: You seems to be capable of using It seems there are no objections for ditching this custom |
|
@Explorer09 I don't recall the details; maybe I just wasn't aware of the |
|
@hishamhm In 32-bit systems Also note that
|
|
Another sign of the times, now that talking about it reminds me of the old days, was that when I started htop, pid numbers were much smaller. Back in the day, it was IIRC a signed 16-bit value. That means I originally didn't expect any Vectors to be larger than 32768, so overflow using 32-bit indices was not a practical concern! Fun trip down memory lane. Of course, later Vectors were used for more things, and pids (and systems!) got larger. But back when I first implemented that Vector it was unthinkable to me that its 32-bit indices could overflow: I don't think my machine had enough RAM for that. :) |
3fdb5c8 to
4bb087b
Compare
|
|
||
| //static int comparisons = 0; | ||
|
|
||
| /* |
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.
Don't keep commented-out code around. That's what a VCS is for …
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.
Then I would have to delete the commented-out combSort code as well...
Would @hishamhm comment on this?
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.
I'm happy to provide historical commentary, but I defer to the current maintainers on all things code review!
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.
The combSort vs. insertionSort place is a different part of the code.
But as sorting has currently two implementations, this is some other cleanup task (#1785) anyway …
| assert(Vector_isConsistent(this)); | ||
| quickSort(this->array, 0, this->items - 1, compare); | ||
| vectorCompareFn = compare; | ||
| qsort(this->array, this->items, sizeof(Object*), Vector_compareObjects); |
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.
If it weren't for qsort_r only being standardized in POSIX.2024, I'd opt for using it; avoiding the need for global variables. Speaking of which: the wrapper/adapter should check the vectorCompareFn for NULL (marked unlikely to the optimizer).
FWIW: If we wanna be fancy, we could detect a compatible qsort_r implementation in configure.ac and prefer that if available; falling back to qsort only if stars don't align.
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.
I wish to keep the changes small and leave the qsort_r support to a separate commit.
POSIX standardised the GNU version of qsort_r but before GNU, FreeBSD had a version of qsort_r that was ABI-incompatible. Although checking for qsort_r is possible in configure, it's not trivial code. Given that we still need to support platforms without qsort_r, I consider this qsort_r thing a lower priority.
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.
Same here. Exactly because qsort_r support is such a mess I'd suggested to skip it for now and revisit it later..
Which leaves the one NULL check I mentioned above as an item TBD …
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.
@BenBE Yes. I plan to add an assert(!vectorCompareFn) before assignment in order to catch whether Vector_*Sort is called in a signal handler or multithreaded code by mistake.
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.
That's not what I was referring to. And also won't actually help except as a canary.
|
I pushed the relevant (short-term) fix from this PR in ee1d1a8. The question whether we want to update the actual sorting implementation and replace it by something more sprakling should be discussed while known bugs in existing code are fixed. I.e. focus on fixing the original issue, improve the algorithm in a second step. @Explorer09 Please try to base your algorithm replacement work onto d3cd557. That way we can later compare the different sort implementations. |
|
I closed this in favor of #1798. |
I don't know if I need to bother fix this one. Since we could replace the whole "quickSort" algorithm with srandard C
qsort(). ThisquickSortfunction can be trace to this very old commit: 7ca1081. I have no idea why @hishamhm chose a custom "quickSort" algorithm rather than a standard Cqsort()function.