`make -C ios bench` renders four channels of synth with 0/1/2/4 effects each, times every effect on its own, and compares a ten-biquad cascade three ways. It asserts nothing, so it stays out of core-test; no shipped code changes (the file is under core/tests, which neither app compiles). What it says, on an M4 at -O2. Per-sample virtual dispatch — listed in the handover as a thing to fix — costs 0.11 ns of 9.13 ns per sample, about 1%: the indirect branch is predicted and amortises over the effect's own work. The obvious block restructuring is a 162% regression, because running band-at-a-time over a block sends the running value through memory ten times per sample instead of keeping it in a register across the cascade. So that idea is recorded as measured and closed, rather than left as an invitation. Where the cost actually is: Ambience, 52% of all eight effects together (65.9 ns/sample, 3.5x Tape Delay). A full chain is ~77% of render time, and three-quarters of that is Ambience. Not urgent — four channels of four effects still render at 42x real time. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
278 lines
11 KiB
C++
278 lines
11 KiB
C++
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
// What the insert-FX chain costs. Nothing here asserts; it prints numbers, so a
|
|
// change to the DSP can be argued about with measurements instead of intuition.
|
|
// Three parts:
|
|
//
|
|
// A a whole engine render (four channels of synth, a note on every line) with
|
|
// 0, 1, 2 and 4 effects per channel: the share of fillBlock the chains take.
|
|
// B each effect on its own: ns per sample, and what one instance costs as a
|
|
// share of one channel's real-time budget.
|
|
// C the block-rate question from DEVELOPER_HANDOVER.md §9, on a stand-in with
|
|
// EQ10's shape (ten biquads in series), run three ways: per sample through a
|
|
// virtual call (what the engine does today), per sample through the concrete
|
|
// type (the same work with dispatch removed — the difference is what block
|
|
// dispatch could save), and band-at-a-time over a block with the coefficients
|
|
// hoisted. All three do the same arithmetic in the same order per band.
|
|
//
|
|
// bench [seconds] (default 4; part A renders that much audio per variant)
|
|
|
|
#include <chrono>
|
|
#include <cmath>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <memory>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "echofield/GrainSources.hpp"
|
|
#include "sampler/SampleStore.hpp"
|
|
#include "sizzle/Effect.hpp"
|
|
#include "sizzle/Engine.hpp"
|
|
#include "sizzle/Model.hpp"
|
|
#include "wavetable/WaveTables.hpp"
|
|
|
|
using namespace sizzle;
|
|
|
|
namespace {
|
|
|
|
constexpr int SR = 48000;
|
|
constexpr int BLOCK = engine::Engine::BLOCK_FRAMES;
|
|
|
|
double now() {
|
|
using namespace std::chrono;
|
|
return duration<double>(steady_clock::now().time_since_epoch()).count();
|
|
}
|
|
|
|
/** White-ish noise, the same every run so two runs are comparable. */
|
|
std::vector<float> noise(size_t n) {
|
|
std::vector<float> v(n);
|
|
uint32_t seed = 0x1234567u;
|
|
for (float &x : v) {
|
|
seed = seed * 1103515245u + 12345u;
|
|
x = (float)((seed >> 8) & 0xFFFF) / 65535.f - 0.5f;
|
|
}
|
|
return v;
|
|
}
|
|
|
|
// ---------------------------------------------------------------- part A
|
|
|
|
/** Four channels of NES_SYNTH, a note on every line of the active pattern, and
|
|
* [nfx] shared effect slots on each channel. Slots 0..3 are the instruments and
|
|
* 4..7 the effects, so four channels of four effects still fit in sixteen. */
|
|
void buildProject(model::Project &p, int nfx) {
|
|
static const char *kFx[4] = {"FILTER", "EQ10", "DELAY", "AMBIENCE"};
|
|
for (int i = 0; i < 4; ++i) {
|
|
p.toolbox[i].fill("NES_SYNTH");
|
|
p.toolbox[4 + i].fill(kFx[i]);
|
|
}
|
|
for (int ch = 0; ch < 4; ++ch) {
|
|
auto &mc = p.mixer.channels[ch];
|
|
mc.midiChannel = ch + 1;
|
|
mc.instrumentSlot = ch;
|
|
for (int i = 0; i < model::Mixer::FX_SLOTS; ++i) mc.fxSlots[i] = i < nfx ? 4 + i : -1;
|
|
}
|
|
model::Pattern &pat = p.activePattern();
|
|
for (int t = 0; t < model::Pattern::TRACK_COUNT; ++t)
|
|
for (int line = 0; line < pat.length; ++line) {
|
|
model::Cell &c = pat.cell(t, line);
|
|
c.note = 48 + t * 5 + line % 5;
|
|
c.velocity = 100;
|
|
c.channel = t + 1;
|
|
}
|
|
}
|
|
|
|
/** Seconds of wall time to render [seconds] of audio with [nfx] effects per channel. */
|
|
double renderCost(int nfx, double seconds) {
|
|
sampler::SampleStore store;
|
|
model::Project project;
|
|
buildProject(project, nfx);
|
|
|
|
engine::Engine eng(SR, &store, engine::EngineGlobals{});
|
|
eng.setProject(&project);
|
|
eng.play();
|
|
|
|
std::vector<float> buf((size_t)BLOCK * 2);
|
|
const int warmup = (int)(0.5 * SR) / BLOCK;
|
|
for (int b = 0; b < warmup; ++b) eng.fillBlock(buf.data(), BLOCK);
|
|
|
|
const int blocks = (int)(seconds * SR) / BLOCK;
|
|
const double t0 = now();
|
|
for (int b = 0; b < blocks; ++b) eng.fillBlock(buf.data(), BLOCK);
|
|
return now() - t0;
|
|
}
|
|
|
|
void partA(double seconds) {
|
|
std::printf("\nA. engine render, 4 channels of synth, %.0f s of %d Hz audio per variant\n", seconds, SR);
|
|
std::printf(" %-22s %9s %9s %12s\n", "effects per channel", "wall s", "x real", "vs 0 fx");
|
|
double base = 0;
|
|
for (int nfx : {0, 1, 2, 4}) {
|
|
const double t = renderCost(nfx, seconds);
|
|
if (nfx == 0) base = t;
|
|
std::printf(" %-22d %9.3f %8.0fx %11s\n", nfx, t, seconds / t,
|
|
nfx == 0 ? "-" : (std::to_string((long long)std::lround((t / base - 1) * 100)) + "%").c_str());
|
|
}
|
|
std::printf(" (the chain's share at 4 fx is 1 - base/total)\n");
|
|
}
|
|
|
|
// ---------------------------------------------------------------- part B
|
|
|
|
void partB(double seconds) {
|
|
static const char *kTypes[] = {"BITCRUSHER", "FILTER", "EQ10", "DELAY",
|
|
"MIX_TIGHTENER", "TAPE_ENGINE", "GRANULAR", "AMBIENCE"};
|
|
const size_t n = (size_t)(seconds * SR);
|
|
const std::vector<float> in = noise(n);
|
|
|
|
double ns[8] = {0};
|
|
double total = 0;
|
|
for (size_t k = 0; k < sizeof(kTypes) / sizeof(*kTypes); ++k) {
|
|
model::ToolboxSlot slot;
|
|
slot.fill(kTypes[k]);
|
|
std::unique_ptr<Effect> fx = Effect::create(kTypes[k], SR);
|
|
if (!fx) continue;
|
|
fx->update(slot.values, 120.f);
|
|
|
|
for (size_t i = 0; i < n / 8; ++i) fx->process(in[i]); // warm the delay lines
|
|
double sink = 0;
|
|
const double t0 = now();
|
|
for (size_t i = 0; i < n; ++i) sink += fx->process(in[i]);
|
|
ns[k] = (now() - t0) * 1e9 / (double)n;
|
|
if (!std::isfinite(sink)) std::printf(" note: %s went non-finite\n", kTypes[k]);
|
|
total += ns[k];
|
|
}
|
|
|
|
std::printf("\nB. one effect instance, %.0f s of samples each\n", seconds);
|
|
std::printf(" %-16s %12s %16s\n", "effect", "ns/sample", "share of all 8");
|
|
for (size_t k = 0; k < sizeof(kTypes) / sizeof(*kTypes); ++k) {
|
|
if (ns[k] == 0) { std::printf(" %-16s %12s\n", kTypes[k], "no processor"); continue; }
|
|
std::printf(" %-16s %12.2f %15.0f%%\n", kTypes[k], ns[k], ns[k] / total * 100);
|
|
}
|
|
std::printf(" %-16s %12.2f\n", "all eight", total);
|
|
}
|
|
|
|
// ---------------------------------------------------------------- part C
|
|
|
|
/** EQ10's PeakBiquad, copied so the bench can hold it by value and reach inside. */
|
|
struct Biquad {
|
|
double b0 = 1, b1 = 0, b2 = 0, a1 = 0, a2 = 0;
|
|
double z1 = 0, z2 = 0;
|
|
|
|
void set(int sr, float centerHz, float gainDb) {
|
|
const double a = std::pow(10.0, gainDb / 40.0);
|
|
const double w0 = 2.0 * M_PI * centerHz / sr;
|
|
const double cosW = std::cos(w0);
|
|
const double alpha = std::sin(w0) / 2.0;
|
|
const double a0 = 1 + alpha / a;
|
|
b0 = (1 + alpha * a) / a0;
|
|
b1 = (-2 * cosW) / a0;
|
|
b2 = (1 - alpha * a) / a0;
|
|
a1 = (-2 * cosW) / a0;
|
|
a2 = (1 - alpha / a) / a0;
|
|
}
|
|
|
|
float process(float x) {
|
|
const double input = (double)x;
|
|
const double out = b0 * input + z1;
|
|
z1 = b1 * input - a1 * out + z2;
|
|
z2 = b2 * input - a2 * out;
|
|
return (float)out;
|
|
}
|
|
};
|
|
|
|
struct Cascade {
|
|
Biquad bands[10];
|
|
Cascade() {
|
|
static const float centers[10] = {31.f, 63.f, 125.f, 250.f, 500.f, 1000.f, 2000.f, 4000.f, 8000.f, 16000.f};
|
|
for (int i = 0; i < 10; ++i) bands[i].set(SR, centers[i], (i % 3) * 3.f - 3.f);
|
|
}
|
|
void reset() { for (Biquad &b : bands) { b.z1 = 0; b.z2 = 0; } }
|
|
|
|
/** Band-at-a-time over a whole block: each band is time-invariant, so this is
|
|
* the same arithmetic in the same order per band — only the coefficient loads
|
|
* are hoisted out of the sample loop. This is what an override would do. */
|
|
void processBlockHoisted(const float *in, float *out, int n) {
|
|
for (int i = 0; i < n; ++i) out[i] = in[i];
|
|
for (Biquad &band : bands) {
|
|
const double b0 = band.b0, b1 = band.b1, b2 = band.b2, a1 = band.a1, a2 = band.a2;
|
|
double z1 = band.z1, z2 = band.z2;
|
|
for (int i = 0; i < n; ++i) {
|
|
const double input = (double)out[i];
|
|
const double y = b0 * input + z1;
|
|
z1 = b1 * input - a1 * y + z2;
|
|
z2 = b2 * input - a2 * y;
|
|
out[i] = (float)y;
|
|
}
|
|
band.z1 = z1;
|
|
band.z2 = z2;
|
|
}
|
|
}
|
|
};
|
|
|
|
/** The engine's shape today: a virtual call per sample through an Effect*. */
|
|
struct VirtualCascade : Effect {
|
|
Cascade c;
|
|
void update(const Params &, float) override {}
|
|
float process(float x) override {
|
|
float y = x;
|
|
for (Biquad &b : c.bands) y = b.process(y);
|
|
return y;
|
|
}
|
|
};
|
|
|
|
void partC(double seconds) {
|
|
const size_t n = (size_t)(seconds * SR) / BLOCK * BLOCK;
|
|
const std::vector<float> in = noise(n);
|
|
std::vector<float> outA(n), outB(n);
|
|
|
|
std::vector<float> outC(n);
|
|
|
|
VirtualCascade vc;
|
|
Effect *fx = &vc; // through the base class, as the engine does
|
|
double t0 = now();
|
|
for (size_t i = 0; i < n; ++i) outA[i] = fx->process(in[i]);
|
|
const double viaVirtual = now() - t0;
|
|
|
|
VirtualCascade dc; // same code, called on the concrete type
|
|
dc.c.reset();
|
|
t0 = now();
|
|
for (size_t i = 0; i < n; ++i) outB[i] = dc.VirtualCascade::process(in[i]);
|
|
const double viaDirect = now() - t0;
|
|
|
|
Cascade block;
|
|
t0 = now();
|
|
for (size_t i = 0; i < n; i += BLOCK) block.processBlockHoisted(in.data() + i, outC.data() + i, BLOCK);
|
|
const double blocked = now() - t0;
|
|
|
|
double maxDiff = 0;
|
|
for (size_t i = 0; i < n; ++i) {
|
|
maxDiff = std::fmax(maxDiff, std::fabs((double)outA[i] - (double)outB[i]));
|
|
maxDiff = std::fmax(maxDiff, std::fabs((double)outA[i] - (double)outC[i]));
|
|
}
|
|
|
|
const double perNs = viaVirtual * 1e9 / (double)n;
|
|
std::printf("\nC. ten-biquad cascade (EQ10's shape), %.0f s of samples\n", seconds);
|
|
std::printf(" %-32s %8.2f ns/sample\n", "per sample, virtual (today)", perNs);
|
|
std::printf(" %-32s %8.2f ns/sample %+.0f%%\n", "per sample, direct call",
|
|
viaDirect * 1e9 / (double)n, (viaDirect / viaVirtual - 1) * 100);
|
|
std::printf(" %-32s %8.2f ns/sample %+.0f%%\n", "block, coefficients hoisted",
|
|
blocked * 1e9 / (double)n, (blocked / viaVirtual - 1) * 100);
|
|
std::printf(" dispatch is %.2f ns of the %.2f, max diff between the three %.3g\n",
|
|
(viaVirtual - viaDirect) * 1e9 / (double)n, perNs, maxDiff);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main(int argc, char **argv) {
|
|
const double seconds = argc > 1 ? std::strtod(argv[1], nullptr) : 4.0;
|
|
wavetable::WaveTables::prewarmBlocking();
|
|
echofield::GrainSources::prewarmBlocking();
|
|
|
|
std::printf("sizzle bench: %d Hz, block %d, %.0f s per measurement\n", SR, BLOCK, seconds);
|
|
partA(seconds);
|
|
partB(seconds);
|
|
partC(seconds);
|
|
std::printf("\n");
|
|
return 0;
|
|
}
|