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

Optimize ord implementation and signed zero canonicalization #144

Merged
merged 4 commits into from
Oct 10, 2023

Conversation

orlp
Copy link
Contributor

@orlp orlp commented Oct 10, 2023

These micro-optimizations significantly reduce the number of instructions comparisons take, and often makes them branchless as well. Similarly we use a trick to canonicalize signed zero to positive zero in a single instruction without branches for faster hashing.

@orlp
Copy link
Contributor Author

orlp commented Oct 10, 2023

For example, a <= b went from this:

example::old_leq:
        vucomiss        xmm1, xmm0
        jae     .LBB0_1
        mov     al, 1
        vucomiss        xmm0, xmm1
        jae     .LBB0_5
        mov     al, -1
        vucomiss        xmm0, xmm0
        jp      .LBB0_4
.LBB0_5:
        inc     al
        cmp     al, 2
        setb    al
        ret
.LBB0_1:
        xor     eax, eax
        vucomiss        xmm0, xmm1
        sbb     eax, eax
        inc     al
        cmp     al, 2
        setb    al
        ret
.LBB0_4:
        vucomiss        xmm1, xmm1
        setnp   al
        inc     al
        cmp     al, 2
        setb    al
        ret

to this:

example::new_leq:
        vcmpleps        xmm0, xmm0, xmm1
        vxorps  xmm2, xmm2, xmm2
        vcmpunordps     xmm1, xmm1, xmm2
        vorps   xmm0, xmm1, xmm0
        vmovd   eax, xmm0
        and     al, 1
        ret

Copy link
Collaborator

@mbrubeck mbrubeck left a comment

Choose a reason for hiding this comment

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

Thank you!

@mbrubeck mbrubeck merged commit 4e29b08 into reem:master Oct 10, 2023
2 checks passed
@orlp
Copy link
Contributor Author

orlp commented Oct 10, 2023

@mbrubeck To also give some concrete numbers, on my Apple M1 machine sorting a shuffled Vec of 1 million OrderedFloat<f64>s went from 110ms to 84ms, an 1.3x speedup. I'd expect the difference on x86-64 to be even greater.

maxdeviant referenced this pull request in zed-industries/zed Aug 8, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [ordered-float](https://togithub.com/reem/rust-ordered-float) |
workspace.dependencies | patch | `2.10.0` -> `2.10.1` |

---

### Release Notes

<details>
<summary>reem/rust-ordered-float (ordered-float)</summary>

###
[`v2.10.1`](https://togithub.com/reem/rust-ordered-float/releases/tag/v2.10.1)

[Compare
Source](https://togithub.com/reem/rust-ordered-float/compare/v2.10.0...v2.10.1)

#### What's Changed

- Refactor Hash implementation by
[@&#8203;jogru0](https://togithub.com/jogru0) in
[https://github.com/reem/rust-ordered-float/pull/129](https://togithub.com/reem/rust-ordered-float/pull/129)
- Optimize Ord implementation by
[@&#8203;orlp](https://togithub.com/orlp) in
[https://github.com/reem/rust-ordered-float/pull/144](https://togithub.com/reem/rust-ordered-float/pull/144)

**Full Changelog**:
reem/rust-ordered-float@v2.10.0...v2.10.1

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "after 3pm on Wednesday" in timezone
America/New_York, Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

Release Notes:

- N/A

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzOC4yMC4xIiwidXBkYXRlZEluVmVyIjoiMzguMjAuMSIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOltdfQ==-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
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