Skip to content

Reduce allocations during encoding #11

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,11 @@ pub fn encode(

if !ac.is_empty() {
// I'm sure there's a better way to write this; following the Swift atm :)
let maxf = |a: &f64, b: &f64| a.partial_cmp(b).unwrap_or(Ordering::Equal);
let actual_maximum_value = ac
.clone()
.into_iter()
.map(|[a, b, c]| f64::max(f64::max(f64::abs(a), f64::abs(b)), f64::abs(c)))
.max_by(|a, b| a.partial_cmp(b).unwrap_or(Ordering::Equal))
.iter()
.map(|channels| channels.iter().copied().map(f64::abs).max_by(maxf).unwrap())
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since we are iterating over an array, this will not be any less efficient than the hand-rolled version because it will likely be unrolled or vectorized.

.max_by(maxf)
.unwrap();
let quantised_maximum_value = usize::max(
0,
Expand Down