Repository navigation
Server: avoid per-frame deep copy of audio buffers in CreateLevelsForAllConChannels - #3803
Conversation
CServer::CreateLevelsForAllConChannels() took its vecvecsData argument (CVector<CVector<int16_t>>, i.e. one audio buffer per connected client) by value. The function is called from CServer::OnTimer() on the realtime audio timer thread on every frame that has connected clients, so the entire per-client audio data was deep-copied each frame. Pass it by const reference, matching the adjacent vecNumAudioChannels parameter. No behavioural change. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
Seems sensible. We'd need to check if there's really no reason for a copy though. |
|
Not sure how to parse the scope of const here, but it might be a clue that perhaps there's no reason to copy the bits. |
|
Just been looking at this, and studying the life-cycle of
Comments? |
|
OK, I've now just read the analysis on #3804, which I'd overlooked before. It largely aligns with what I came up with above (without using AI!), but reassures me that the writing and reading are protected against concurrent access. If that's the case, I would propose removing the vec parameters from |
|
I believe as long as threads do not write to the same index of the array, we would not even need locking (but some kind of final synchronization after all threads have finished). |
|
The requested deeper analysis is now on the issue: #3804 (comment) — git archaeology of how the copy became per-frame (it was gated and outside the mutex in #42), a memory-model review of the |
Probably on the basis that max clients wasn't so high and it only happened once every 200 frames? It's definitely read-only so should be able to work on a reference (in takes a reference as the first thing inside its main loop: that should have triggered "why not pass references, then?" I guess). |
|
I pushed a commit to this PR that removed the vec parameters and used direct member access instead. It seems to work fine in my test server. @mcfnord, see what you think. @pljones I think this could/should go into 4.0.0 - we will have more betas/rcs before release, which would be plenty of time to find any unlikely issues. EDIT: ah, I see you changed it to 4.0.0 while I was typing. Cool. |
softins
left a comment
There was a problem hiding this comment.
Approving, even though one of the commits is mine.
I am just the messenger. |
|
Then we should probably add Claude as contributor too not sure. |
|
3.12.3? @pljones ? |
|
Should just work. I'll label it. If there's a problem with the cherry-pick, drop the label. |
|
Linux build (nothing taken out) run against backport cleanly. Smoke test ("it runs without odd messages") on GUI Client and Server. Merged to release/3_12. |
What
CServer::CreateLevelsForAllConChannels()declares itsvecvecsDataparameter asThis PR changes it to
const CVector<CVector<int16_t>>&(by const reference), matching the neighbouringvecNumAudioChannelsparameter.Why
The function is called from
CServer::OnTimer()— the realtime audio timer thread — on every frame in which at least one client is connected:Because
CVectorderives fromstd::vector, passing by value deep-copies the entire pre-allocated member vector every frame.vecvecsDatais initialized in theCServerconstructor toiMaxNumChannels(the-usetting) worst-case stereo buffers and is never shrunk, so the copy always covers the full configured channel limit — even a nearly empty server pays it in full. This both allocates on the audio thread (which the codebase otherwise takes care to avoid — see the "no memory must be allocated" comments in theCServerconstructor) and burns memory bandwidth.A small allocation-counting reproduction of the exact copy (150 channels =
-u 150, 2×128 int16 per buffer, 375 frames/s) measured ~56,600 heap allocations/s and ~30 MB/s copied, purely from the by-value parameter; at the default-u 10it is still ~4,100 allocations/s and ~2 MB/s. The copy is not elided at-O2.Change
One parameter, declaration + definition — no behavioural change. The function only reads
vecvecsData.Testing
CONFIG+=headless, Qt 5.15) is clean.OnHandledSignal: 15).clang-format-14applied to the touched files.See #3804 for the analysis.