perf(audio): no stutter in step with Link's footsteps with a cabled DualSense (the speaker's sound search reads a few spots, not 64 KB) #2312
No reviewers
Labels
No labels
area/accessibility
area/audio
area/input
area/install
area/menu
area/renderer
area/saves
area/tooling
area/upscaling
beanstalk: approved
beanstalk: failed
beanstalk: needs approval
beanstalk: running
beanstalk: tested
ci: approved
good first issue
gpu/amd
gpu/igpu
gpu/intel
gpu/nvidia
help wanted
kind
bug
kind
chore
kind
docs
kind
feature
kind
performance
kind
question
needs measurements
platform/linux
platform/macos
platform/steam-deck
platform/windows
preview
priority
critical
priority
high
priority
low
regression
risk
high
risk
low
risk
medium
status
blocked
status
confirmed
status
duplicate
status
invalid
status
needs-info
status
wontfix
ci: approved
No milestone
No project
No assignees
3 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!2312
Loading…
Reference in a new issue
No description provided.
Delete branch "Veix/Longshot:fix/pad-speaker-search"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
With a DualSense on a cable, the frame time zigzags in step with Link's footsteps. The controller speaker looks up every sound buffer the game queues by searching up to 64 KB of memory in front of it.
Since #1545, some sound classes play from the DualSense's speaker by default (Navi's voice, Z-targeting, pick-ups, ...). To know which sound a wave buffer belongs to,
AppendWaveBuffercallshos_audio_source_offor every buffer the game queues, and it searched for the wave's header up to 64 KB (BWAV_MAX_BEFORE) in front of the samples, withVirtualQueryover that range first. A buffer that continues a streamed sound, or a later one of a looping sound, has no header in front of it, so the whole 64 KB was searched every time, on the audio thread, while it holds one of the game's virtual cores and the audio lock. A single search took up to 2-3 ms. The game's other threads waited for it, and the frame time rose and fell with each footstep sound (a rhythm at 4.6 Hz, Link's running steps).A wave's first channel always starts right behind its header and its channel table, on the archive's 0x40 alignment: 0x80 bytes for a mono wave, at most 0x280 for 8 channels.
hos_audio_source_ofnow looks only at those spots (at most 9 probes, one page boundary at most) instead of 64 KB. When it finds a header, it takes the start of every channel of that wave from the header's own table and keeps them by address, so a wave's later channels (up to megabytes behind the header) and every later play of the same sound are found by one lookup. An entry is trusted only while the same wave's header (its magic and checksum) is still in front of it. A buffer that neither names is searched as far asmainsearched, once: a later channel queued before its first is found as before, and where that search finds no header (a streamed sound's ring) the place is kept as such, so a ring that comes round again costs a look in the table. Every buffer is still classed, as before.How to see it on
main: a DualSense on a USB cable (the speaker needs its USB sound card; over Bluetooth it stays off), the speaker at its defaults, Castle Town's market, run around. The frame graph zigzags in step with the footsteps; withHOS_PAD_SPEAKER=0it is flat.Read first:
hos_audio_source_ofinhost/audio_source.c,audio_source_find_backandBWAV_FIRST_MAXinhost/audio_source.h, thentests/test_audio_source_load.py.Testing
Please no automatic fixes on this branch (
@fix, bot pushes): review comments are welcome, I make the changes myself.Tested: Windows 11, RTX 4070 SUPER 12 GB, driver 621.24, Ryzen 9 9900X, a DualSense on a USB cable, the speaker at its defaults (the log: "pad speaker: playing on the DualSense's endpoint" in every run). The scripted Castle Town route, two interleaved runs each side (below): the footstep zigzag gone, frames over 20 ms a third of
main's. The first-channel spot checked on the game's own data: all 1,152 sound archives (Sound/Resource/*.bars.zs, unpacked with the game's dictionary), 25,676 waves; every first channel lies right behind its header and channel table (0x80 mono x24,728, 0xc0 stereo x614, up to 0x200 for 6 channels), none elsewhere, the channels always in ascending order.Of the 948 multi-channel waves, 115 have a later channel between 0x280 bytes and 64 KB behind the header (44 of them menu sounds in
UI_Static), whichmainfound with its far search and this finds the same way, once a place; 850 have one beyond 64 KB, whichmainnever found and this finds from their first channel's header.tests/test_audio_source_load.pyfails onmain("asked_most <= 0x280": for a streamed buffermainasks about 64 KB of memory), fails on4f03fec7for review L1 (a later channel queued first not found), and passes here. It counts the far searches: a mono sound played 100 times, 0; a later channel queued first, 10 plays, 1; a streamed ring of 8 places come round 100 times, 8; after the kept places are pushed out by 2,048 others, the later channel found again with at most 1 more.tests/test_audio_source.pyandtests/test_audio_wave_buffer_release.pypass;tests/run_all.py --changed origin/main: 18 passed, 0 failed (1 skipped: Linux only). The whole suite and the Linux build: CI.Still to test:
main(@Beanstalk)Measurements
Verdict: frames over 20 ms in Castle Town 74 and 90 on
main, 26 and 26 here; the footstep rhythm gone (0.46 and 0.57 ms of swing at 4.6 Hz onmain, 0.04 and 0.08 here).Setup: Windows 11, RTX 4070 SUPER 12 GB, driver 621.24, Ryzen 9 9900X, a DualSense on a USB cable, the speaker at its defaults. 2560x1440, DLAA, uncapped (
HOS_FPS_MAX=1000), V-Sync off,RX_VK_LOAD=start,RX_VK_FRAME_GATE=0(main's default), no other switch but the frame trace's, the same on both sides. Both builds from the same lift ofmain753fb5af(lifted code df6d1eb1a7), only the host code differs. The scripted route through Castle Town's market and the castle, in the order main, PR, main, PR; the second market stay from 5 s after its entry (54.5 s, ~4,100 frames), from the frame trace; both runs of each side are given.753fb5af4f03fec7The head
0443f84a(review L1 and L2), one run with the same setup and lift: 22 frames over 20 ms, 0.09 ms of swing at 4.6 Hz, p99 19.1 ms, the game's own time p99 16.3 ms: as the two runs above.Earlier, the same route at 3840x2160 DLAA with one run each on an older lift:
main136 frames over 20 ms,HOS_PAD_SPEAKER=095, this change 83; the 4.6 Hz swing 0.51, 0.08 and 0.11 ms.Risks
The worst case is a sound played from the wrong speaker (the TV instead of the controller, or the other way round). Nothing else in the game's sound or memory changes: every read goes through the same checks as on
main.mainsearched, and found; its wave's channels are kept from then on. One case still differs frommain: a place kept as having no header (a streamed sound's ring) whose memory the game later gives to a wave's later channel is not searched far again, so that sound plays with the voice's previous class until its wave's first channel is queued, which keeps every channel of it.mainsearched it again.AppendWaveBuffer, under the audio lock.Windows only in practice: the speaker needs the DualSense's USB sound card, which the endpoint lookup finds only on Windows.
HOS_PAD_SPEAKER=0turns the speaker off; a revert is enough. A player notices nothing after updating: no save or option changes.Checklist
Ready for review
main; the title is a Conventional Commit.mise run testpasses (any failure that also fails onmainis named).main, or Measurements says why there is nothing to measure.third_party/untouched; nothing GPL-incompatible.When it applies
mainand passes here.tests/that reads changed source is updated, not weakened.Assisted-by:/Co-Authored-By:trailers.Ready to merge
main, measured again or a comment says why the numbers still hold.Written with the help of an AI coding agent.
Tested in game: Windows 11, RTX 4070 SUPER 12 GB, driver 621.24, a DualSense on a USB cable with the speaker at its defaults: Castle Town's market and the castle on the scripted route, two runs of main and two of this PR interleaved at 1440p DLAA uncapped. The frame graph's zigzag in step with Link's footsteps is gone; frames over 20 ms 74 and 90 on main, 26 and 26 here.
@review
Recommendation: Request changes. The measured Windows run shows a substantial reduction in footstep-related frame-time spikes, but the shorter search can repeatedly misroute channels that main classified.
The change replaces a 64 KB search on each queued audio buffer with a short search and an address cache. It reduces the measured frame-time spikes for a cabled DualSense, at the cost of losing classification for some later channels.
Blocking (1): must change before merging.
host/audio_source.c:284: A later channel is no longer found unless its first channel has already populated the cache, and the cache does not guarantee that this only happens once. (more on the line) Do: Preserve classification of later channels independently of first-channel queue order and cache eviction, while keeping the common streamed-buffer search bounded; add a game-free test covering repeated later-channel-first plays and eviction.Nits (1): optional.
host/audio_source.c:295: The description says the cache records channels up to megabytes behind the header, but it discards every offset above 64 KB. (more on the line) Do: Describe the 64 KB cache limit and its effect, or support the larger offsets with appropriate validation and a test.@fixcan do L1 and L2.Checks: Failing on this head, which is behind its base, where it passes:
PR description / Description follows the template (pull_request_target).@rebase(or@fix) brings it up to date and runs it again; not held against it. Still running: 9 checks.More: does it help, other systems, switches, what was read
Does it help: Yes. In two interleaved Castle Town runs per side on Windows with an RTX 4070 SUPER and Ryzen 9 9900X, frames over 20 ms fell from 74/90 to 26/26, and the 4.6 Hz frame-time swing fell from 0.46/0.57 ms to 0.04/0.08 ms. These are the author's reported frame-trace numbers.
Other systems: The lookup is reached only while the controller speaker is active and runs under the audio lock. Windows page-read checks and Linux compilation are affected; the reported game runs cover Windows on one machine, while the requested additional Windows hardware run and host builds remain outstanding.
Switches: None added, changed or removed.
Reviewed:
4f03fec72aagainst753fb5af68: Read AGENTS.md, CONTRIBUTING.md, docs/dev/standards.md, the pull request template, the commits, all three changed files, and the relevant audio caller and memory-region check. Skimmed references in other tests. docs/review-checklist.md and ../Tooling/AGENTS.md were absent. No generated files or recorded runs were changed.; nothing run (a code review).Checked and fine:
Testing: No code or tests were run, as requested. The author reports the game-free audio tests passing and the Windows game measurements; CI is still running.
Overlaps and merge order: Not checked; the network is closed.
Automated code review by BeanBot (gpt-6.1-terra), asked for by HumanBean. Nothing was run; a maintainer decides.
@ -260,3 +283,2 @@}before = readable;#endifsize_t before = BWAV_FIRST_MAX; /* (as far as the game's memory is mapped before it) */L1 (blocking): A later channel is no longer found unless its first channel has already populated the cache, and the cache does not guarantee that this only happens once.
When: If a wave's second channel starts more than 0x280 but at most 0x10000 bytes after its header and is queued before its first channel, main finds the header but this search returns -1. If the first channel is never queued, or its cache entry is displaced before the later channel is queued on another play, the channel keeps the voice's previous class on every such play and can be routed to the wrong output.
Do: Preserve classification of later channels independently of first-channel queue order and cache eviction, while keeping the common streamed-buffer search bounded; add a game-free test covering repeated later-channel-first plays and eviction.
@ -265,0 +292,4 @@const unsigned channels = audio_source_u16(h + BWAV_CHANNELS);for (unsigned ch = 0; ch < channels; ch++) { /* (every channel's start, by the header's table) */const uint32_t off = audio_source_u32(h + BWAV_HEADER + ch * BWAV_CHANNEL + BWAV_CHANNEL_DATA);if (off < BWAV_ALIGN * 2 || off > BWAV_MAX_BEFORE) continue;L2 (nit): The description says the cache records channels up to megabytes behind the header, but it discards every offset above 64 KB.
When: For a wave with a channel offset greater than 0x10000, finding its first channel does not cache that later channel; its subsequent buffer cannot be found by the 0x280-byte search.
Do: Describe the 64 KB cache limit and its effect, or support the larger offsets with appropriate validation and a test.
L1 and L2 are fixed in
0443f84a, the way L1 asked. The gain holds: 22 frames over 20 ms in the market, against 74 and 90 onmain.L1: right: 115 of the game's 948 multi-channel waves have a later channel between 0x280 bytes and 64 KB behind the header (44 are menu sounds). A buffer that neither the kept places nor the near search name is now searched as far as
mainsearched, once a place: a later channel queued first is found, and where no header is found (a streamed sound's ring) the place is kept as such, so the ring costs a table lookup when it comes round. The new test counts the far searches: later channel first, 10 plays: found every time, 1 far search; mono sound, 100 plays: 0; a ring of 8 places, 100 rounds: 8; after eviction: found, at most 1 more. It fails on4f03fec7and onmain. One case still differs frommain(a no-header place later reused for a wave); it is in the Risks section.L2: channel starts are kept wherever they lie, each checked against its header before use, so the 850 waves with a channel beyond 64 KB, which
mainnever classed, are classed too.Jer9 referenced this pull request2026-11-23 16:17:47 +00:00