MCPcopy Create free account
hub / github.com/FastLED/FastLED / optimize_array

Function optimize_array

src/fl/stl/json.cpp.hpp:733–786  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

731}
732
733fl::shared_ptr<json_value> optimize_array(fl::shared_ptr<json_value> array_val) {
734 auto arr = array_val->data.ptr<json_array>();
735 if (!arr) return array_val;
736
737 ArrayType type = classify_array(*arr);
738
739 switch (type) {
740 case ALL_UINT8: {
741 fl::vector<u8> vec;
742 vec.reserve(arr->size());
743 for (const auto& elem : *arr) {
744 auto val = elem->as_int();
745 if (val) vec.push_back(static_cast<u8>(*val));
746 }
747 return fl::make_shared<json_value>(fl::move(vec));
748 }
749
750 case ALL_INT16: {
751 fl::vector<i16> vec;
752 vec.reserve(arr->size());
753 for (const auto& elem : *arr) {
754 auto val = elem->as_int();
755 if (val) vec.push_back(static_cast<i16>(*val));
756 }
757 return fl::make_shared<json_value>(fl::move(vec));
758 }
759
760 case ALL_FLOATS: {
761 fl::vector<float> vec;
762 vec.reserve(arr->size());
763 for (const auto& elem : *arr) {
764 if (elem->is_float()) {
765 auto val = elem->as_float();
766 if (val) vec.push_back(*val);
767 } else if (elem->is_int()) {
768 auto val = elem->as_int();
769 if (val) {
770 // Route i64 -> float via i32 to avoid the direct cast
771 // pulling libgcc-nofp's `_floatdisf.o` (~5 KB of soft-FP
772 // double cascade) on no-FPU targets. ALL_FLOATS
773 // classification only fires when every int is within
774 // +/-2^24 anyway (classify_array line 717), so the int32
775 // narrowing is lossless. See FastLED #3076.
776 vec.push_back(static_cast<float>(static_cast<fl::i32>(*val)));
777 }
778 }
779 }
780 return fl::make_shared<json_value>(fl::move(vec));
781 }
782
783 default:
784 return array_val; // Keep as generic array
785 }
786}
787
788// Builder - handles nested objects/arrays with stack management (Milestone 6)
789class JsonBuilder : public JsonVisitor {

Callers 1

on_tokenMethod · 0.85

Calls 8

classify_arrayFunction · 0.85
reserveMethod · 0.45
sizeMethod · 0.45
as_intMethod · 0.45
push_backMethod · 0.45
is_floatMethod · 0.45
as_floatMethod · 0.45
is_intMethod · 0.45

Tested by

no test coverage detected