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

feat: Add a feature that allows usage on no_std targets #242

Merged
merged 4 commits into from
Mar 18, 2023

Conversation

notgull
Copy link
Contributor

@notgull notgull commented Mar 4, 2023

This PR adds features std (enabled by default) and libm that allow this crate to be used on no_std targets.

Copy link
Member

@cmyr cmyr left a comment

Choose a reason for hiding this comment

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

Okay, so: I definitely think we would like to work in no-std environments; this is something we've talked about, but never been motivated to actually approach.

That said, I'm not sure if we want to assume the solution is always musl? I really don't know what our options are, though, so I'd be curious if @dfrg or @raphlinus have any opinions.

I've reviewed this as-is; my main issue currently is that the trait is sort of half-baked, since we define it in both cases but it's only ever used if std is missing; if this is the approach we take I think we should simplify this trait to only exist if std is not active.

println!("</svg>");
println!("</body>");
println!("</html>");
#[cfg(feature = "std")]
Copy link
Member

Choose a reason for hiding this comment

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

Can we do something like just adding #![cfg(feature = "std")] to the top of the file? (Where possible, I prefer solutions that minimize diff size)

println!("</svg>");
println!("</body>");
println!("</html>");
#[cfg(feature = "std")]
Copy link
Member

Choose a reason for hiding this comment

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

ditto

.github/workflows/ci.yml Show resolved Hide resolved
src/bezpath.rs Show resolved Hide resolved
src/common.rs Outdated

impl FloatFuncs for f32 {
$(fn $name(self $(,$arg: $arg_ty)*) -> $ret {
#[cfg(feature = "std")]
Copy link
Member

Choose a reason for hiding this comment

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

slightly pedantic, but if we do want to use this trait in both cases (which would require us to call the trait methods directly, or give them names that do not shadow the std names) I would do this the other way around: if libm is set, always use libm, if libm is not set and std is set use std. (Because perhaps for some reason you want to use musl even if std is available? Tell me if I'm wrong 💁)

@@ -4,6 +4,91 @@

use arrayvec::ArrayVec;

/// Defines a trait that chooses between libstd or libm implementations of float methods.
Copy link
Member

Choose a reason for hiding this comment

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

so... is this actually necessary in the case of std? If std is present then these methods will already exist on our types, and method resolution will work, so it feels like we could get away with only defining this trait if the musl feature is active?

(the fact that this trait does nothing if std is active is supported by the need to add #[allow(unused_code)] on the import)

src/cubicbez.rs Outdated Show resolved Hide resolved
src/mindist.rs Outdated Show resolved Hide resolved
src/param_curve.rs Outdated Show resolved Hide resolved
src/quadbez.rs Outdated Show resolved Hide resolved
@notgull
Copy link
Contributor Author

notgull commented Mar 7, 2023

Thanks for the review!

That said, I'm not sure if we want to assume the solution is always musl?

Just to clarify, the libm crate doesn't link to musl's libm. It's actually a from-scratch implementation of math routines in pure Rust. However, since it's pure Rust (read: not hardware-accelerated), I prefer using hardware intrinsics when available. That's why I prefer std over libm in this patch, since libm tends to be significantly slower.

I've reviewed this as-is; my main issue currently is that the trait is sort of half-baked, since we define it in both cases but it's only ever used if std is missing

Good point! I'll go back and fix it like this when I have the chance.

@notgull
Copy link
Contributor Author

notgull commented Mar 14, 2023

@cmyr What is the status of reviewing this PR?

@cmyr
Copy link
Member

cmyr commented Mar 17, 2023

Just to clarify, the libm crate doesn't link to musl's libm. It's actually a from-scratch implementation of math routines in pure Rust. However, since it's pure Rust (read: not hardware-accelerated), I prefer using hardware intrinsics when available. That's why I prefer std over libm in this patch, since libm tends to be significantly slower.

Okay cool, I hadn't understood that on my first pass.

This can get rebased to bring in the more recent clippy fixes, and then I'm going to solicit a second opinion; modulo that I'm happy to merge!

@richard-uk1
Copy link
Collaborator

Prior art: #172

@dfrg
Copy link
Contributor

dfrg commented Mar 17, 2023

I’m so conflicted on this one. I’d really like to see no-std support in kurbo and this PR does what essentially amounts to the best that can be done given the constraints.

My primary concern is that pulling in a dependency that transitively references kurbo without std will still make use of the suboptimal math functions even if std exists in the full dependency graph. There is an issue in libm that suggests making all symbols weak which would solve this problem but that hasn’t been done yet.

That said, since we can override this by adding an explicit kurbo dep with std, I’m also inclined to suggest merging.

+1

@notgull
Copy link
Contributor Author

notgull commented Mar 17, 2023

I've rebased on the latest master.

Copy link
Member

@cmyr cmyr left a comment

Choose a reason for hiding this comment

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

I think chad makes an interesting point, but there is a clear workaround available if this problem crops up, so I think there is clear value in landing this.

edit: I've made one last suggestion below (https://github.com/linebender/kurbo/pull/242/files#r1140642582), happy to merge when we figure that out.

Thanks @notgull!

src/common.rs Outdated Show resolved Hide resolved
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.

4 participants