Perf: cut per-tick allocation and SwiftData churn on the 20 Hz playback path - #117
Merged
Merged
Conversation
Audit findings for audio glitches under high CPU load — all behavior- identical: - The 20 Hz timer wrapped every tick() in a fresh Task even though it already fires on the main run loop: ~1200 heap allocations + actor-hop requeues per minute, each deferring the tick to a later scheduling slot. Run it synchronously via MainActor.assumeIsolated (same pattern as the audio-session handlers). - effectiveEndSeconds read track.audibleEndSeconds (a SwiftData managed read) 20x/s from tick + UI; now served from a ~1 Hz-refreshed cache (the value only moves when the silence scan backfills, minutes before it matters). - The ~5 s periodic persist re-mapped queue.map(\.id) (one SwiftData read per queued track) and built a fresh JSONEncoder every call; the ID array is now invalidated only on queue mutation and the coders are static. - Lock-screen artwork fetch drops to utility priority. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013TJoWkqg8bzkGdzxWhWjWP
sanylax0
approved these changes
Jul 20, 2026
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.
Part 2 of the CPU/memory audit — the playback hot path, most directly tied to "random audio bugs under high CPU load". All changes are behavior-identical.
Taskper tick. The timer already fires on the main run loop, sotick()now runs synchronously viaMainActor.assumeIsolated(the same pattern the audio-session handlers use). The old wrapper was ~1,200 heap allocations + actor-hop re-enqueues per minute, each pushing the tick to a later scheduling slot — worst exactly when the CPU is contended.effectiveEndSecondsstops hitting SwiftData 20×/s.track.audibleEndSecondsis a managed-object read that contends with ingest writes; it's now served from a cache refreshed ~1 Hz intick()(the value only ever changes when the silence scan backfills it, minutes before it matters at track end; a ≤1 s pickup delay is imperceptible). UI reads (secondsUntilTransition) share the cache.queue.map(\.id)(one SwiftData read per queued track) is now cached and invalidated only when the queue actually mutates;JSONEncoder/JSONDecoderare static instead of per-call..utilitypriority.Device check
🤖 Generated with Claude Code
https://claude.ai/code/session_013TJoWkqg8bzkGdzxWhWjWP
Generated by Claude Code