Audio: Optimize sink checks in crossover_s32_default#9178
Conversation
aeb61cb to
f32aebb
Compare
lyakh
left a comment
There was a problem hiding this comment.
ok, this optimisation avoids checking inactive sinks for each frame on each channel. So yes, it should save some time when there are inactive sinks. But please restore the original index calculation
| } | ||
|
|
||
| idx += nch; | ||
| } |
| for (i = 0; i < frames; i++) { | ||
| x = audio_stream_read_frag_s32(source_stream, idx); | ||
| /* Read source */ | ||
| x = audio_stream_read_frag_s32(source_stream, ch + i * nch); |
There was a problem hiding this comment.
you replaced an idx += nch addition with an addition and a multiplication ch + i * nch - subject to any compiler optimisations. Depending on those optimisations this might or might not end up generating different code, but I personally don't think this would be faster. I'd keep the original addition, personally, I prefer this form:
for (i = 0, idx = ch; i < frames; i++, idx += nch) {
but that might be a matter of taste. The original code was run-time equivalent to this, don't think your version is an improvement.
| x = audio_stream_read_frag_s32(source_stream, idx); | ||
| /* Read source */ | ||
| x = audio_stream_read_frag_s32(source_stream, ch + i * nch); | ||
| /* Apply crossover split */ |
There was a problem hiding this comment.
does this comment add any information?
| idx); | ||
| /* Write output to active sinks */ | ||
| for (j = 0; j < active_sinks; j++) { | ||
| y = audio_stream_write_frag_s32(sink_stream[j], ch + i * nch); |
There was a problem hiding this comment.
and now you have this calculation twice? No, please, remove this.
ShriramShastry
left a comment
There was a problem hiding this comment.
Thank you for your code review feedback. I reconsidered the index increment approach and agree that incrementing the index directly within the loop construct is both stylistically clearer and potentially more efficient because it eliminates unnecessary multiplication. Regarding the comment, I recognise that it is redundant and should be removed for clarity. I'll make these changes accordingly.
f32aebb to
4e0131a
Compare
4e0131a to
66b65fb
Compare
| for (i = 0; i < frames; i++) { | ||
| /* Iterate over frames */ | ||
| for (i = 0, idx = ch; i < frames; i++, idx += nch) { | ||
| /* Read source */ |
There was a problem hiding this comment.
indentation - there's a spurious space in front of the comment
There was a problem hiding this comment.
Done, Thanks.
issue addressed, thanks, would be nice to clean up indentation though
66b65fb to
451f364
Compare
kv2019i
left a comment
There was a problem hiding this comment.
A few minor style comments inline, but the optimization itself seems good and gets and check out of the inner loop.
451f364 to
e9c1e3a
Compare
lgirdwood
left a comment
There was a problem hiding this comment.
Best to split this PR in patches that each have one change. Easiest way to do this quickly is to git reset on to origin/main HEAD (this will leave all your changes as a diff and not as patches), then use git gui to individually pick lines for each patch.
| for (j = 0; j < num_sinks; j++) { | ||
| if (bsinks[j]) | ||
| sink_stream[active_sinks++] = bsinks[j]->data; | ||
| } | ||
|
|
||
| /* Process for each channel */ | ||
| for (ch = 0; ch < nch; ch++) { | ||
| idx = ch; | ||
| /* Set current crossover state */ | ||
| state = &cd->state[ch]; | ||
| for (i = 0; i < frames; i++) { | ||
| /* Iterate over frames */ | ||
| for (i = 0, idx = ch; i < frames; i++, idx += nch) { | ||
| /* Read source */ | ||
| x = audio_stream_read_frag_s32(source_stream, idx); | ||
| cd->crossover_split(*x, out, state); | ||
|
|
||
| for (j = 0; j < num_sinks; j++) { | ||
| if (!bsinks[j]) | ||
| continue; | ||
| sink_stream = bsinks[j]->data; | ||
| y = audio_stream_write_frag_s32(sink_stream, | ||
| idx); | ||
| /* Write output to active sinks */ | ||
| for (j = 0; j < active_sinks; j++) { | ||
| y = audio_stream_write_frag_s32(sink_stream[j], idx); | ||
| /* Copy processed data to sink */ |
There was a problem hiding this comment.
Best to split this, as this part is more than the commit message. I would make this a second patch that states the reason and any performance improvement data.
| int32_t num_sinks, | ||
| uint32_t frames) | ||
| { | ||
| /* Array to hold active sink streams; initialized to null */ |
There was a problem hiding this comment.
all this commit does is delete the comments you added, please check your commits to make sure they make sense before pushing them.
e9c1e3a to
a41f27d
Compare
cujomalainey
left a comment
There was a problem hiding this comment.
all my previous comments are still valid, please fix them
a41f27d to
833cfb6
Compare
Optimize the function by pre-initializing active sinks and adjusting loops for better performance and readability. Signed-off-by: Shriram Shastry <malladi.sastry@intel.com>
Add Doxygen comments to describe the parameters and functionality of the crossover_s32_default function. This helps in understanding the function's flow and the purpose of each section. Signed-off-by: Shriram Shastry <malladi.sastry@intel.com>
833cfb6 to
d000d67
Compare
ShriramShastry
left a comment
There was a problem hiding this comment.
Thank you for reviewing the change. I addressed the review comments. Please have a look.
Optimize
crossover_s32_defaultby pre-determining the active sinks before processing frames. This check-in reduces redundant null checks for each frame and channel, leading to performance improvements in processing audio streams with multiple sinks.