Skip to content

perf(json): JsonToType<T> dispatch through non-template cores (-2 KB Teensy 4; closes #3247) - #3253

Merged
zackees merged 1 commit into
masterfrom
feat/json-visit-dispatch-phase2-3247
Jun 19, 2026
Merged

zackees merged 1 commit into
masterfrom
feat/json-visit-dispatch-phase2-3247

Conversation

@zackees

@zackees zackees commented Jun 19, 2026 •

Copy link
Copy Markdown
Member

Closes #3247 (#3244 Tier 2E Phase 2).

Introduces non-template json_convert_to__core helpers, routes JsonToType::convert through them.

Verification

Target Before After Δ
LPC845 / AutoResearch 47,128 B 47,020 B -108 B
Teensy 4 / AutoResearch 348,160 B 346,112 B -2,048 B

LPC845 win is modest because #3226 already gated most visitor paths to setError bodies on Low-memory; the Teensy 4 win matches the audit's predicted ~2 KB.

Refs #3244, #3247.

Summary by CodeRabbit

  • Chores
    • Optimized JSON-to-primitive type conversion infrastructure by centralizing conversion logic and improving error handling for integer, boolean, floating-point, and string conversions from JSON data.

… cores (closes #3247)

#3247 / #3244 Tier 2E Phase 2.

Introduces `fl/remote/rpc/json_dispatch.{h,cpp.hpp}` with four shared
non-template helpers:
  - `json_convert_to_i64_core(val, out_i64)`
  - `json_convert_to_bool_core(val, out_bool)`
  - `json_convert_to_float_core(val, out_float)`
  - `json_convert_to_string_core(val, out_string)`

Each replaces the per-(T × Visitor) `visit_fn` thunk explosion produced
when `JsonToIntegerVisitor<T>` / `JsonToFloatVisitor<T>` /
`JsonToBoolVisitor` / `JsonToStringVisitor` invoke `data.visit(visitor)`.

The thin templated `JsonToType<T>::convert` wrappers in `json_to_type.h`
now call the appropriate core (which does the 10-way alternative
switch, the warning text formatting, and the conversion to the
canonical i64/bool/float/string output) and then apply per-T
narrowing + overflow detection.

`FL_NO_INLINE` on the cores is load-bearing under LTO -- without it
the compiler folds the body back at each call site, defeating dedup.

## Verification

LPC845-BRK / AutoResearch (Low-memory):
    flash 47,128 B -> 47,020 B  (-108 B)

Teensy 4.0 / AutoResearch (Large-memory):
    flash 348,160 B -> 346,112 B  (-2,048 B)

The Low-memory win is modest because PR #3226 (#3224 Tier 1A) already
gated most float / per-T visitor paths to setError() bodies; the linker
had already DCE'd most of what this refactor would have shared. The
Large-memory win matches the audit's predicted ~2 KB.

## Compatibility

The visitor structs in `json_visitors.h` remain intact (not removed)
for backward source compat. No public API change; behavior preserved
(same warning text format, same error wording). Existing host tests in
`tests/test_json*.cpp` and `tests/fl/remote/rpc.cpp` pass unchanged.

Refs #3244, #3247
@coderabbitai

coderabbitai Bot commented Jun 19, 2026 •

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: cee1b193-0b4b-4555-93a8-6e29c60c159f

📥 Commits

Reviewing files that changed from the base of the PR and between 851c7e7 and 5bf3c4b.

📒 Files selected for processing (4)
  • src/fl/remote/rpc/_build.cpp.hpp
  • src/fl/remote/rpc/json_dispatch.cpp.hpp
  • src/fl/remote/rpc/json_dispatch.h
  • src/fl/remote/rpc/json_to_type.h

📝 Walkthrough

Walkthrough

Introduces four FL_NO_INLINE non-template core functions (json_convert_to_i64_core, json_convert_to_bool_core, json_convert_to_float_core, json_convert_to_string_core) in fl::detail that switch on the JSON value's runtime tag. The existing JsonToType template specializations are refactored to delegate to these cores, performing per-T narrowing after the core returns. The unity build file is updated to compile the new implementation.

Changes

Non-template JSON dispatch layer

Layer / File(s) Summary
json_dispatch.h: core function declarations
src/fl/remote/rpc/json_dispatch.h
New header with extensive documentation and four FL_NO_INLINE fl::detail function declarations: json_convert_to_i64_core, json_convert_to_bool_core, json_convert_to_float_core, and json_convert_to_string_core, each taking const json_value& and a typed output pointer and returning TypeConversionResult.
json_dispatch.cpp.hpp: core conversion implementations
src/fl/remote/rpc/json_dispatch.cpp.hpp
Implements all four cores with per-alternative handling. The i64 core handles bool, float (platform-gated), and strtol-parsed strings with warnings. The bool core accepts a fixed string vocabulary (true/false, 1/0, yes/no). The float core uses strtod for strings with platform guards. The string core formats all primitives and null with warnings, and errors on arrays/objects.
JsonToType refactor + unity build wiring
src/fl/remote/rpc/json_to_type.h, src/fl/remote/rpc/_build.cpp.hpp
All four JsonToType<T> specializations now delegate to the matching core. Integral types call json_convert_to_i64_core, then narrow to T with overflow detection. bool calls json_convert_to_bool_core. Floating-point goes through json_convert_to_float_core via a float intermediate. fl::string calls json_convert_to_string_core. The unity build gains an include for json_dispatch.cpp.hpp.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐇 Four little cores now do the work,
No visitor thunks shall linger or lurk.
Switch on the tag, write out the raw,
Template explosions kept in awe.
Flash saved by dozens — hop, hop, hooray! ✨

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/json-visit-dispatch-phase2-3247

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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.

[Type-Erasure] json: collapse JsonToInteger/Float/Bool/String visitors into json_convert_to_<X>_core (#3244 Tier 2E Phase 2)

1 participant