Skip to content

Commit f7ff7c9

Browse files
committed
AudioParam premature optim: avoid clamp, avoid unecessary assignments
1 parent f596d6c commit f7ff7c9

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

src/param.rs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -754,9 +754,11 @@ impl AudioParamProcessor {
754754

755755
if value.is_nan() {
756756
value = self.default_value;
757+
} else {
758+
// do not use `clamp` because it has extra branches for NaN or when max < min
759+
value = value.max(self.min_value).min(self.max_value);
757760
}
758-
759-
output.channel_data_mut(0)[0] = value.clamp(self.min_value, self.max_value);
761+
output.channel_data_mut(0)[0] = value;
760762
} else {
761763
// a-rate processing and input non-zero
762764
output.set_single_valued(false);
@@ -766,9 +768,10 @@ impl AudioParamProcessor {
766768

767769
if o.is_nan() {
768770
*o = self.default_value;
771+
} else {
772+
// do not use `clamp` because it has extra branches for NaN or when max < min
773+
*o = o.max(self.min_value).min(self.max_value);
769774
}
770-
771-
*o = o.clamp(self.min_value, self.max_value)
772775
});
773776
}
774777
} else {
@@ -785,9 +788,10 @@ impl AudioParamProcessor {
785788

786789
if o.is_nan() {
787790
*o = self.default_value;
791+
} else {
792+
// do not use `clamp` because it has extra branches for NaN or when max < min
793+
*o = o.max(self.min_value).min(self.max_value);
788794
}
789-
790-
*o = o.clamp(self.min_value, self.max_value)
791795
});
792796
}
793797
}
@@ -1120,15 +1124,14 @@ impl AudioParamProcessor {
11201124
if end_index_clipped > start_index {
11211125
let mut time = (start_index as f64).mul_add(infos.dt, infos.block_time);
11221126

1127+
let mut value = 0.;
11231128
for _ in start_index..end_index_clipped {
1124-
let value =
1129+
value =
11251130
compute_linear_ramp_sample(start_time, duration, start_value, diff, time);
1126-
11271131
self.buffer.push(value);
1128-
11291132
time += infos.dt;
1130-
self.intrinsic_value = value;
11311133
}
1134+
self.intrinsic_value = value;
11321135
}
11331136
}
11341137

@@ -1220,8 +1223,9 @@ impl AudioParamProcessor {
12201223
if end_index_clipped > start_index {
12211224
let mut time = (start_index as f64).mul_add(infos.dt, infos.block_time);
12221225

1226+
let mut value = 0.;
12231227
for _ in start_index..end_index_clipped {
1224-
let value = compute_exponential_ramp_sample(
1228+
value = compute_exponential_ramp_sample(
12251229
start_time,
12261230
duration,
12271231
start_value,
@@ -1230,10 +1234,10 @@ impl AudioParamProcessor {
12301234
);
12311235

12321236
self.buffer.push(value);
1233-
self.intrinsic_value = value;
12341237

12351238
time += infos.dt;
12361239
}
1240+
self.intrinsic_value = value;
12371241
}
12381242
}
12391243

@@ -1345,18 +1349,19 @@ impl AudioParamProcessor {
13451349
if end_index_clipped > start_index {
13461350
let mut time = (start_index as f64).mul_add(infos.dt, infos.block_time);
13471351

1352+
let mut value = 0.;
13481353
for _ in start_index..end_index_clipped {
13491354
// check if we have reached start_time
1350-
let value = if time - start_time < 0. {
1355+
value = if time - start_time < 0. {
13511356
self.intrinsic_value
13521357
} else {
13531358
compute_set_target_sample(start_time, time_constant, end_value, diff, time)
13541359
};
13551360

13561361
self.buffer.push(value);
1357-
self.intrinsic_value = value;
13581362
time += infos.dt;
13591363
}
1364+
self.intrinsic_value = value;
13601365
}
13611366
}
13621367

@@ -1445,19 +1450,20 @@ impl AudioParamProcessor {
14451450
if end_index_clipped > start_index {
14461451
let mut time = (start_index as f64).mul_add(infos.dt, infos.block_time);
14471452

1453+
let mut value = 0.;
14481454
for _ in start_index..end_index_clipped {
14491455
// check if we have reached start_time
1450-
let value = if time - start_time < 0. {
1456+
value = if time < start_time {
14511457
self.intrinsic_value
14521458
} else {
14531459
compute_set_value_curve_sample(start_time, duration, values, time)
14541460
};
14551461

14561462
self.buffer.push(value);
1457-
self.intrinsic_value = value;
14581463

14591464
time += infos.dt;
14601465
}
1466+
self.intrinsic_value = value;
14611467
}
14621468
}
14631469

0 commit comments

Comments
 (0)