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

changed BUFFER_SIZE to usize (cf. #49) #52

Merged
merged 6 commits into from Nov 17, 2021

Conversation

b-ma
Copy link
Collaborator

@b-ma b-ma commented Nov 15, 2021

Not really sure about these clippy directives, maybe the casting from usize to u32 should be handled in another way (even if I can't see how this could fail):

(Besides, I have two tests failling (node::oscillator::tests::default_sine_rendering_should_match_snapshot and node::iir_filter::test::default_periodic_wave_rendering_should_match_snapshot), I don't think they are related to my changes because they are also failing on the master branch, it seems that the allowed error is too low. I can fill a new issue with more details if it's not something you are already aware of.)

@Jerboas86
Copy link
Contributor

Jerboas86 commented Nov 15, 2021

Not really sure about these clippy directives, maybe the casting from usize to u32 should be handled in another way (even if I can't see how this could fail)

usize is 8 bytes on 64bits architectures, you will have truncations for values over 4294967295.
You can use try_from(u: usize) for make it explicit that the conversion is fallible. But buffer of this size will probably fail for other reason.

What do you think @orottier ?

@b-ma
Copy link
Collaborator Author

b-ma commented Nov 15, 2021

Yes and what is casted here is a constant. Anyway I could understand you don't consider this clean (and the buffer size might also become configurable in the next version of the API), so let me know.

Also, I don't really understand why the build failed (using cargo clippy --all-targets -- -D warnings, I have several errors popping up, but not this one), any hints?

@Jerboas86
Copy link
Contributor

Yes and what is casted here is a constant.

Yes, you're right.

Another way would be to make a BUFFER_SIZE_U32 and derives BUFFER_SIZE_USIZE from it. this avoids the clippy error and try_from call.

Copy link
Contributor

@Jerboas86 Jerboas86 left a comment

Choose a reason for hiding this comment

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

All these explicit casts that fly away, great!
Thanks @b-ma

I've made some suggestions, but i'm sure @orottier will have precious thoughts and comments. Let's wait his review to see what should be our next move.

Comment on lines 1520 to +1528
assert_float_eq!(
output.channel_data(0).as_slice(),
&ref_sine.data[..],
ulps_all <= 40
ulps_all <= 64
);
assert_float_eq!(
output.channel_data(1).as_slice(),
&ref_sine.data[..],
ulps_all <= 40
ulps_all <= 64
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggestions:

  • Maybe we should switch to absolute tolerance comparison. ULP are difficult to reason about, and since we are using values from a small interval, absolute error should be fine.
  • Cherry pick this commit to make a separate PR

Copy link
Owner

Choose a reason for hiding this comment

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

This is something I've noticed too. Sometimes tests on my machine fail with the current ulps_all margins.
I agree we should probably use absolute values for all integration/snapshot tests

Copy link
Owner

Choose a reason for hiding this comment

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

I have created #53 for this

tests/offline.rs Outdated
Comment on lines 14 to 16
// compute modulo before to avoid clippy::assertions-on-constants errors
let modulo = LENGTH % BUFFER_SIZE;
assert!(modulo != 0);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggestions:

  • maybe try assert_ne!(LENGTH % BUFFER_SIZE,0) to see if it passes clippy warning

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yes thanks, it works

src/lib.rs Outdated
/// Render quantum size (audio graph is rendered in blocks of this size)
pub const BUFFER_SIZE: usize = 128;
pub const BUFFER_SIZE: usize = BUFFER_SIZE_U32 as usize;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggestions:

  • replace BUFFER_SIZE with BUFFER_SIZE_USIZE to make explicit that there is several BUFFER_SIZE_* variables, even when usize type is used.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

My (not really opinionated) rationale here was that as BUFFER_SIZE_U32 is finally only used in low-level io.rs, maybe it's good to reinforce that it is a kind of "special" const compared to BUFFER_SIZE (which should be preferred).

Copy link
Owner

Choose a reason for hiding this comment

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

I also prefer BUFFER_SIZE as a succinct name, conforming nicely to the spec naming.
If the U32 variant is only used in io.rs, let's move the const there so it will not pollute the pub namespace.
You could even use the usize variant in that file, and only cast at the final moment when passsing it to cpal. In that case we don't even need a const

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ok, used u32::try_from(u: usize) at last moment and clippy doesn't complain

@orottier
Copy link
Owner

Thanks for your work @b-ma, looking very good!
Regarding clippy: the CI runner always uses the latest version of rustc and clippy. Maybe that is causing the difference with your local output?

@b-ma
Copy link
Collaborator Author

b-ma commented Nov 17, 2021

Regarding clippy, I had a couple of errors before getting to the one reported by the CI. It's fixed in 76a1e43.

@orottier
Copy link
Owner

Excellent. Code is much cleaner now

@orottier orottier merged commit a32309a into orottier:main Nov 17, 2021
@b-ma b-ma deleted the feature/buffer-size branch November 4, 2023 06:43
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

3 participants