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 5, 2024
1 parent 2d38b64 commit 9bb6a33
Show file tree
Hide file tree
Showing 16 changed files with 87 additions and 35 deletions.
2 changes: 1 addition & 1 deletion idf_component.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ dependencies:
idf: ">=5.1"
RB3201-RBProtocol-library:
git: https://github.com/RoboticsBrno/RB3201-RBProtocol-library.git
version: 49f81d8fae9d5eea1ce2b44ed786ec6ed25174b7
version: v14.0.0
4 changes: 2 additions & 2 deletions library.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
"maintainer": true
}
],
"version": "5.2.8",
"version": "5.3.0",
"frameworks": ["espidf", "arduino"],
"platforms": "espressif32",
"dependencies": [
{
"name": "RB3201-RBProtocol",
"version": "https://github.com/RoboticsBrno/RB3201-RBProtocol-library/archive/refs/tags/v13.3.3.zip"
"version": "https://github.com/RoboticsBrno/RB3201-RBProtocol-library/archive/refs/tags/v14.0.0.zip"
}
],
"build": {
Expand Down
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ framework = arduino
upload_speed = 921600
monitor_speed = 115200

lib_deps = https://github.com/RoboticsBrno/RB3201-RBProtocol-library/archive/refs/tags/v13.3.3.zip
lib_deps = https://github.com/RoboticsBrno/RB3201-RBProtocol-library/archive/refs/tags/v14.0.0.zip
2 changes: 2 additions & 0 deletions src/builder/widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ rbjson::Object& Widget::style() {
}

void Widget::serialize(std::ostream& ss) {
extra().shrink_to_fit();

ss << "{";
{
ss << "\"uuid\":" << m_state.uuid() << ",";
Expand Down
4 changes: 3 additions & 1 deletion src/builder/widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ namespace gridui {

class _GridUi;

extern _GridUi UI;

namespace builder {

template <typename Self, typename Constructed>
Expand All @@ -49,7 +51,7 @@ class BuilderMixin {
}

protected:
void addCallback(const std::string& name, callback_t cb) {
void addCallback(const std::string& name, const callback_t& cb) {
auto* cbHeap = static_cast<void*>(new callback_t(cb)); // fuj
self().m_state.addCallback(&callbackTrampoline, &callbackDeleter, name, cbHeap);
}
Expand Down
2 changes: 1 addition & 1 deletion src/gridui_version.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#pragma once

#define RB_GRIDUI_VERSION 0x050208
#define RB_GRIDUI_VERSION 0x050300
13 changes: 5 additions & 8 deletions src/widgets/widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ WidgetState::WidgetState(uint16_t uuid, float x, float y, float w, float h, uint
, m_bloom_global(0)
, m_bloom_tick(0) {

m_data.set("x", x);
m_data.set("y", y);
m_data.set("w", w);
m_data.set("h", h);
m_data.set("p", WidgetPos(x, y, w, h).encoded());
if(tab != 0) {
m_data.set("tab", tab);
}
Expand Down Expand Up @@ -71,8 +68,8 @@ bool WidgetState::popChanges(rbjson::Object& state) {

const auto& m = m_data.members();
for (auto itr = m.begin(); itr != m.end(); ++itr) {
if (wasChangedInTickLocked(itr->first)) {
state.set(itr->first, itr->second->copy());
if (wasChangedInTickLocked(itr->name, itr->name_len)) {
state.set(std::string(itr->name, itr->name_len), itr->value->copy());
}
}
m_bloom_tick = 0;
Expand Down Expand Up @@ -144,9 +141,9 @@ void WidgetState::markGlobalChangedLocked(const std::string& key) {
}
}

bool WidgetState::wasChangedInTickLocked(const std::string& key) const {
bool WidgetState::wasChangedInTickLocked(const char *key, size_t key_len) const {
for (int i = 0; i < hash_count; ++i) {
const auto bit = murmur3_32((uint8_t*)key.c_str(), key.size(), i) % 16;
const auto bit = murmur3_32((uint8_t*)key, key_len, i) % 16;
if ((m_bloom_tick & (1 << bit)) == 0)
return false;
}
Expand Down
64 changes: 52 additions & 12 deletions src/widgets/widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,42 @@ class CallbacksHolder {
const cb_deleter_t m_cb_deleter;
};

class WidgetPos {
union {
uint32_t _encoded;
struct {
uint8_t _x;
uint8_t _y;
uint8_t _w;
uint8_t _h;
};
};

public:
WidgetPos(uint32_t encoded) {
_encoded = encoded;
}
WidgetPos(float x, float y, float w, float h) {
_x = x*10.f;
_y = y*10.f;
_w = w*10.f;
_h = h*10.f;
}
uint32_t encoded() const { return _encoded; };
float x() const { return float(_x)/10.f; }
float y() const { return float(_y)/10.f; }
float w() const { return float(_w)/10.f; }
float h() const { return float(_h)/10.f; }
WidgetPos& setX(float x) { _x = x*10.f; return *this; }
WidgetPos& setY(float y) { _y = y*10.f; return *this; }
WidgetPos& setW(float w) { _w = w*10.f; return *this; }
WidgetPos& setH(float h) { _h = h*10.f; return *this; }
};

/**
* @defgroup widgets_constructed Layout widgets
* Classes in this module are used to modify state of the already constructed Layout.
*/

class WidgetState {
friend class gridui::builder::Widget;
friend class gridui::_GridUi;
Expand All @@ -88,6 +119,14 @@ class WidgetState {
return bool(m_cb_holder);
}

WidgetPos pos() const {
return WidgetPos(m_data.getInt("p"));
}

void setPos(const WidgetPos& p) {
m_data.set("p", p.encoded());
}

private:
// Each mutex is ~100 bytes of heap allocation. Let's keep just one for this.
static std::mutex m_mutex;
Expand All @@ -100,8 +139,9 @@ class WidgetState {
void update(rbjson::Object* other) {
m_mutex.lock();
for (auto itr : other->members()) {
m_data.set(itr.first, itr.second->copy());
markGlobalChangedLocked(itr.first);
const std::string name_str(itr.name, itr.name_len);
m_data.set(name_str, itr.value->copy());
markGlobalChangedLocked(name_str);
}
m_mutex.unlock();
}
Expand All @@ -121,7 +161,7 @@ class WidgetState {

void markChangedLocked(const std::string& key);
void markGlobalChangedLocked(const std::string& key);
inline bool wasChangedInTickLocked(const std::string& key) const;
inline bool wasChangedInTickLocked(const char *key, size_t key_len) const;

bool popChanges(rbjson::Object& state);
bool remarkAllChanges();
Expand Down Expand Up @@ -163,35 +203,35 @@ class Widget {
}

void setWidgetX(float val) {
m_state->set("x", new rbjson::Number(val));
m_state->setPos(m_state->pos().setX(val));
}

float widgetX() const {
return data().getDouble("x");
return m_state->pos().x();
}

void setWidgetY(float val) {
m_state->set("y", new rbjson::Number(val));
m_state->setPos(m_state->pos().setY(val));
}

float widgetY() const {
return data().getDouble("y");
return m_state->pos().y();
}

void setWidgetW(float val) {
m_state->set("w", new rbjson::Number(val));
m_state->setPos(m_state->pos().setW(val));
}

float widgetW() const {
return data().getDouble("w");
return m_state->pos().w();
}

void setWidgetH(float val) {
m_state->set("h", new rbjson::Number(val));
m_state->setPos(m_state->pos().setH(val));
}

float widgetH() const {
return data().getDouble("h");
return m_state->pos().h();
}

void setWidgetTab(uint16_t tab) {
Expand Down
2 changes: 1 addition & 1 deletion test-inis/esp32-idf3-arduino.ini
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ build_flags =
-fmax-errors=5
-DLX16A_ARDUINO=1

lib_deps = https://github.com/RoboticsBrno/RB3201-RBProtocol-library/archive/refs/tags/v13.3.3.zip
lib_deps = https://github.com/RoboticsBrno/RB3201-RBProtocol-library/archive/refs/tags/v14.0.0.zip
2 changes: 1 addition & 1 deletion test-inis/esp32-idf4-arduino.ini
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ build_flags =
-fmax-errors=5
-DLX16A_ARDUINO=1

lib_deps = https://github.com/RoboticsBrno/RB3201-RBProtocol-library/archive/refs/tags/v13.3.3.zip
lib_deps = https://github.com/RoboticsBrno/RB3201-RBProtocol-library/archive/refs/tags/v14.0.0.zip
2 changes: 1 addition & 1 deletion test-inis/esp32-idf5-idf.ini
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ build_flags =
-std=gnu++14
-fmax-errors=5

lib_deps = https://github.com/RoboticsBrno/RB3201-RBProtocol-library/archive/refs/tags/v13.3.3.zip
lib_deps = https://github.com/RoboticsBrno/RB3201-RBProtocol-library/archive/refs/tags/v14.0.0.zip
2 changes: 1 addition & 1 deletion test-inis/esp32c3-idf4-arduino.ini
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ build_flags =
-fmax-errors=5
-DLX16A_ARDUINO=1

lib_deps = https://github.com/RoboticsBrno/RB3201-RBProtocol-library/archive/refs/tags/v13.3.3.zip
lib_deps = https://github.com/RoboticsBrno/RB3201-RBProtocol-library/archive/refs/tags/v14.0.0.zip
2 changes: 1 addition & 1 deletion test-inis/esp32c3-idf5-idf.ini
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ build_flags =
-std=gnu++14
-fmax-errors=5

lib_deps = https://github.com/RoboticsBrno/RB3201-RBProtocol-library/archive/refs/tags/v13.3.3.zip
lib_deps = https://github.com/RoboticsBrno/RB3201-RBProtocol-library/archive/refs/tags/v14.0.0.zip
2 changes: 1 addition & 1 deletion test-inis/esp32s3-idf4-arduino.ini
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ build_flags =
-fmax-errors=5
-DLX16A_ARDUINO=1

lib_deps = https://github.com/RoboticsBrno/RB3201-RBProtocol-library/archive/refs/tags/v13.3.3.zip
lib_deps = https://github.com/RoboticsBrno/RB3201-RBProtocol-library/archive/refs/tags/v14.0.0.zip
2 changes: 1 addition & 1 deletion test-inis/esp32s3-idf5-idf.ini
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ build_flags =
-std=gnu++14
-fmax-errors=5

lib_deps = https://github.com/RoboticsBrno/RB3201-RBProtocol-library/archive/refs/tags/v13.3.3.zip
lib_deps = https://github.com/RoboticsBrno/RB3201-RBProtocol-library/archive/refs/tags/v14.0.0.zip
15 changes: 13 additions & 2 deletions web/js/05_widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,26 @@ Widget.prototype.applyState = function (state) {
var proto = Object.getPrototypeOf(this)
var pos = false
for (var k in state) {
var v = state[k]

if(k === "p") {
this.x = (v & 0xFF)/10;
this.y = ((v >> 8) & 0xFF)/10;
this.w = ((v >> 16) & 0xFF)/10;
this.h = ((v >> 24) & 0xFF)/10;
pos = true
continue;
}

if (!state.hasOwnProperty(k) || !proto.PROPERTIES.hasOwnProperty(k)) {
continue
}

var prop = proto.PROPERTIES[k]
if (prop.set === undefined) {
this[k] = prop.type(state[k])
this[k] = prop.type(v)
} else {
prop.set.call(this, state[k])
prop.set.call(this, v)
}

if (k.length === 1 && 'xywh'.indexOf(k) !== -1) {
Expand Down

0 comments on commit 9bb6a33

Please sign in to comment.