Skip to content
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

wazevo(ssa): avoids allocation during natural order sort #2024

Merged
merged 4 commits into from
Feb 8, 2024
Merged

Conversation

mathetake
Copy link
Member

This starts using slices.SortFunc (thanks to @achille-roussel) to avoid
allocations during ssa.passSortSuccessors. As a result for zig stdlib compilation,
huge amount of allocations has been eliminated.

                                        │    old.txt    │             new.txt              │
                                        │     B/op      │     B/op       vs base           │
Zig/optimizing/Compile/test-opt.wasm-10   513.2Mi ± ∞ ¹   506.6Mi ± ∞ ¹  ~ (p=1.000 n=1) ²
¹ need >= 6 samples for confidence interval at level 0.95
² need >= 4 samples to detect a difference at alpha level 0.05

                                        │    old.txt    │             new.txt             │
                                        │   allocs/op   │  allocs/op    vs base           │
Zig/optimizing/Compile/test-opt.wasm-10   1201.3k ± ∞ ¹   781.8k ± ∞ ¹  ~ (p=1.000 n=1) ²
¹ need >= 6 samples for confidence interval at level 0.95
² need >= 4 samples to detect a difference at alpha level 0.05

Signed-off-by: Takeshi Yoneda <[email protected]>
@mathetake mathetake marked this pull request as ready for review February 8, 2024 01:07
@mathetake mathetake requested a review from evacchi as a code owner February 8, 2024 01:08
Signed-off-by: Takeshi Yoneda <[email protected]>
Signed-off-by: Takeshi Yoneda <[email protected]>
@mathetake mathetake merged commit 8cf0fc3 into main Feb 8, 2024
62 checks passed
@mathetake mathetake deleted the sortgood branch February 8, 2024 01:21
Comment on lines +11 to +16
if j.ReturnBlock() {
return 1
}
if i.ReturnBlock() {
return -1
}
Copy link
Collaborator

@achille-roussel achille-roussel Feb 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the function return zero when i.ReturnBlock() && j.ReturnBlock() ?

I honestly don't know if it matters, but it could confuse the sorting algorithm if f(i, j) => 1 but also f(j, i) => 1.

The comparison function has three states bascially for less than, equal, and greater than, which is slightly different from the "less" function that was historically used in the sort package.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah it should return zero but it doesn't make any difference in the result at the end of the day. Would you mind raising a fix if you like? 🏃

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These things make me nervous, I've seen quick-sort turn into O(N^2) because the comparison function wasn't producing a coherent output.

Sending a PR 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants