Skip to content

ConvolverNode: compute the impulse response tail in large blocks #1463

Closed

Description

@abboskhon-dev

Feature Request

I would like to implement this. CONTRIBUTING.md asks for a design doc before a feature PR, so this issue is the design doc. I have been in this code recently for #1454 and #1455.

Summary

Since #1357 the convolver renders the first 8 partitions on the audio thread and prefetches the rest on the thread pool. The tail is still uniformly partitioned: every tail partition is multiplied once per 128-frame quantum.

The proposal is to compute the tail in large blocks (2048 frames) instead. We run a simpler form of this as a patch on 0.13.5. On a Galaxy S21 it cut reverb CPU by about 2.5x.

There is no JS API change and no docs change. The output stays equal within float rounding.

What the code does today

Read on main at 7da2c383 and on tag 1.0.1.

  • Convolver::process multiplies HEAD_SEGMENTS = 8 partitions (1024 frames) on the audio thread.
  • ConvolverNode::scheduleTails calls trySchedule once per convolver per quantum. At 48 kHz that is 375 tasks a second per convolver: 750 for a stereo impulse response, 1500 for a 4-channel one. Each task passes through the load-balancer thread and then a worker.
  • Convolver::computeTail multiplies all remaining partitions at 129 bins each. For a 2 s impulse response at 48 kHz that is 742 partitions, about 96,000 complex multiply-adds per quantum per convolver.
  • When the slot is not ready, process does the same work inline on the audio thread. OfflineAudioContext::getConvolverThreadPool() returns nullptr, so offline rendering always takes this path.

Design

The head and the prefetch stay. The tail gets its own block size T.

  • Head: 128-frame partitions on the audio thread, covering the first 2 × T frames of the impulse response. HEAD_SEGMENTS becomes 2 × T / 128.
  • Tail: a second partitioned stage with block size T (FFT size 2 × T) over the rest of the impulse response. It takes T input frames and produces T output frames at a time.
  • The tail starts at offset 2 × T, so a tail block is first needed T frames after its input is complete. A worker has that long to finish it. The audio thread never waits, and the inline fallback stays.

With T = 2048 at 48 kHz:

  • Tasks: about 23 a second per convolver instead of 375.
  • Deadline: 2048 frames (about 43 ms) instead of 8 quanta (about 21 ms).
  • Tail work: 16 times fewer multiply-adds, because tail work per frame falls in proportion to the block size. For the 2 s example: about 36 million a second per convolver today, about 2.2 million with 2048-frame blocks.
  • New cost: the tail needs its own 4096-point FFT pair per block, about 23 pairs a second per convolver. Today's tail has no FFT of its own. So the CPU saving on the tail is smaller than 16x. I have not measured by how much.
  • Head work: 32 partitions instead of 8 on the audio thread (about 0.4 million → 1.5 million multiply-adds a second per convolver).

T = 1024 is the smaller step: it keeps today's 21 ms deadline with a 16-partition head and gives 8 times fewer tail multiply-adds.

The head does not have to be 2 × T. In general the head covers T plus the deadline. T = 2048 with today's 21 ms deadline needs a 24-partition head.

A late block costs about as much as a late quantum does today, plus one larger FFT pair: the multiply-adds in one block are about equal to the impulse response length, whatever the block size. It can happen once per T frames, not once per quantum. Offline rendering gets the tail saving directly, because it always computes the tail inline.

Implementation

All in common/cpp/audioapi/dsp/Convolver.{h,cpp} and core/effects/ConvolverNode.cpp.

  • Convolver::init splits the impulse response at 2 × T. Frames before it become 128-frame partitions, as today. Frames after it become T-frame partitions with a 2 × T FFT. An impulse response of 2 × T frames or fewer has no tail and runs on the audio thread only.
  • The audio thread keeps a 2 × T window of input frames. On the quantum that completes a tail block it runs the tail's forward FFT and stores the spectrum in a second delay line. That keeps the rule the code has today: only the audio thread writes input spectra, workers only read them. The delay line keeps spare slots for late workers, as _segments does now.
  • scheduleTails schedules one task per convolver on that quantum only, not on every quantum.
  • computeTail multiplies the tail partitions, runs the inverse FFT and publishes T time-domain frames. The slot scheme stays (state word, busy bit, CAS claim). A slot holds T frames, not a 129-bin spectrum, because the two FFT sizes cannot be summed in the frequency domain.
  • process adds 128 frames of the current tail block to the head's output after the head's inverse FFT. On the first quantum of a block, a slot that is not ready is computed inline, into a buffer the audio thread owns.
  • Memory stays about the same: the tail's spectra take about one complex value per impulse response frame at any block size.

