From 459591b0df6414a684589fd5d576ecd89c85a7a8 Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Tue, 17 Aug 2021 03:58:46 +0300 Subject: [PATCH] webui: slightly reduce wsRegister() overhead 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) --- code/espurna/debug.cpp | 2 +- code/espurna/ws.cpp | 10 +++++----- code/espurna/ws.h | 18 ++++++++++++------ 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/code/espurna/debug.cpp b/code/espurna/debug.cpp index 82a03aae48..d089c9d435 100644 --- a/code/espurna/debug.cpp +++ b/code/espurna/debug.cpp @@ -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(len) + 1 }; auto* buffer = new (std::nothrow) char[BufferSize]; if (!buffer) { return; diff --git a/code/espurna/ws.cpp b/code/espurna/ws.cpp index 06e10fda87..d65fef7496 100644 --- a/code/espurna/ws.cpp +++ b/code/espurna/ws.cpp @@ -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; } diff --git a/code/espurna/ws.h b/code/espurna/ws.h index 019b2fe5ae..7d06399295 100644 --- a/code/espurna/ws.h +++ b/code/espurna/ws.h @@ -36,16 +36,22 @@ using ws_on_send_callback_f = std::function; using ws_on_action_callback_f = std::function; using ws_on_keycheck_callback_f = std::function; +// TODO: use iterators as inputs for Post(), avoid depending on vector / any specific container using ws_on_send_callback_list_t = std::vector; using ws_on_action_callback_list_t = std::vector; using ws_on_keycheck_callback_list_t = std::vector; 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; @@ -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);