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

Add #![no_std] compatibility #26

Merged
merged 15 commits into from
Apr 20, 2024
Merged

Conversation

poshcoe
Copy link
Contributor

@poshcoe poshcoe commented Apr 19, 2024

Adds #![no_std] (with alloc) compatibility by introducing a "std" cargo feature.

I'm interested in evaluating bitcode for embedded applications. I found adding support relatively painless, most of the changes are swapping use std::* for use::core::*, and adding explicit use of alloc.

Notable changes:

  • HashMap and HashSet implementations for Decode and Encode are disabled for no_std
  • Added an unrelated improvement to the thread_local here, utilising the const syntax which should improve efficiency by avoiding runtime initialisation checks.
  • As thread_local! is std-only, I added a separate impl_u8! which falls-back to allocating the "scratch space" on each pack8/unpack8 call. See below images for benchmarks on the impact of this change (target_arch = "x86_64").

cargo +nightly bench bench_pack:

Screenshot_20240419_104158

cargo +nightly bench bench_unpack_u:

Screenshot_20240419_124131

At a glance the performance impact is minimal, resulting in a < 1% average performance deficit on both pack and unpack without std.

It remains to be seen if bitcode performs well on no_std target architectures. I'll likely be testing this in the near future.

@finnbear finnbear requested a review from caibear April 19, 2024 06:25
@finnbear
Copy link
Member

finnbear commented Apr 19, 2024

Thanks for the PR! LGTM. In the interest of not inadvertently breaking #![no_std] in the future, is it possible to add a GitHub Actions job that runs a subset of the tests?

@poshcoe
Copy link
Contributor Author

poshcoe commented Apr 19, 2024

In the interest of not inadvertently breaking #![no_std] in the future, is it possible to add a GitHub Actions job that runs a subset of the tests?

Sure, I'll tack it on along with fixes for the lints

@poshcoe
Copy link
Contributor Author

poshcoe commented Apr 19, 2024

I also took the chance to enable compat of the "serde", "glam" and "arrayvec" features with #![no_std].

The new test therefore enables --features derive,serde,glam,arrayvec

Cargo.toml Outdated Show resolved Hide resolved
Copy link
Member

@finnbear finnbear left a comment

Choose a reason for hiding this comment

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

Thanks very much! 🚀

bitcode_derive/src/lib.rs Outdated Show resolved Hide resolved
src/derive/map.rs Show resolved Hide resolved
src/derive/vec.rs Show resolved Hide resolved
src/pack_ints.rs Outdated Show resolved Hide resolved
@caibear
Copy link
Member

caibear commented Apr 20, 2024

Build is failing because cargo fmt's output changed between nightly-2023-04-25 and your rust version.

@caibear
Copy link
Member

caibear commented Apr 20, 2024

Ok, I've fixed the issues I noticed.

@poshcoe
Copy link
Contributor Author

poshcoe commented Apr 20, 2024

thanks!

@caibear caibear merged commit 67d5183 into SoftbearStudios:main Apr 20, 2024
1 check 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.

3 participants