Skip to content

Commit

Permalink
webui: slightly reduce wsRegister() overhead
Browse files Browse the repository at this point in the history
Since there are no dynamic callbacks registered at setup(),
use simple function pointers as the input type.

Internal type is still std::func, though
(and not yet sure how to get rid of it correctly)
  • Loading branch information
mcspr committed Aug 17, 2021
1 parent a6062fe commit 459591b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
2 changes: 1 addition & 1 deletion code/espurna/debug.cpp
Expand Up @@ -186,7 +186,7 @@ void formatAndSend(const char* format, va_list args) {
return;
}

const size_t BufferSize { len + 1 };
const size_t BufferSize { static_cast<size_t>(len) + 1 };
auto* buffer = new (std::nothrow) char[BufferSize];
if (!buffer) {
return;
Expand Down
10 changes: 5 additions & 5 deletions code/espurna/ws.cpp
Expand Up @@ -146,27 +146,27 @@ void wsPostSequence(const ws_on_send_callback_list_t& cbs) {

// -----------------------------------------------------------------------------

ws_callbacks_t& ws_callbacks_t::onVisible(ws_on_send_callback_f cb) {
ws_callbacks_t& ws_callbacks_t::onVisible(ws_callbacks_t::on_send_f cb) {
on_visible.push_back(cb);
return *this;
}

ws_callbacks_t& ws_callbacks_t::onConnected(ws_on_send_callback_f cb) {
ws_callbacks_t& ws_callbacks_t::onConnected(ws_callbacks_t::on_send_f cb) {
on_connected.push_back(cb);
return *this;
}

ws_callbacks_t& ws_callbacks_t::onData(ws_on_send_callback_f cb) {
ws_callbacks_t& ws_callbacks_t::onData(ws_callbacks_t::on_send_f cb) {
on_data.push_back(cb);
return *this;
}

ws_callbacks_t& ws_callbacks_t::onAction(ws_on_action_callback_f cb) {
ws_callbacks_t& ws_callbacks_t::onAction(ws_callbacks_t::on_action_f cb) {
on_action.push_back(cb);
return *this;
}

ws_callbacks_t& ws_callbacks_t::onKeyCheck(ws_on_keycheck_callback_f cb) {
ws_callbacks_t& ws_callbacks_t::onKeyCheck(ws_callbacks_t::on_keycheck_f cb) {
on_keycheck.push_back(cb);
return *this;
}
Expand Down
18 changes: 12 additions & 6 deletions code/espurna/ws.h
Expand Up @@ -36,16 +36,22 @@ using ws_on_send_callback_f = std::function<void(JsonObject& root)>;
using ws_on_action_callback_f = std::function<void(uint32_t client_id, const char * action, JsonObject& data)>;
using ws_on_keycheck_callback_f = std::function<bool(const char * key, JsonVariant& value)>;

// TODO: use iterators as inputs for Post(), avoid depending on vector / any specific container
using ws_on_send_callback_list_t = std::vector<ws_on_send_callback_f>;
using ws_on_action_callback_list_t = std::vector<ws_on_action_callback_f>;
using ws_on_keycheck_callback_list_t = std::vector<ws_on_keycheck_callback_f>;

struct ws_callbacks_t {
ws_callbacks_t& onVisible(ws_on_send_callback_f);
ws_callbacks_t& onConnected(ws_on_send_callback_f);
ws_callbacks_t& onData(ws_on_send_callback_f);
ws_callbacks_t& onAction(ws_on_action_callback_f);
ws_callbacks_t& onKeyCheck(ws_on_keycheck_callback_f);
using on_send_f = void(*)(JsonObject&);
ws_callbacks_t& onVisible(on_send_f);
ws_callbacks_t& onConnected(on_send_f);
ws_callbacks_t& onData(on_send_f);

using on_action_f = void(*)(uint32_t, const char*, JsonObject&);
ws_callbacks_t& onAction(on_action_f);

using on_keycheck_f = bool(*)(const char*, JsonVariant&);
ws_callbacks_t& onKeyCheck(on_keycheck_f);

ws_on_send_callback_list_t on_visible;
ws_on_send_callback_list_t on_connected;
Expand All @@ -70,7 +76,7 @@ bool wsDebugSend(const char* prefix, const char* message);
// WARNING: callback lists are taken by reference! make sure that list is ether:
// - std::move(...)'ed to give control of the callback list to us
// - persistent and will be available after the current block ends (global, heap-allocated, etc.)
// de-allocation is not expected e.g. ws_callbacks_t from wsRegister() is never destroyed
// de-allocation is not expected e.g. referenced struct from `wsRegister()` is never destroyed

void wsPost(uint32_t client_id, ws_on_send_callback_f&& cb);
void wsPost(ws_on_send_callback_f&& cb);
Expand Down

0 comments on commit 459591b

Please sign in to comment.