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

Refactor clmul to use Union #73

Merged
merged 8 commits into from
Oct 5, 2023

Conversation

000wan
Copy link
Contributor

@000wan 000wan commented Sep 28, 2023

Summary

It resolves #44.
Struct Clmul is modified to:

pub struct Clmul {
    inner: Inner,
    token: mul_intrinsics::InitToken,
}

with Union type Inner:

union Inner {
    intrinsics: intrinsics::Clmul,
    soft: soft::Clmul,
}

It follows the implementation of polyval using InitToken.

Results

#73 (comment)

References

https://doc.rust-lang.org/reference/items/unions.html
https://github.com/RustCrypto/universal-hashes/blob/master/polyval/src/backend/autodetect.rs
https://github.com/RustCrypto/utils/blob/master/cpufeatures/src/lib.rs

@sinui0 sinui0 requested review from sinui0 and themighty1 September 29, 2023 14:26
@sinui0
Copy link
Collaborator

sinui0 commented Sep 29, 2023

Thanks for the PR!

Would it be possible to remove the match blocks all-together? The cpufeatures macro generates a ZST InitToken which can be used to replace is_intr in Clmul.

https://github.com/RustCrypto/universal-hashes/blob/fc463be29521177527377f02ada141c1ec329ae9/polyval/src/backend/autodetect.rs#L25-L56

This essentially moves the is_intr to a global atomic instead of storing a bool in every instance of Clmul. We can also avoid checking both instances in this case.

@000wan
Copy link
Contributor Author

000wan commented Sep 29, 2023

Oh, I see. Do you mean declaring is_intr for global state using Atomic module?

clmul/src/backend.rs Outdated Show resolved Hide resolved
@000wan
Copy link
Contributor Author

000wan commented Sep 29, 2023

[Updated after 570d042]

So I've benched these both for intrinsics and soft, and here are the results:
(Compared to the dev branch, running $ cargo bench --bench clmul)

intrinsics:

clmul                   time:   [2.0842 ns 2.0942 ns 2.1036 ns]                   
                        change: [-80.054% -79.865% -79.674%] (p = 0.00 < 0.05)
                        Performance has improved.

clmul_reuse             time:   [6.7801 ns 6.8201 ns 6.8617 ns]                         
                        change: [-18.986% -14.868% -11.035%] (p = 0.00 < 0.05)
                        Performance has improved.

reduce                  time:   [668.48 ps 686.49 ps 705.57 ps]                    
                        change: [-94.706% -94.544% -94.380%] (p = 0.00 < 0.05)

soft:

clmul                   time:   [34.806 ns 35.014 ns 35.233 ns]                   
                        change: [-10.990% -9.9367% -8.8572%] (p = 0.00 < 0.05)
                        Performance has improved.

clmul_reuse             time:   [34.388 ns 34.735 ns 35.132 ns]                         
                        change: [-18.041% -16.826% -15.262%] (p = 0.00 < 0.05)
                        Performance has improved.

reduce                  time:   [1.7560 ns 1.7783 ns 1.8069 ns]                    
                        change: [-83.553% -83.150% -82.735%] (p = 0.00 < 0.05)
                        Performance has improved.

It seems clmul using intrinsics has been considerably improved, and using soft has a little.

@sinui0
Copy link
Collaborator

sinui0 commented Sep 29, 2023

Nice results!

I think we can avoid depending on lazy_static, we already use cpufeatures.

You can do:

pub struct Clmul {
  inner: Inner,
  token: mul_instrinsics::InitToken
}

impl Clmul {
  pub fn clmul(self, other: Self) -> (Self, Self) {
    if self.token.get() {
      // self.inner is intrinsics
    } else {
      // self.inner is soft
    }
  }
}

InitToken is a ZST so this should be effectively the same, but 1 less dep

clmul/src/backend.rs Outdated Show resolved Hide resolved
Copy link
Collaborator

@sinui0 sinui0 left a comment

Choose a reason for hiding this comment

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

Good work!

If you have a moment maybe you could check if inlining helps perf too

Thanks!

clmul/src/backend.rs Show resolved Hide resolved
Copy link
Collaborator

@themighty1 themighty1 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, looks good!!!
Do you plan to add anything else, or can I merge this @000wan ?

@000wan
Copy link
Contributor Author

000wan commented Oct 5, 2023

I think we can merge this.
Thank you for kind reviews !! :)

@themighty1 themighty1 merged commit 142d4c4 into privacy-scaling-explorations:dev Oct 5, 2023
2 checks passed
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.

Refactor out dispatch from clmul
3 participants