Server: avoid per-frame deep copy of audio buffers in CreateLevelsForAllConChannels#3803
Open
mcfnord wants to merge 1 commit into
Open
Server: avoid per-frame deep copy of audio buffers in CreateLevelsForAllConChannels#3803mcfnord wants to merge 1 commit into
mcfnord wants to merge 1 commit into
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>
mcfnord
marked this pull request as ready for review
July 18, 2026 19:21
Member
|
Seems sensible. We'd need to check if there's really no reason for a copy though. |
Contributor
Author
|
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.