Skip to content

Commit d73c520

Browse files
authored
BLECharacteristic::notify() optimization
GeneralUtils::hexDump() doesn't output anything if the log level is not "VERBOSE". Additionally, it is very CPU intensive, even when it doesn't output anything. So it is much better to *not* call it at all if not needed. In a simple program which calls BLECharacteristic::notify() every 50 ms, the performance gain of this little optimization is 37% in release mode (-O3) and 57% in debug mode. Of course, the "#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_VERBOSE" guard could also be put inside the GeneralUtils::hexDump() function itself. But it's better to put it here also, as it is clearer (indicating a verbose log thing) and it allows to remove the "m_value.getValue().c_str()" call, which is in itself quite CPU intensive.
1 parent e27a050 commit d73c520

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

Diff for: libraries/BLE/src/BLECharacteristic.cpp

+14-1
Original file line numberDiff line numberDiff line change
@@ -471,8 +471,21 @@ void BLECharacteristic::notify(bool is_notification) {
471471

472472
m_pCallbacks->onNotify(this); // Invoke the notify callback.
473473

474+
// GeneralUtils::hexDump() doesn't output anything if the log level is not
475+
// "VERBOSE". Additionally, it is very CPU intensive, even when it doesn't
476+
// output anything! So it is much better to *not* call it at all if not needed.
477+
// In a simple program which calls BLECharacteristic::notify() every 50 ms,
478+
// the performance gain of this little optimization is 37% in release mode
479+
// (-O3) and 57% in debug mode.
480+
// Of course, the "#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_VERBOSE" guard
481+
// could also be put inside the GeneralUtils::hexDump() function itself. But
482+
// it's better to put it here also, as it is clearer (indicating a verbose log
483+
// thing) and it allows to remove the "m_value.getValue().c_str()" call, which
484+
// is, in itself, quite CPU intensive.
485+
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_VERBOSE
474486
GeneralUtils::hexDump((uint8_t *)m_value.getValue().c_str(), m_value.getValue().length());
475-
487+
#endif
488+
476489
if (getService()->getServer()->getConnectedCount() == 0) {
477490
log_v("<< notify: No connected clients.");
478491
m_pCallbacks->onStatus(this, BLECharacteristicCallbacks::Status::ERROR_NO_CLIENT, 0);

0 commit comments

Comments
 (0)