From ddd4e70f80316e0aa2f5a650f245e7bdee70248b Mon Sep 17 00:00:00 2001 From: Ilia Sharin Date: Sun, 8 Nov 2026 22:08:09 -0500 Subject: [PATCH] Engine: Drain autoreleased chunks on the feeder, pump and waveform threads The feeder and pump threads run their loops with no autorelease pool of their own, so every autoreleased object a pass makes (the Float32 chunks ChunkList's removeSamplesAsFloat32 and convertChunk hand back, and their data) lived until the thread ended: the footprint grew for as long as playback went on, about 1 MB/s with a decoder that returns one ~20 ms chunk per readAudio. The waveform scanner drained its pool only per decoded chunk, so a whole track's converted blocks piled up while it scanned (hundreds of MB for a long track of small chunks). Wrap the feeder's feed(chunk), each pump pass, and each scanned block in an autoreleasepool. --- Audio/Engine/Facade/WaveformScanner.swift | 98 +++++++++++++---------- Audio/Engine/Feeder/Feeder.swift | 4 +- Audio/Engine/Pump/Pump.swift | 4 +- 3 files changed, 61 insertions(+), 45 deletions(-) diff --git a/Audio/Engine/Facade/WaveformScanner.swift b/Audio/Engine/Facade/WaveformScanner.swift index f183e36df..352dfbd18 100644 --- a/Audio/Engine/Facade/WaveformScanner.swift +++ b/Audio/Engine/Facade/WaveformScanner.swift @@ -148,6 +148,11 @@ public final class WaveformScanner { return accumulator.finalWaveform() } + /// After one converted block: next block, no frames left, or the segment's end reached. + private enum BlockStep { + case next, stop, done + } + /// What the decoder said of the track. private struct Track { let rate: Double @@ -179,56 +184,63 @@ public final class WaveformScanner { chunk.lossless = track.lossless converter.add(chunk) while !converter.isEmpty(), !cancelled.isSet { - let floats = converter.removeSamples(asFloat32: 4096) - let frames = floats.frameCount() - guard frames > 0 else { break } - let format = floats.format - let channels = Int(format.mChannelsPerFrame) - let data = floats.removeSamples(frames) - - if framesPerBucket == 0 { - let rate = format.mSampleRate - let scale = track.rate > 0 ? rate / track.rate : 1 - position = Double(start) * scale - if track.totalFrames > 0 { - let total = Double(track.totalFrames) * scale - framesPerBucket = accumulator.isProgressive ? total / Double(Waveform.bucketCount) : Double(Self.unknownLengthBlock) - stop = end.map { Double($0) * scale } ?? total * 1.01 + rate - } else { - framesPerBucket = Double(Self.unknownLengthBlock) - stop = Self.unknownLengthLimit * rate + // The block's temporaries (the float chunk, its data) go per block: drained only per readAudio, a + // decoder that hands over small chunks piled up the whole track here (hundreds of MB) + let step = autoreleasepool { () -> BlockStep in + let floats = converter.removeSamples(asFloat32: 4096) + let frames = floats.frameCount() + guard frames > 0 else { return .stop } + let format = floats.format + let channels = Int(format.mChannelsPerFrame) + let data = floats.removeSamples(frames) + + if framesPerBucket == 0 { + let rate = format.mSampleRate + let scale = track.rate > 0 ? rate / track.rate : 1 + position = Double(start) * scale + if track.totalFrames > 0 { + let total = Double(track.totalFrames) * scale + framesPerBucket = accumulator.isProgressive ? total / Double(Waveform.bucketCount) : Double(Self.unknownLengthBlock) + stop = end.map { Double($0) * scale } ?? total * 1.01 + rate + } else { + framesPerBucket = Double(Self.unknownLengthBlock) + stop = Self.unknownLengthLimit * rate + } } - } - if mono.count < frames { - mono = [Float](repeating: 0, count: frames) - } - if channels > 1 { - if downmix == nil || downmixFormat.map({ !Self.sameFormat($0, format) }) ?? true { - let config = floats.channelConfig != 0 ? floats.channelConfig : AudioChunk.guessChannelConfig(UInt32(channels)) - downmix = DownmixProcessor(inputFormat: Self.floatFormat(rate: format.mSampleRate, channels: channels), inputConfig: config, - andOutputFormat: Self.floatFormat(rate: format.mSampleRate, channels: 1), outputConfig: UInt32(AudioChannelFrontCenter)) - downmixFormat = format + if mono.count < frames { + mono = [Float](repeating: 0, count: frames) } - data.withUnsafeBytes { input in - mono.withUnsafeMutableBufferPointer { output in - downmix?.process(input.baseAddress!, frameCount: frames, output: output.baseAddress!) + if channels > 1 { + if downmix == nil || downmixFormat.map({ !Self.sameFormat($0, format) }) ?? true { + let config = floats.channelConfig != 0 ? floats.channelConfig : AudioChunk.guessChannelConfig(UInt32(channels)) + downmix = DownmixProcessor(inputFormat: Self.floatFormat(rate: format.mSampleRate, channels: channels), inputConfig: config, + andOutputFormat: Self.floatFormat(rate: format.mSampleRate, channels: 1), outputConfig: UInt32(AudioChannelFrontCenter)) + downmixFormat = format } - } - } else { - data.withUnsafeBytes { input in - mono.withUnsafeMutableBytes { output in - output.copyMemory(from: UnsafeRawBufferPointer(rebasing: input.prefix(frames * MemoryLayout.size))) + data.withUnsafeBytes { input in + mono.withUnsafeMutableBufferPointer { output in + downmix?.process(input.baseAddress!, frameCount: frames, output: output.baseAddress!) + } + } + } else { + data.withUnsafeBytes { input in + mono.withUnsafeMutableBytes { output in + output.copyMemory(from: UnsafeRawBufferPointer(rebasing: input.prefix(frames * MemoryLayout.size))) + } } } - } - // Not past the segment's end, which the next segment reads. - let counted = min(frames, max(0, Int((stop - position).rounded(.up)))) - mono.withUnsafeBufferPointer { accumulator.add(UnsafeBufferPointer(rebasing: $0.prefix(counted)), at: Int(position), framesPerBucket: framesPerBucket) } - position += Double(frames) - reporter.tick() - if position >= stop { return true } + // Not past the segment's end, which the next segment reads. + let counted = min(frames, max(0, Int((stop - position).rounded(.up)))) + mono.withUnsafeBufferPointer { accumulator.add(UnsafeBufferPointer(rebasing: $0.prefix(counted)), at: Int(position), framesPerBucket: framesPerBucket) } + position += Double(frames) + reporter.tick() + if position >= stop { return .done } + return .next + } + if step == .stop { break } + if step == .done { return true } } } return false diff --git a/Audio/Engine/Feeder/Feeder.swift b/Audio/Engine/Feeder/Feeder.swift index 5a9545c2e..d617e6e6b 100644 --- a/Audio/Engine/Feeder/Feeder.swift +++ b/Audio/Engine/Feeder/Feeder.swift @@ -279,7 +279,9 @@ public final class Feeder { } let decodeMs = EngineLog.milliseconds(since: start) if let chunk, chunk.frameCount() > 0 { - feed(chunk) + // The conversion's temporaries (float chunks, their data) go per chunk: this thread has no other + // pool, so outside one they lived as long as the thread (about 1 MB/s with a small-chunk decoder) + autoreleasepool { feed(chunk) } } else { advance() } diff --git a/Audio/Engine/Pump/Pump.swift b/Audio/Engine/Pump/Pump.swift index 56739af66..ff8bcc3db 100644 --- a/Audio/Engine/Pump/Pump.swift +++ b/Audio/Engine/Pump/Pump.swift @@ -216,7 +216,9 @@ public final class Pump { while isRunning { let start = EngineLog.now() let gap = EngineLog.milliseconds(since: last) - let worked = pass() + // One pool per pass: this thread has no other, so whatever a pass (or a DSP stage) autoreleases would + // otherwise live as long as playback + let worked = autoreleasepool { pass() } let took = EngineLog.milliseconds(since: start) if gap > EngineLog.slowPass * 1000 || took > EngineLog.slowPass * 1000 { let shallowMs = Double(cog_ring_readable(ring)) / outputFormat.sampleRate * 1000