-
-
Notifications
You must be signed in to change notification settings - Fork 839
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
avm1: Replace recursion with iterative qsort
#18626
Conversation
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.
Thank you! Sorry for a delay, I added some review comments. I think this looks fine, though I'm kinda surprised at the 20% speedup.
stack.push((0, elements.len() - 1)); | ||
|
||
while let Some((low, high)) = stack.pop() { | ||
if low >= high { |
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.
>
isn't normally possible, right? Can we assert
that?
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.
>
isn't normally possible, right?
Yes, you're right! Should I use assert!
or debug_assert!
for 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.
assert
does sound nice, unless it kills the performance back.
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 think we can't replace the if
condition with assert
because low >= high
can occur when the pivot partition results in a subarray with no elements. (e.g., [5, 5, 5, 5, 5])
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 know low == high
can occur, but I meant that specifically low > high
shouldn't ever occur.
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 know
low == high
can occur, but I meant that specificallylow > high
shouldn't ever occur.
Oh, I misunderstood then. I tried to assert!
low >= high, not low > high. Then that explains why the tests failed.
I think it looks good, once you update the PR I can probably quickly test and approve it tomorrow c: |
bfe95f3
to
2075784
Compare
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 don't think that assert
is strictly critical, so I think this can go in as-is (let's just squash on merge).
Thank you! |
Fixes #18109
Speeds up sorting
In this example:
Recursion qsort duration: 347ms
Iterative qsort duration: 271ms