Skip to content

Commit

Permalink
feat: reduce memory usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Tasssadar committed Jul 2, 2024
1 parent 8790a10 commit 9de526a
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/builder/arm.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Arm : public Widget, public BuilderMixin<Arm, gridui::Arm> {
}

protected:
virtual void serialize(std::stringstream& ss) {
virtual void serialize(std::ostream& ss) {
Widget::serialize(ss);
extra().remove("info");
}
Expand Down
5 changes: 2 additions & 3 deletions src/builder/widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ namespace builder {

Widget::Widget(const char* type, WidgetState& state)
: m_state(state)
, m_type(type)
, m_style(nullptr) {
, m_type(type) {
}

Widget::~Widget() {
Expand All @@ -30,7 +29,7 @@ rbjson::Object& Widget::style() {
return *res;
}

void Widget::serialize(std::stringstream& ss) {
void Widget::serialize(std::ostream& ss) {
ss << "{";
{
ss << "\"uuid\":" << m_state.uuid() << ",";
Expand Down
3 changes: 1 addition & 2 deletions src/builder/widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class Widget {
protected:
Widget(const char* type, WidgetState& state);

virtual void serialize(std::stringstream& ss);
virtual void serialize(std::ostream& ss);

rbjson::Object& extra();
rbjson::Object& style();
Expand All @@ -96,7 +96,6 @@ class Widget {
Widget& operator=(const Widget&) = delete;

const char* m_type;
rbjson::Object* m_style;
};

};
Expand Down
47 changes: 34 additions & 13 deletions src/gridui.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <esp_log.h>
#include <esp_timer.h>
#include <stdio.h>
#include <fstream>

#include "gridui.h"
#include "rbdns.h"
Expand Down Expand Up @@ -164,34 +165,54 @@ void _GridUi::commit() {
return;
}

std::vector<char> layout_json;
{
std::stringstream ss;
m_layout->serialize(ss);

char buf[256];

snprintf(buf, sizeof(buf), "%s/layout.json", rb_web_get_files_root());

std::ofstream stream;
stream.rdbuf()->pubsetbuf(buf, sizeof(buf));
stream.open(buf, std::ofstream::trunc);

if(stream.fail()) {
ESP_LOGE("GridUI", "failed to open %s", buf);
return;
}

m_states.shrink_to_fit();

m_layout->serialize(stream);
m_layout.reset();

ss.seekp(-1, std::stringstream::cur);
stream.seekp(-1, std::ofstream::cur);

ss << ",\"widgets\": [";
stream << ",\"widgets\": [";
for (size_t i = 0; i < m_widgets.size(); ++i) {
if (i != 0) {
ss << ",";
stream << ",";
}

auto& w = m_widgets[i];
w->serialize(ss);
w->serialize(stream);
w.reset();
}
m_widgets.clear();
m_widgets.shrink_to_fit();
m_states.shrink_to_fit();

ss << "]}";
stream << "]}";

layout_json.resize(((size_t)ss.tellp()) + 1);
ss.get(layout_json.data(), layout_json.size());
}
if(stream.fail()) {
ESP_LOGE("GridUI", "failed to serialize layout");
return;
}

ESP_ERROR_CHECK(rb_web_add_file("layout.json", layout_json.data(), layout_json.size() - 1));
stream.close();
if(stream.fail()) {
ESP_LOGE("GridUI", "failed to write layout");
return;
}
}

esp_timer_create_args_t args = {
.callback = stateChangeTask,
Expand Down
2 changes: 1 addition & 1 deletion src/gridui.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ class _GridUi {
m_states.emplace_back(std::unique_ptr<WidgetState>(state));

auto* widget = new T(T::name(), *state);
m_widgets.push_back(std::unique_ptr<T>(widget));
m_widgets.emplace_back(std::unique_ptr<T>(widget));
return widget;
}

Expand Down
4 changes: 3 additions & 1 deletion src/widgets/widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ WidgetState::WidgetState(uint16_t uuid, float x, float y, float w, float h, uint
m_data.set("y", y);
m_data.set("w", w);
m_data.set("h", h);
m_data.set("tab", tab);
if(tab != 0) {
m_data.set("tab", tab);
}
}

bool WidgetState::set(const std::string& key, rbjson::Value* value) {
Expand Down

0 comments on commit 9de526a

Please sign in to comment.