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

Enforce target feature #61

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open

Enforce target feature #61

wants to merge 21 commits into from

Conversation

ogxd
Copy link
Owner

@ogxd ogxd commented Jan 20, 2024

Problem(s)

This PR addresses as few points:

  • error[E0432]: unresolved import platform  #57
  • Performance issue due to code not being compiled for specific features, not harnessing the hardware capabilities, despite target-cpu=native being set
  • Performance issue due to target-cpu=native not being set at all

Possible Solutions

  • Tell users to explicitely define their features when building with RUSTFLAGS target-feature=+aes,+sse2...
    • ❌ This is a poor solution in terms of DX
    • ❌ It constrains projects that depend on gxhash to adapt their CI/CD pipelines
  • Build-time feature check
    • ❌ Does not work well with cross compilation
  • Enforcing feature with #[target_feature(enable = "feature")]

Chosen solution

This PR change behavior to enforce required target features to fully leverage hardware capabilities. In order not to enforce X86 avx2 / vaes however we rely on an additional build-time check. This would not work properly in a cross-compiling situation, but I think it is acceptable for now, especially given that avx2 gxhash only runs on unstable rust anyway.

EDIT 1: Inlining?

For some reason, it seems that #[target_feature(enable = "feature")] prevents inlining. This is really unfortunate. It also seems that the attribute is "skipped" if the target feature is already enabled. For instance, if passed via RUSTFLAGS, of it method is called within a method that itself has the attribute. When the attribute is skipped, we can have inlining.
While it is rather annoying, we could deal with this limitation by using the attribute on the gxhash method itself (higher caller), However it's more complex for the Hasher part, as inlining on individual write_x methods makes a big difference.

@ogxd ogxd self-assigned this Jan 20, 2024
@ogxd
Copy link
Owner Author

ogxd commented Jan 21, 2024

This turns out to be much more challenging than expected: the target_feature disables inlining, which defeats the purpose of an fast hashing algorithm. This is done by design for "safety reasons" I assume. This is unfortunate because with runtime checks the context could have been made safe not to disable inlining.
Until this is a little less fuzzy, let's go for a more developer friendly approach, which is to detect the missing target feature within build.rs to inform the developer that he must enable some target-feature(s). #62

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.

None yet

1 participant