Skip to content

Decide on pool-allocator vs typed-array backing for fpc_pid and fpc_biquad #2

Closed as not planned

Description

@aajll
Type Design
Estimate Decision: ~1h. Implementation if option 2 chosen: ~0.5d.
Depends on —
Unblocks Any deployment constrained by SRAM on a small TI C2000 part.
Design doc —

Background

fpc currently allocates instances of all three components — fpc_pid, fpc_fir, fpc_biquad — from three separate pool-allocator pools (one per component type). All three pools share a single compile-time POOL_ITEM_SIZE macro, sized for the largest struct (fpc_fir).

Measured sizeof() per component (default FPC_FILTER_MAX_ORDER=64):

Component sizeof (host) sizeof (C2000, MAU / phys B) Pool slot Per-slot waste
fpc_pid 64 B 32 MAU / 64 B 528 B / 528 MAU (1056 B-phys) 88% host / 94% C2000
fpc_fir (order=64) 520 B 262 MAU / 524 B 528 B / 528 MAU (1056 B-phys) 1.5% host / 50% C2000
fpc_biquad 40 B 22 MAU / 44 B 528 B / 528 MAU (1056 B-phys) 92% host / 96% C2000

Net static-RAM cost at default config (FPC_MAX_INSTANCES=8, three pools):

Target Allocated Used Wasted
Host (byte-addressable) 12.4 KB ~5 KB ~7.4 KB (58%)
TI C2000 (physical) 24.8 KB ~5 KB ~19.8 KB (80%)

The waste is structural: a single POOL_ITEM_SIZE cannot right-size three differently-sized payloads.

The pool allocator's POOL_ITEM_SIZE is a compile-time macro consumed by pool.c's storage pool_byte_t bytes[POOL_MAX_SLOTS * POOL_ITEM_SIZE]. Per-pool-instance slot sizing is not currently supported and would be a non-trivial refactor to pool-allocator (would have to move from static byte buffer to runtime-sized storage with _Static_assert losing its authority).

Goal

Decide which of three approaches fpc will take, and capture the decision (with rationale) in CONTRIBUTING.md or the design rationale section of README.md. The decision matters most for downstream consumers planning to deploy fpc on small TI C2000 parts (e.g. F28004x with 32 KB SRAM, where current defaults consume ~75% of available RAM in pool storage alone).

Option 1 — Accept the waste

No code changes. Document the per-component sizing and the per-target footprint in README.md so deployers can compute their actual RAM cost.

Right when: target devices have sufficient SRAM; engineering time is the constraint; consumers can lower fpc_max_instances or fpc_filter_max_order if they hit the ceiling.

Option 2 — Typed static arrays for small components

Replace fpc_pid_pool_storage and fpc_biquad_pool_storage with typed static arrays plus a per-component status array. Keep fpc_fir on the pool (only 1.5% waste on host, 50% on C2000 — but still significant in absolute terms).

Shape per component:

static struct fpc_pid fpc_pid_instances[FPC_MAX_INSTANCES];
static POOL_ATOMIC(uint_least8_t) fpc_pid_status[FPC_MAX_INSTANCES];

static struct fpc_pid *fpc_pid_acquire_slot(void) { /* linear scan */ }
static void            fpc_pid_release_slot(struct fpc_pid *ctx) {
    /* secure-clear payload BEFORE flipping status (mirrors pool_release) */
}

Preserves the single-writer/many-readers contract via the POOL_ATOMIC macro from pool_platform.h. Drops the pool_id field from struct fpc_pid / struct fpc_biquad (pointer arithmetic gives the index back). Public API of fpc is unchanged — callers still receive struct fpc_* pointers.

Reclaims ~7.3 KB on host / ~15.4 KB physical on C2000 (~60% of current footprint).

Cost: ~60 lines of code added across pid_controller.c and filters.c; two new code paths to validate (acquire/release of the typed arrays); one new TSAN-gated concurrency test mirroring pool's test_pool_concurrency.c.

Option 3 — Refactor pool-allocator for per-instance slot sizing

Make pool_item_size a runtime field on struct pool_t rather than a compile-time macro. Storage becomes externally-supplied (pool_init_from_buffer(pool, buf, n_slots, slot_size)), and the _Static_assert mechanism is replaced with a runtime check in pool_init.

Reclaims the same memory as option 2.

Cost: substantial. Pool's safety story (deterministic WCET, single-static-assert sizing, no runtime configuration drift) weakens. Would need a major-version bump on pool-allocator and a re-run of MISRA review on the runtime-sizing path. Affects downstream pool consumers beyond fpc.

Recommendation

Option 2. The pool was designed to amortise allocation overhead and centralise status/ISR-safety semantics for medium-sized blocks; the PID and biquad slots are small enough that an inline static-array allocator gives the same API surface (and the same single-writer/many-readers contract) without the slot bloat. It localises the change to fpc, leaves pool-allocator unchanged, and is reversible — if a future component grows large enough to benefit from pool-allocator's machinery, swap it back.

Acceptance criteria

  • Decision recorded in CONTRIBUTING.md (a "Backing storage" subsection under the existing pool/concurrency discussion).
  • If option 1: a per-component sizing + per-target footprint table added to README.md under a new "Memory footprint" section.
  • If option 2: src/pid_controller.c and src/filters.c updated; struct fpc_pid and struct fpc_biquad lose their pool_id field; new tests/test_fpc_concurrency.c (TSAN-gated) exercises the typed-array path; CHANGELOG.md notes the struct layout change.
  • If option 3: filed as a separate ticket against the pool-allocator repo; this ticket is closed as superseded.

Out of scope

  • Changes to pool-allocator (unless option 3 is chosen).
  • Changes to fpc_fir backing — separately addressable if the 1.5%/50% waste matters.
  • Reducing default FPC_MAX_INSTANCES or FPC_FILTER_MAX_ORDER to mitigate footprint. Both knobs already exist and can be tuned per-deployment.

References

  • src/pid_controller.c (current fpc_pid_pool_storage)
  • src/filters.c (current fpc_fir_pool_storage + fpc_biquad_pool_storage)
  • meson.build (derived fpc_pool_item_size from fpc_filter_max_order)
  • pool-allocator include/pool.h (POOL_ITEM_SIZE is compile-time)

Activity

  1. added
    enhancement
    on May 28, 2026
  2. aajll commented on Oct 14, 2026

    @aajll
    OwnerAuthor

    Superseded by #5: PID, FIR and biquad instances are now caller-owned structs (#6 to #9), so neither pool-allocator nor typed-array backing is used and the dependency is gone.

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

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions