-
Notifications
You must be signed in to change notification settings - Fork 5
perf(processor): reduce allocations and avoid redundant analysis #36
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
Merged
Conversation
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
- Allocate a single filteredFrame and reuse it in: - measureWithLoudnorm - applyLoudnormAndMeasure - Remove repeated ffmpeg.AVFrameAlloc()/AVFrameFree() calls; defer a single ffmpeg.AVFrameFree and use AVFrameUnref between iterations - Reduces heap churn and CPU overhead during measurement/encoding loops, improving throughput and lowering memory pressure
- Add MeasureOutputRegions to run silence and speech measurements using one audio reader, avoiding repeated file open/demux/decode cycles. - Introduce measureOutputSilenceRegionFromReader and measureOutputSpeechRegionFromReader to allow reusing an open reader. - Update Pass 2 and normalisation (Pass 3/4) flows to call MeasureOutputRegions and attach returned samples to measurements. - Add Reader.Seek to internal/audio/reader.go and use it to rewind the reader between measurements when needed. - Preserve non-fatal behaviour: measurement failures are logged as warnings and do not abort processing. Reduces I/O and decoding overhead during region measurements, improving performance and latency for multi-pass processing without behaviour changes. Signed-off-by: Martin Wimpress <martin@wimpress.org>
- Add computeSilenceMedians to precompute RMS and spectral flux medians used by multiple silence/room-tone detection steps. - Update estimateNoiseFloorAndThreshold and findSilenceCandidatesFromIntervals to accept precomputed medians. - Precompute medians once in AnalyzeAudio and pass them into detection functions to eliminate repeated O(n log n) sorts and extra allocations. - Reduce CPU work and memory allocations for long recordings; no change to detection thresholds or observable behaviour. Signed-off-by: Martin Wimpress <martin@wimpress.org>
- Replace linear scan with sort.Search for O(log n) lookup when locating the first interval at or after the requested start time. - Add clarifying comment that intervals are already sorted by timestamp from the collection loop in AnalyzeAudio. - Adjust out-of-range check to return nil when startIdx >= len(intervals). Reduces CPU work in analyzer for large interval lists without changing behaviour. Signed-off-by: Martin Wimpress <martin@wimpress.org>
- Extract spectral metrics once per filtered frame and reuse for accumulators - Add spectral parameter to extractIntervalFrameMetrics and extractFrameMetadata - Replace repeated extractSpectralMetrics calls with single extraction per frame - No functional change; reduces CPU work and allocations during analysis Signed-off-by: Martin Wimpress <martin@wimpress.org>
Contributor
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1 issue found across 4 files
Confidence score: 3/5
- Potential panic in
internal/processor/analyzer.goifAnalyzeAudiocallscomputeSilenceMedianswith an empty interval slice; this is a user-visible crash risk on certain inputs. - Score reflects a concrete runtime failure risk (severity 6/10) despite being a single issue.
- Pay close attention to
internal/processor/analyzer.go- add guarding for empty interval slices beforecomputeSilenceMedians.
Prompt for AI agents (all issues)
Check if these issues are valid — if so, understand the root cause of each and fix them.
<file name="internal/processor/analyzer.go">
<violation number="1" location="internal/processor/analyzer.go:2371">
P2: Guard against empty interval slices before calling computeSilenceMedians; otherwise AnalyzeAudio can panic on inputs that yield zero interval samples.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
- Return zero-value silenceMedians when searchIntervals is empty - Prevent index errors in computeSilenceMedians and callers that assume a non-empty result Signed-off-by: Martin Wimpress <martin@wimpress.org>
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.
Summary
Reduce CPU and memory usage in the processing pipeline by removing duplicated work
and reusing heavy-weight objects. These changes target hotspots in Pass 1 and the
loudnorm passes to improve throughput and lower peak memory allocations.
Changes
Testing
just testto exercise unit testsagainst
main(measure overall processing time and peak RSS)Related Issues