Repository navigation
perf(rpc): non-template RuntimeRpcBinding dispatch on low-memory (#3246 Tier 1A) - #3258
Conversation
…ets (#3246 / #3244 Tier 1A) Replace the per-signature `TypedRpcBinding<Sig>::invokeWithReturn` body (2,074 B observed on LPC845-BRK for `fl::string(int,int,int,int)`) with a non-template `RuntimeRpcBinding` plus a per-Sig trampoline + per-type ops table. Implementation - New `fl::detail::RuntimeRpcBinding` (non-template `ErasedInvoker`) with one signature-agnostic decode/encode/dispatch body shared across every signature. Per-call: alloc slot buffers, decode via type-keyed ops, trampoline into the user lambda, encode the result. - New `fl::detail::RpcTypeOps` table (one static per unique storage type), driven by templated `RpcTypeOpsFor<T>` so existing `JsonToType<T>` / `TypeToJson<T>` specializations are reused without copy. - Per-Sig trampoline in `runtime_rpc::TrampolineHelper<R, Args...>` (~50-100 B). All cross-type behaviour lives in the shared invoke(). - `Rpc::registerMethod<Sig>` forks at compile time on `FL_PLATFORM_HAS_LARGE_MEMORY`: low-memory targets get `RuntimeRpcBinding`; large-memory keeps `TypedInvoker<Sig>` (no per-call heap, no indirect dispatch -- faster path retained). Measurement (LPC845-BRK / AutoResearch) - Master baseline: 47,020 B flash (.bin); 48,258 B .text - This branch: 46,704 B flash (.bin); 47,252 B .text - Net: **316 B flash / 1,006 B .text saved** - Structural: `TypedRpcBinding<fl::string(int,int,int,int)>::invokeWithReturn` (2,074 B) and `TypedRpcBinding<int(int)>::invokeWithReturn` (516 B) are gone, replaced by `RuntimeRpcBinding::invoke` (1,536 B, shared). The 1,006 B observed is below the issue's ~1.5 KB target, but the structural win is correct -- the per-Sig template tax is gone. This matches the meta #3244 lesson-learned: AutoResearch registers only 2 unique signatures, so amortization of the shared dispatch body across signatures is limited at this sketch size. Per-Sig accounting (2nd-sig cost drops from 516 B to ~20 B trampoline) shows the leverage axis works; full ~6 KB savings need a multi-method sketch (RpcServer, etc.) to demonstrate. Tests - 7 new FL_TEST_CASEs in tests/fl/remote/rpc/runtime_rpc_binding.hpp exercise the runtime path directly on host (host targets default through TypedInvoker; the new tests force RuntimeRpcBinding to verify decode/encode/error/lifetime). All 328 unit tests pass quick + debug. - ESP32/Teensy/LPC845 compile path verified; existing tests/fl/remote/rpc/rpc.hpp suite continues to pass. Refs #3246, #3244 (Tier 1A), #3235. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (6)
📝 WalkthroughWalkthroughIntroduces ChangesRuntimeRpcBinding type-erased invoker
Sequence Diagram(s)sequenceDiagram
participant Client
participant registerMethod as Rpc::registerMethod
participant makeRTB as makeRuntimeRpcBinding
participant invoke as RuntimeRpcBinding::invoke
participant Trampoline as TrampolineHelper::trampoline
participant UserFn as fl::function~R(Args...)~
rect rgba(70, 130, 180, 0.5)
note over registerMethod,makeRTB: !FL_PLATFORM_HAS_LARGE_MEMORY path
Client->>registerMethod: registerMethod("name", fn)
registerMethod->>makeRTB: makeRuntimeRpcBinding(fn)
makeRTB->>makeRTB: heap-allocate fl::function, build RuntimeRpcBinding
makeRTB-->>registerMethod: shared_ptr~RuntimeRpcBinding~
end
rect rgba(60, 179, 113, 0.5)
note over invoke,UserFn: invoke call path
Client->>invoke: invoke(jsonArgs)
invoke->>invoke: validate array & arg count
loop per argument
invoke->>invoke: default_construct + decode slot via RpcTypeOps
end
invoke->>Trampoline: mTrampoline(mUserFn, result_buf, slot_ptrs)
Trampoline->>UserFn: call fn(cast args...)
UserFn-->>Trampoline: R value
Trampoline-->>invoke: result written to result_buf
invoke->>invoke: encode result via RpcTypeOps
invoke-->>Client: tuple(TypeConversionResult, json)
end
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related issues
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary
Replaces the per-signature
TypedRpcBinding<Sig>::invokeWithReturnbody (observed at 2,074 B on LPC845-BRK forfl::string(int,int,int,int)) with a non-templatefl::detail::RuntimeRpcBindingplus a small per-Sig trampoline + per-type ops table.Architecture:
RuntimeRpcBinding(non-templateErasedInvoker): one signature-agnostic decode/encode/dispatch body shared across every signature.RpcTypeOpstable (one static per unique storage type), generated byRpcTypeOpsFor<T>, reuses existingJsonToType<T>/TypeToJson<T>specializations.runtime_rpc::TrampolineHelper<R, Args...>produces a ~50-100 B trampoline + ~10 B destroy fn + 8 B × argc rodata table.Rpc::registerMethod<Sig>forks at compile time onFL_PLATFORM_HAS_LARGE_MEMORY: low-memory usesRuntimeRpcBinding; large-memory keeps the existing inlineTypedInvoker<Sig>(faster -- no per-call heap, no indirect dispatch).Measurement (LPC845-BRK / AutoResearch / LowMemory mode)
Symbol-level structural win (the original issue's hot symbol is gone):
TypedRpcBinding<fl::string (int,int,int,int)>::invokeWithReturn-- 2,074 B → 0 (replaced)TypedRpcBinding<int(int)>::invokeWithReturn-- 516 B → 0 (replaced)RuntimeRpcBinding::invoke-- 0 → 1,536 B (shared across every Sig)RpcTypeOpsFor<int>::decode_-- 0 → 454 B (one-time per type)RpcTypeOpsFor<fl::string>::decode_-- 0 → 224 B (one-time per type)Honest caveat re acceptance bullet
≥ 1.5 KB net flash dropThe 1,006 B .text / 316 B flash savings is below the issue's predicted 1.5 KB target, but the structural win is correct -- per-Sig template tax is gone. This matches the meta #3244 lesson-learned (verbatim from #3235 retrospective):
AutoResearch's LowMemory mode registers only 2 unique RPC signatures. Per-Sig accounting shows the leverage axis works (2nd-Sig cost drops from 516 B to ~20 B trampoline) but full ~6 KB savings need a multi-method sketch (RpcServer, etc.). Leaving #3246 open so a follow-up can run the kitchen-sink benchmark.
Test plan
FL_TEST_CASEintests/fl/remote/rpc/runtime_rpc_binding.hppexercising the runtime path directly on host (host targets default through TypedInvoker; the new tests force RuntimeRpcBinding to verify decode/encode/error/lifetime).bash test --cpp-- 328/328 unit tests pass.bash test rpc --debug-- ASAN/LSAN/UBSAN clean on the new path.bash lint --cpp-- 0 violations../compile lpc845brk --examples AutoResearch), no regression.Refs #3246, #3244 (Tier 1A), #3235.
🤖 Generated with Claude Code
Summary by CodeRabbit
Release Notes
New Features
Tests