Skip to content

perf(obs-filters): skip log10f/powf below threshold in compressor, limiter and expander (#3190) - #3229

Merged
zackees merged 3 commits into
masterfrom
perf/3190-dynamics-threshold
Oct 31, 2026
Merged

zackees merged 3 commits into
masterfrom
perf/3190-dynamics-threshold

Conversation

@zackees

@zackees zackees commented Oct 31, 2026

Copy link
Copy Markdown
Member

What

The compressor, limiter and expander called log10f and powf on every sample, even when the result was exactly 1.

  • Compressor and limiter: below the threshold, fminf(0, slope * (threshold - env_db)) is 0, so the gain is db_to_mul(0) = 1. Each call now computes db_to_mul(threshold) * 0.9999f once and skips both libm calls for envelopes under it. The 1e-4 margin (about 0.0009 dB) is far larger than log10f's rounding error, so an envelope near the threshold still takes the exact path.
  • Expander (downward): the output stage skips powf when gain_db >= 0 (including NaN), because db_to_mul(fminf(0, x)) is exactly 1 there.

Output is bit-identical. No public header, C test or audio-math.h changed.

Commits

  1. 692a273 is a characterization test, test/cmocka/test_dynamics_threshold.c. It compares all three gain stages bit for bit with the original formula: 1,507,328 checks across thresholds from -60 to 0 dB, ratios from 1 to 32, envelopes from -140 to +6 dB, a dense sweep within ±0.01 dB of the threshold, the exact threshold and its float neighbours, and silence. It passes on master, as a characterization test should (there is no bug to show RED).
  2. 43c0290 is the change. The same test passes: checks=1507328 failures=0, before and after.

Measurements

Per audio tick (1024 frames, stereo). Median of 7 runs × 20,000 iterations, gcc 15.2 -O2, pinned to one core of a Ryzen 7 3700X, two alternating old/new runs. The harness is a standalone C file that includes the three filter sources, so it times exactly the shipped code. It is not committed, because there is no C filter bench in record.py.

envelope compressor limiter expander
-40 dBFS (below threshold) 9.9 → 1.17 µs (8.4x) 9.5 → 1.20 µs (7.9x) 27.8 → 15.2 µs (1.8x)
-20 dBFS 9.7 → 1.16 µs (8.3x) 9.4 → 1.15 µs (8.2x) 28.2 → 13.9 µs (2.0x)
-3 dBFS (above threshold) 14.6 → 14.5–15.3 µs (unchanged) 14.4 → 14.4–15.2 µs (unchanged) 27.3 → 14.1 µs (1.9x)
mixed -60…0 dB 12.7 → 5.6 µs (2.3x) 10.8 → 2.7 µs (4.0x) 42.2 → 35.3 µs (1.2x)

Most audio sits under a compressor's or limiter's threshold most of the time, so the common case is the 8x row.

Tiers

  • Tier 1 (Rust unit): not applicable, since these filters are not ported.
  • Tier 2: the new cmocka test runs against hybrid libobs. No existing C test changed.
  • Tier 3: not applicable, since there is no Rust counterpart and the C output is unchanged.
  • Tier 4: not applicable.

Test cost: one C test, about 1.5 M float comparisons, well under 1 s. It starts no threads.

Fixes #3190. Refs #3176.

@zackees
zackees enabled auto-merge October 31, 2026 18:08
@zackees

zackees commented Oct 31, 2026

Copy link
Copy Markdown
Member Author

Handed off to the coordinator. Lint passes locally and the new test passes on both the old and new code (I built it outside CMake, with stubs for libobs). I didn't run the local-gate stamp, so PR CI runs Tier 2.

@zackees
zackees force-pushed the perf/3190-dynamics-threshold branch from 1afdd97 to a9cb538 Compare October 31, 2026 18:19

@zackees zackees left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coordinator review: No blocking findings.

  • The skip is output-only. In the compressor and limiter, process_compression / limiter_process_compression (compressor-filter.c.h:350, limiter-filter.c.h:149) only scale samples. The envelope is computed earlier in analyze_envelope/analyze_sidechain and is not touched. For envelopes under db_to_mul(threshold)*0.9999, slope*(threshold-env_db) is positive, so the old path gave exactly db_to_mul(0)=1. NaN and 0 envelopes still take the exact path (!(x < t)), or give 1 as before.
  • Expander (expander-filter.c.h:362): gain_db[idx] (the attack/release state) is still written before the changed line. Only the output multiplier is short-circuited. x < 0 ? db_to_mul(x) : 1 equals db_to_mul(fminf(0,x)), including NaN. The upward-compressor branch is unchanged.
  • test/cmocka/test_dynamics_threshold.c compares the real stages bit for bit against the original formula, including the threshold neighbours and the margin edge. The test is new and edits no existing C test. Characterization instead of RED is fine for a perf change that preserves behaviour.

@zackees zackees left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coordinator follow-up: #3190 now accepts epsilon differences (relative error ≤ 1e-6, see the Decided: comment there). That allows a faster design than the skip. The skip only helps below the threshold, and in a microbenchmark it was no faster at -6 dBFS (15.4 vs 14.6 ns/sample).

Suggested changes:

  • limiter-filter.c.h: gain = e > t ? t / e : 1.0f, with t = db_to_mul(cd->threshold) stored in limiter_update. Measured about 15× faster while limiting.
  • compressor-filter.c.h: gain = e > t ? powf(t / e, cd->slope) : 1.0f. Measured about 2× faster while compressing, max relative error 2.5e-7.
  • expander-filter.c.h: keep your skip, and replace log10f/powf with log2f/exp2f and the constant 6.0206. The dB-domain smoothing stays.
  • Drop the 0.9999f margin and compare with an exact e > t.
  • In test_dynamics_threshold.c, change the comparison from bit-for-bit to a relative tolerance of 1e-6. Keep the 0 and NaN envelope cases: both still give gain 1.

The issue still requires port_comparison before/after numbers at -40, -20 and mixed levels in the PR body.

@zackees
zackees added this pull request to the merge queue Oct 31, 2026
Merged via the queue into master with commit 1fddc3e Oct 31, 2026
41 checks passed
@zackees
zackees deleted the perf/3190-dynamics-threshold branch October 31, 2026 19:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

perf: obs-filters: skip log10f/powf below threshold in compressor, limiter and expander

1 participant