Tests in test/src/dsp/ConvolverTest.cpp:

  • The five existing cases stay (inline tail, short IR, precomputed tail, late tail, pool-scheduled tail, each against direct convolution), but they need changes. Their impulse responses are 2000 to 4000 frames, which would have no tail any more, and their miss counts assume one tail per quantum.
  • New cases for impulse response lengths at the split: 2 × T, 2 × T + 1, and a length that is not a multiple of T.
  • A case that runs more than one tail block, so the block hand-over is covered on both the ready and the late path.

Open questions

  • Block size: 2048 or 1024?
  • The forward FFT of the tail window (one 2 × T-point FFT every T frames) is on the audio thread in this design. I chose that to keep one writer for the delay line. Would you rather have it on the worker?
  • Should the block size be a constant next to HEAD_SEGMENTS, or in core/utils/Constants.h with the pool sizes?

Our numbers

All of these are from our patched 0.13.5. None of them measure 1.0.0 or 1.0.1.

Our patch is the zero-slack form: the head covers the first 2048 frames in 128-frame partitions, the tail runs in 2048-frame blocks, and the tail block is computed in the quantum that completes its input. That was fine in 0.13.5, where the audio thread waited for the pool on every quantum anyway.

Setup: Galaxy S21, release build, 4-channel (true stereo) impulse response. Our five reverb presets are all 4-channel, 55,199 to 181,251 frames (1.15 to 3.78 s) at 48 kHz.

  • Uniform → two-stage: reverb CPU fell by about 2.5x. I quote the ratio only. The absolute values were taken with a script that we later found to read too high. This is CPU for the whole reverb path, so it is not comparable to the multiply-add ratio above: at that time every quantum still went to the pool and waited.
  • Output: we render our delay and reverb cases offline in the app and in Chrome and compare them. Every reverb case passes with the two-stage convolver. The limit is a residual of -60 dB against Chrome's render, so this shows parity with Chrome, not sample equality with the uniform convolver.
  • Hand-off cost, with the two-stage patch, 4 convolver tasks and one wait() on every quantum: 15 tracks playing, simpleperf for 10 s at 1000 Hz. The pool threads used 30% of one core. About half of that was convolution. The other half was kernel wake-ups, sched_yield, clock_gettime and wait polling.
  • Then we moved the 15 quanta in 16 that have no tail block onto the audio thread and kept the pool for the 16th. The same change made wait() wait on the task counter, as fix: remove waiting and redesign the convolver approach #1357 did. Same phone and session, percent of one core, three 10 s runs: whole app 89, 93, 90 → 61, 73, 60. Pool threads plus JS thread 36, 37, 36 → 15, 18, 15. Audio thread 20, 21, 20 → 17, 20, 17. The rendered output was byte-identical before and after.

What we did not measure

  • Anything on 1.0.x. In 0.13.5 the audio thread waited for the pool on every quantum. fix: remove waiting and redesign the convolver approach #1357 removed that wait, so part of the hand-off cost above is already gone upstream. I do not know how much one hand-off per convolver per quantum costs in 1.0.1.
  • The task rates and multiply-add counts in this issue. They are computed from block sizes at 48 kHz.
  • The design above, with one block of slack and a worker computing ahead. We have only built the zero-slack form.
  • The cost of the tail's forward FFT on the audio thread, one quantum in 16.
  • The WPT convolver tests against the two-stage output.
  • Any device other than one Galaxy S21.

Plan

Once the block size and the open questions are settled, I will open a PR against main from my fork, with the gtests above. I will measure main and the PR branch on the same phone with the repo's example app (the ConvolverIR example) and post both sets of numbers in the PR.

Activity

  1. mdydek commented on Nov 17, 2026

    @mdydek
    Member

    Hi, thanks a lot for the detailed description. So, starting from the beginning: 128 frames as block was set as a consequence of constant render quantum size set to 128, in #1364 there are improvements in the Convolver implementation so it can accept any size not only 128. Overall the plan looks solid, however I do not know if all is adding up, that would require more attention and thinking about the changes in the algorithm. As to your questions: first two, I leave the decision to you, but rememeber benchmarks are the key factor here, I would like to see the version that is faster. you can put the constant next to HEAD_SEGMENTS

  2. added
    feature-requestUser-submitted proposals or requests for new features
    on Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature-requestUser-submitted proposals or requests for new features

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions