Repository navigation
fix(json): use integer float conversion for low-memory JSON - #3211
Conversation
|
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 (8)
✅ Files skipped from review due to trivial changes (3)
🚧 Files skipped from review as they are similar to previous changes (4)
📝 WalkthroughWalkthroughIntroduces ChangesFL_JSON_FLOAT_ENABLED Guard and Fixed-Point JSON Integration
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related issues
Possibly related PRs
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 |
There was a problem hiding this comment.
Actionable comments posted: 3
🧹 Nitpick comments (1)
tests/fl/stl/json.cpp (1)
1602-1616: ⚡ Quick winExercise the fixed-point assignment overload too.
This covers construction and
set(), but the newjson& operator=(FP)path remains untested. A direct assignment check keeps the public fixed-point API covered.Proposed test addition
json value(original); auto extracted = value.as_fixed_point<FP>(); FL_REQUIRE(extracted.has_value()); FL_CHECK_EQ(extracted->raw(), original.raw()); + json assigned; + assigned = original; + auto assigned_value = assigned.as_fixed_point<FP>(); + FL_REQUIRE(assigned_value.has_value()); + FL_CHECK_EQ(assigned_value->raw(), original.raw()); + json obj = json::object(); obj.set("gain", original); auto object_value = obj["gain"].as_fixed_point<FP>();🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@tests/fl/stl/json.cpp` around lines 1602 - 1616, The test currently covers json construction from a fixed_point value and the set() method, but does not test the direct assignment operator. Add a test case that exercises the json& operator=(FP) assignment path by directly assigning a fixed_point value to a json object using the subscript operator and assignment syntax, then verify the value was correctly assigned by extracting and comparing it back to the original.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/fl/math/screenmap.cpp.hpp`:
- Line 373: Replace the `FL_WARN_F` macro call in the ScreenMap::toJson method
with the stream-style `FL_WARN` macro. Change from using `FL_WARN_F` with
formatted string syntax to using `FL_WARN` with the `<<` stream operator to
maintain consistency with the repository's warning logging guidelines.
In `@src/fl/stl/json.h`:
- Around line 334-341: The as_fixed_point() method currently uses
mValue->as_int() which performs automatic type conversions from strings, bools,
and floats, allowing non-integer values to be incorrectly extracted as
fixed-point types. Instead, access the raw i64 slot directly from mValue and
validate that the value exists in that slot and fits within the range of the
fixed-point raw type before calling FP::from_raw. This ensures fixed-point
values are only extracted when they were actually stored as raw integers in the
JSON i64 slot.
In `@src/fl/stl/json/types.h`:
- Around line 28-30: The macro FL_JSON_HAS_FLOAT uses the
FL_<COMPONENT>_HAS_<FEATURE> naming convention but is defined with a value
(0/1), which violates the capability-macro contract where such macros should be
defined-or-undefined with no assigned value. Either refactor FL_JSON_HAS_FLOAT
to be a proper capability macro by conditionally defining it without a value
(e.g., just `#define` FL_JSON_HAS_FLOAT) and update all checks from `#ifdef` or `#if`
FL_JSON_HAS_FLOAT to `#if` defined(FL_JSON_HAS_FLOAT), or rename the macro to a
different naming convention (like FL_JSON_FLOAT_ENABLED) if value-based
overrides with -DFL_JSON_HAS_FLOAT=0 semantics are required.
---
Nitpick comments:
In `@tests/fl/stl/json.cpp`:
- Around line 1602-1616: The test currently covers json construction from a
fixed_point value and the set() method, but does not test the direct assignment
operator. Add a test case that exercises the json& operator=(FP) assignment path
by directly assigning a fixed_point value to a json object using the subscript
operator and assignment syntax, then verify the value was correctly assigned by
extracting and comparing it back to the original.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 811f6b6e-1b89-46e0-8828-d29784d26119
📒 Files selected for processing (7)
src/fl/math/screenmap.cpp.hppsrc/fl/stl/json.hsrc/fl/stl/json/types.hsrc/platforms/shared/ui/json/json_console.cpp.hppsrc/platforms/shared/ui/json/number_field.cpp.hppsrc/platforms/shared/ui/json/slider.cpp.hpptests/fl/stl/json.cpp
a1ff4bd to
6bd2557
Compare
There was a problem hiding this comment.
Actionable comments posted: 3
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/fl/math/screenmap.cpp.hpp (1)
415-432:⚠️ Potential issue | 🟠 Major | ⚡ Quick winGate ScreenMap float JSON emission behind the float-support flag.
Lines 415/419/432 now always emit
fl::json(float). That conflicts with the low-memory contract described for this PR (no float JSON paths when float support is disabled). Please guard this emission path withFL_JSON_FLOAT_ENABLED(or provide a fixed-point/integer emission fallback) so low-memory JSON builds remain float-free.Proposed minimal guard pattern
`#if` FASTLED_NO_JSON FL_WARN("ScreenMap::toJson called with FASTLED_NO_JSON"); return; `#else` +#if !FL_JSON_FLOAT_ENABLED + FL_WARN("ScreenMap::toJson called without JSON float support"); + return; +#endif if (!doc) { FL_WARN_F("ScreenMap::toJson called with nullptr doc"); return; }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/fl/math/screenmap.cpp.hpp` around lines 415 - 432, The code currently unconditionally emits float values to JSON (the x and y array values via segment[i].x and segment[i].y, and the diameter field) which conflicts with the low-memory float-free contract when FL_JSON_FLOAT_ENABLED is disabled. Guard the float emission paths using the FL_JSON_FLOAT_ENABLED preprocessor flag: wrap the xArray and yArray population loops and the diameter field assignment in the segmentObj with conditional compilation blocks, and provide integer or fixed-point alternatives (or skip emission) when the flag is disabled to ensure the low-memory JSON builds remain float-free.
🧹 Nitpick comments (1)
tests/fl/stl/json.cpp (1)
1602-1622: ⚡ Quick winAdd at least one non-float-exact raw sample to the fixed-point round-trip test.
The current raw sample (
0x00018000) is float-exact, so this test can still pass under float-backed storage. Add a case like0x01000001to enforce true raw fidelity.Proposed test extension
FL_TEST_CASE("json fixed_point raw round-trip") { using FP = fl::fixed_point<16, 16>; const FP original = FP::from_raw(0x00018000); + const FP non_float_exact = FP::from_raw(0x01000001); json value(original); auto extracted = value.as_fixed_point<FP>(); FL_REQUIRE(extracted.has_value()); FL_CHECK_EQ(extracted->raw(), original.raw()); + + json precise(non_float_exact); + auto precise_extracted = precise.as_fixed_point<FP>(); + FL_REQUIRE(precise_extracted.has_value()); + FL_CHECK_EQ(precise_extracted->raw(), non_float_exact.raw());🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@tests/fl/stl/json.cpp` around lines 1602 - 1622, The test function "json fixed_point raw round-trip" currently only tests with a float-exact raw value (0x00018000), which means the test could pass even if the implementation uses float-backed storage and loses precision. Add an additional test case within this function using a non-float-exact raw value like 0x01000001 to verify that raw fixed-point values are preserved accurately through the json round-trip conversion process. Apply the same test pattern (construct from raw, convert to json, extract back, verify raw value matches) for this new non-float-exact value.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/fl/stl/json.h`:
- Around line 275-276: The float/double entry points in the json class (the
json(double d) constructor, operator=(double) assignment operator, and set
method overloads accepting double parameters) are not wrapped with the
FL_JSON_FLOAT_ENABLED preprocessor directive, making them always available
regardless of the macro's definition. Wrap these methods with `#ifdef`
FL_JSON_FLOAT_ENABLED and `#endif` directives to properly gate them. Apply this
gating to the json(double) constructor around line 275, the operator=(double)
method around lines 347-350, and the set(key, double) method around line 883 to
ensure these API surfaces are only exposed when FL_JSON_FLOAT_ENABLED is
explicitly enabled.
- Around line 276-277: The fixed-point constructor template json(FP v) at lines
276-277 and related assignment operators at lines 352-355 and 884-885 are
converting fixed-point values to float before storage, which loses precision for
values outside float mantissa range. Replace the float conversion call
detail::fixed_point_to_float(v) with extraction of the raw fixed-point integer
representation (using FP's underlying integer value or bit representation) and
store this raw value in the i64 JSON slot instead of the float conversion path.
Apply this change consistently across all three template instantiation locations
mentioned to maintain raw fixed-point storage semantics.
In `@src/fl/stl/json/types.h`:
- Around line 35-127: The newly added helper functions
json_round_shift_right_u64, json_floor_log2_u64,
json_ieee754_float_bits_from_scaled_u64,
json_ieee754_float_bits_from_double_bits, and json_double_to_float are defined
directly in the header file within the detail namespace, which violates the
coding guideline that header files in src/**/ should contain declarations only.
Move these function implementations to a corresponding .impl.hpp or .cpp.hpp
file and keep only their declarations in the header file, then include the
implementation file at the end of the header.
---
Outside diff comments:
In `@src/fl/math/screenmap.cpp.hpp`:
- Around line 415-432: The code currently unconditionally emits float values to
JSON (the x and y array values via segment[i].x and segment[i].y, and the
diameter field) which conflicts with the low-memory float-free contract when
FL_JSON_FLOAT_ENABLED is disabled. Guard the float emission paths using the
FL_JSON_FLOAT_ENABLED preprocessor flag: wrap the xArray and yArray population
loops and the diameter field assignment in the segmentObj with conditional
compilation blocks, and provide integer or fixed-point alternatives (or skip
emission) when the flag is disabled to ensure the low-memory JSON builds remain
float-free.
---
Nitpick comments:
In `@tests/fl/stl/json.cpp`:
- Around line 1602-1622: The test function "json fixed_point raw round-trip"
currently only tests with a float-exact raw value (0x00018000), which means the
test could pass even if the implementation uses float-backed storage and loses
precision. Add an additional test case within this function using a
non-float-exact raw value like 0x01000001 to verify that raw fixed-point values
are preserved accurately through the json round-trip conversion process. Apply
the same test pattern (construct from raw, convert to json, extract back, verify
raw value matches) for this new non-float-exact value.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 73feee0e-fb18-47f5-b836-6d87afc4a1df
📒 Files selected for processing (7)
src/fl/math/screenmap.cpp.hppsrc/fl/stl/json.hsrc/fl/stl/json/types.hsrc/platforms/shared/ui/json/json_console.cpp.hppsrc/platforms/shared/ui/json/number_field.cpp.hppsrc/platforms/shared/ui/json/slider.cpp.hpptests/fl/stl/json.cpp
🚧 Files skipped from review as they are similar to previous changes (3)
- src/platforms/shared/ui/json/number_field.cpp.hpp
- src/platforms/shared/ui/json/slider.cpp.hpp
- src/platforms/shared/ui/json/json_console.cpp.hpp
6bd2557 to
5c9f24c
Compare
5c9f24c to
02e6a65
Compare
|
Latest update on the remaining CodeRabbit requested-change threads:
Local validation for
|
Fixes #3029.
Updates #3208.
Summary
fl::jsonfloat/double entry points available on low-memory targets while avoiding floating-point arithmetic in the conversion path.doubleinputs to JSON's single-precision float storage with integer IEEE-754 bit handling, including NaN/inf preservation and overflow saturation to infinity.floatinputs as existing float storage, avoid unnecessaryfloat -> doublepromotion inScreenMap::toJson, and continue gating JSON UI implementation files that do real floating-point parsing/arithmetic behindFL_JSON_FLOAT_ENABLED.Validation
bash test --cpp tests/fl/stl/json.cppbash compile uno --examples Blink --backend fbuildbash compile lpc845brk --examples AutoResearch --backend fbuildpassed: flash 62.47 KB / 64 KB, RAM 10.99 KB / 16 KB.Upgrade Notes
No user-facing upgrade is required for existing JSON float callers. Low-memory builds should continue to use the same
fl::json(float),fl::json(double), and fixed-point JSON APIs; the conversion is now handled without depending on target floating-point conversion helpers.Summary by CodeRabbit
Release Notes
New Features
as_fixed_point<FP>()extraction.Improvements
x/y,diameter, and an integerversion.Bug Fixes
Tests