Skip to content

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

Closed

Description

@zackees

Parent: #3244 (Phase 3 burn-down).

Goal

Phase 1 (#3241) replaced SizeVisitor + IsArrayVisitor with direct data.is<T>() / data.ptr<T>() tag checks, saving 416 B on LPC845.

This issue is Phase 2: collapse the conversion visitors (JsonToIntegerVisitor<T>, JsonToFloatVisitor<T>, JsonToBoolVisitor, JsonToStringVisitor) into 4 non-template json_convert_to_<X>_core(tag, storage, out) helpers. Each conversion visitor today emits 10 per-alternative operator() overloads × 1-2 templated T values = 20-30 instantiated bodies; the core approach collapses that to one dispatch per output type.

Reproduction (baseline)

bash bloat lpc845brk --example AutoResearch --top 15
# Look for JsonToIntegerVisitor / JsonToFloatVisitor / BoolConversionVisitor /
# StringConversionVisitor entries -- they total ~2-3 KB on the LPC845 build.

The conversion visitors are reached from json::as<T>() / json::try_as<T>() and from the RPC arg-conversion path (JsonArgConverter).

Acceptance

  • PR introduces 4 non-template helpers under fl/stl/json/dispatch.cpp.hpp:
    • TypeConversionResult json_convert_to_integer_core(json_tag, const void* storage, i64* out)
    • TypeConversionResult json_convert_to_bool_core(json_tag, const void* storage, bool* out)
    • TypeConversionResult json_convert_to_float_core(json_tag, const void* storage, float* out)
    • TypeConversionResult json_convert_to_string_core(json_tag, const void* storage, fl::string* out)
      Each does a single switch over the 10 alternatives.
  • JsonToIntegerVisitor<T> / JsonToFloatVisitor<T> / JsonToBoolVisitor / JsonToStringVisitor become thin templated wrappers that call the core + apply per-T narrowing (e.g. static_cast<T>(i64_result) + range check).
  • json_value exposes a public tag() + raw_storage() accessor so the cores can read the variant without going through visit().
  • Existing host tests in tests/test_json_*.cpp pass unchanged.
  • Measured savings on LPC845-BRK / AutoResearch: ≥ 1.5 KB net flash drop (audit predicted ~2-3 KB; the higher figure assumed Large-memory targets where the float/double conversion machinery is also active).

Design reference

The JSON visitor codegen audit run during #3235 catalogued the quadratic (visitor × alternative) thunk explosion and the proposed dispatch-core shape. Summary in #3235's meta body; full sub-agent report available from this session's transcript.

Risk

  • json_value::visit(Visitor) is public API. External code (user sketches doing j.visit(my_visitor)) must keep working — variant::visit stays intact; the dispatch redesign is additive (a parallel non-template path used internally by the conversion + serializer entry points).
  • default_value_visitor<T> uses SFINAE on integer/float conversion — leave it untouched in this PR.
  • SerializerVisitor recursion (serialize_value(json_value*) calling data.visit(*this)) is the trickiest case; defer to a Phase 3 follow-on (file a new sub-issue if it's worth attacking).

Out of scope

  • default_value_visitor<T> — not worth touching for the savings.
  • SerializerVisitor recursive case — defer.
  • The json token tag enum surface change (if any) — should stay internal to fl/stl/json/.

Refs #3244, #3235 (Tier 2E Phase 2).

Activity

  1. moved this from Triage to Done in FastLED Trackeron Jun 19, 2026
  2. added a commit that references this issue on Jun 19, 2026
    71a2122
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

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions