Use const& in inner loop functions

Bug #1113637 reported by Owen Williams
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Mixxx
Fix Released
Undecided
Owen Williams

Bug Description

I noticed in soundmanager.cpp that two inner functions use copies rather than references:

QHash<AudioOutput, const CSAMPLE*> requestBuffer(QList<AudioOutput> outputs, unsigned long iFramesPerBuffer, SoundDevice *device, double streamTime = 0);

void pushBuffer(QList<AudioInput> inputs, short *inputBuffer, unsigned long iFramesPerBuffer, unsigned int iFrameSize);

Note that a copy of the QList is made on every call.

changing these to const references appears to have a nice impact on performance:

Before: Debug [Main]: Stat("SoundDevicePortAudio::callbackProcess output 0, HDA Intel: STAC92xx Analog (hw:0,0)","count=478,sum=2.15742e+08ns,average=451343ns,min=132001ns,max=1.77272e+06ns,variance=7.36831e+10ns^2,stddev=271446ns")

After:
Debug [Main]: Stat("SoundDevicePortAudio::callbackProcess output 0, HDA Intel: STAC92xx Analog (hw:0,0)","count=452,sum=1.35353e+08ns,average=299454ns,min=79619ns,max=1.36219e+06ns,variance=4.88806e+10ns^2,stddev=221089ns")

patch included

Tags: performance

Related branches

Revision history for this message
Owen Williams (ywwg) wrote :
Revision history for this message
RJ Skerry-Ryan (rryan) wrote :

Huh - assuming this is not just noise that's a nice win. Qt has a feature called implicit sharing which prevents copying the QList. Instead, it keeps a reference count and the copy constructor shares data across all the QLists until one of them is modified at which point it transparently copies the list right before the modification. So unless Qt is mistakenly thinking we're modifying the list and detaching it, I think this doesn't actually copy the elements of the list.

What this does save on is incrementing the reference count and creating a new QList wrapper on the stack. If the measurements aren't noise then this could warrant a quick scan through the engine code to get rid of other uses of implicit sharing.

Patch looks good for 1.11.0 to me -- logic-wise it's a no-op.

Changed in mixxx:
milestone: none → 1.11.0
Revision history for this message
William Good (bkgood) wrote :

Indeed -- QList's implicit sharing is why I wrote that code with copying instead of references (or pointers, same difference). This is surprising.

RJ Skerry-Ryan (rryan)
Changed in mixxx:
assignee: nobody → Owen Williams (ywwg)
status: New → In Progress
Revision history for this message
RJ Skerry-Ryan (rryan) wrote :

Hm. I can't reproduce the improvement on my computer:

without patch:

Debug [Main]: Stat("SoundDevicePortAudio::callbackProcess 0, HDA Intel: CONEXANT Analog (hw:0,0)","count=6086,sum=1.81813e+09ns,average=298739ns,min=32965ns,max=468774ns,variance=8.72133e+09ns^2,stddev=93388.1ns")
Debug [Main]: Stat("SoundDevicePortAudio::callbackProcess output 0, HDA Intel: CONEXANT Analog (hw:0,0)","count=6086,sum=1.78023e+09ns,average=292512ns,min=29962ns,max=462279ns,variance=8.52408e+09ns^2,stddev=92325.9ns")

with patch:

Debug [Main]: Stat("SoundDevicePortAudio::callbackProcess 0, HDA Intel: CONEXANT Analog (hw:0,0)","count=6394,sum=2.00696e+09ns,average=313882ns,min=31289ns,max=478412ns,variance=7.06415e+09ns^2,stddev=84048.5ns")
Debug [Main]: Stat("SoundDevicePortAudio::callbackProcess output 0, HDA Intel: CONEXANT Analog (hw:0,0)","count=6394,sum=1.96274e+09ns,average=306965ns,min=27867ns,max=471638ns,variance=6.97607e+09ns^2,stddev=83522.9ns")

Revision history for this message
RJ Skerry-Ryan (rryan) wrote :

In either case this patch can only help.

Changed in mixxx:
status: In Progress → Fix Committed
tags: added: performance
Revision history for this message
Owen Williams (ywwg) wrote :

You're right, it probably doesn't make sense to apply this patch anywhere else in the code. These two functions are extremely heavy-hit, though, so any gains we can get are probably worth it.

Revision history for this message
Owen Williams (ywwg) wrote :

I meant "everywhere", not "anywhere"

RJ Skerry-Ryan (rryan)
Changed in mixxx:
status: Fix Committed → Fix Released
Revision history for this message
Swiftb0y (swiftb0y) wrote :

Mixxx now uses GitHub for bug tracking. This bug has been migrated to:
https://github.com/mixxxdj/mixxx/issues/6889

lock status: Metadata changes locked and limited to project staff
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.