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

Audio: Optimize sink checks in crossover_s32_default #9178

Conversation

ShriramShastry
Copy link
Contributor

Optimize crossover_s32_default by 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.

@ShriramShastry ShriramShastry force-pushed the optimize_crossover_generic_enhancements branch from aeb61cb to f32aebb Compare May 31, 2024 05:47
lyakh
lyakh previously requested changes May 31, 2024
Copy link
Collaborator

@lyakh lyakh left a comment

Choose a reason for hiding this comment

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

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;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

indentation

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);
Copy link
Collaborator

Choose a reason for hiding this comment

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

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 */
Copy link
Collaborator

Choose a reason for hiding this comment

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

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);
Copy link
Collaborator

Choose a reason for hiding this comment

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

and now you have this calculation twice? No, please, remove this.

Copy link
Contributor Author

@ShriramShastry ShriramShastry left a comment

Choose a reason for hiding this comment

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

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.

@ShriramShastry ShriramShastry force-pushed the optimize_crossover_generic_enhancements branch from f32aebb to 4e0131a Compare May 31, 2024 12:58
@ShriramShastry ShriramShastry requested a review from lyakh May 31, 2024 13:18
@ShriramShastry ShriramShastry force-pushed the optimize_crossover_generic_enhancements branch from 4e0131a to 66b65fb Compare May 31, 2024 13:29
@ShriramShastry ShriramShastry marked this pull request as ready for review June 1, 2024 02:20
@ShriramShastry ShriramShastry requested a review from a team as a code owner June 1, 2024 02:20
for (i = 0; i < frames; i++) {
/* Iterate over frames */
for (i = 0, idx = ch; i < frames; i++, idx += nch) {
/* Read source */
Copy link
Collaborator

Choose a reason for hiding this comment

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

indentation - there's a spurious space in front of the comment

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, Thanks.

@lyakh lyakh dismissed their stale review June 3, 2024 06:13

issue addressed, thanks, would be nice to clean up indentation though

@ShriramShastry ShriramShastry force-pushed the optimize_crossover_generic_enhancements branch from 66b65fb to 451f364 Compare June 3, 2024 07:48
@ShriramShastry ShriramShastry requested a review from lyakh June 3, 2024 09:32
Copy link
Collaborator

@kv2019i kv2019i left a comment

Choose a reason for hiding this comment

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

A few minor style comments inline, but the optimization itself seems good and gets and check out of the inner loop.

src/audio/crossover/crossover_generic.c Show resolved Hide resolved
@ShriramShastry ShriramShastry force-pushed the optimize_crossover_generic_enhancements branch from 451f364 to e9c1e3a Compare June 24, 2024 06:33
Copy link
Member

@lgirdwood lgirdwood left a comment

Choose a reason for hiding this comment

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

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.

Comment on lines 240 to 257
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 */
Copy link
Member

Choose a reason for hiding this comment

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

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.

src/audio/crossover/crossover_generic.c Show resolved Hide resolved
@@ -225,36 +225,29 @@ static void crossover_s32_default(struct comp_data *cd,
int32_t num_sinks,
uint32_t frames)
{
/* Array to hold active sink streams; initialized to null */
Copy link
Member

Choose a reason for hiding this comment

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

all this commit does is delete the comments you added, please check your commits to make sure they make sense before pushing them.

@lgirdwood lgirdwood added this to the v2.11 milestone Jun 26, 2024
@ShriramShastry ShriramShastry force-pushed the optimize_crossover_generic_enhancements branch from e9c1e3a to a41f27d Compare August 20, 2024 06:18
Copy link
Member

@cujomalainey cujomalainey left a comment

Choose a reason for hiding this comment

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

all my previous comments are still valid, please fix them

@ShriramShastry ShriramShastry force-pushed the optimize_crossover_generic_enhancements branch from a41f27d to 833cfb6 Compare August 24, 2024 13:09
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>
Copy link
Contributor Author

@ShriramShastry ShriramShastry left a comment

Choose a reason for hiding this comment

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

Thank you for reviewing the change. I addressed the review comments. Please have a look.

src/audio/crossover/crossover_generic.c Show resolved Hide resolved
@cujomalainey cujomalainey merged commit b381f55 into thesofproject:main Aug 26, 2024
42 of 47 checks passed
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.

6 participants