Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6ce4369

Browse files
authored
BLECharacteristic optimization (#10665)
* 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. * BLECharacteristic optimization Calls to BLEUtils::buildHexData() don't output anything when the log level is not "VERBOSE" or "DEBUG". As this function is quite CPU intensive, it is better to not call it when not needed.
1 parent 98da424 commit 6ce4369

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

‎libraries/BLE/src/BLECharacteristic.cpp‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,13 @@ void BLECharacteristic::handleGATTServerEvent(esp_gatts_cb_event_t event, esp_ga
279279

280280
log_d(" - Response to write event: New value: handle: %.2x, uuid: %s", getHandle(), getUUID().toString().c_str());
281281

282+
// The call to BLEUtils::buildHexData() doesn't output anything if the log level is not
283+
// "DEBUG". As it is quite CPU intensive, it is much better to not call it if not needed.
284+
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_DEBUG
282285
char *pHexData = BLEUtils::buildHexData(nullptr, param->write.value, param->write.len);
283286
log_d(" - Data: length: %d, data: %s", param->write.len, pHexData);
284287
free(pHexData);
288+
#endif
285289

286290
if (param->write.need_rsp) {
287291
esp_gatt_rsp_t rsp;
@@ -390,9 +394,13 @@ void BLECharacteristic::handleGATTServerEvent(esp_gatts_cb_event_t event, esp_ga
390394
rsp.attr_value.handle = param->read.handle;
391395
rsp.attr_value.auth_req = ESP_GATT_AUTH_REQ_NONE;
392396

397+
// The call to BLEUtils::buildHexData() doesn't output anything if the log level is not
398+
// "DEBUG". As it is quite CPU intensive, it is much better to not call it if not needed.
399+
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_DEBUG
393400
char *pHexData = BLEUtils::buildHexData(nullptr, rsp.attr_value.value, rsp.attr_value.len);
394401
log_d(" - Data: length=%d, data=%s, offset=%d", rsp.attr_value.len, pHexData, rsp.attr_value.offset);
395402
free(pHexData);
403+
#endif
396404

397405
esp_err_t errRc = ::esp_ble_gatts_send_response(gatts_if, param->read.conn_id, param->read.trans_id, ESP_GATT_OK, &rsp);
398406
if (errRc != ESP_OK) {
@@ -471,7 +479,20 @@ void BLECharacteristic::notify(bool is_notification) {
471479

472480
m_pCallbacks->onNotify(this); // Invoke the notify callback.
473481

482+
// GeneralUtils::hexDump() doesn't output anything if the log level is not
483+
// "VERBOSE". Additionally, it is very CPU intensive, even when it doesn't
484+
// output anything! So it is much better to *not* call it at all if not needed.
485+
// In a simple program which calls BLECharacteristic::notify() every 50 ms,
486+
// the performance gain of this little optimization is 37% in release mode
487+
// (-O3) and 57% in debug mode.
488+
// Of course, the "#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_VERBOSE" guard
489+
// could also be put inside the GeneralUtils::hexDump() function itself. But
490+
// it's better to put it here also, as it is clearer (indicating a verbose log
491+
// thing) and it allows to remove the "m_value.getValue().c_str()" call, which
492+
// is, in itself, quite CPU intensive.
493+
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_VERBOSE
474494
GeneralUtils::hexDump((uint8_t *)m_value.getValue().c_str(), m_value.getValue().length());
495+
#endif
475496

476497
if (getService()->getServer()->getConnectedCount() == 0) {
477498
log_v("<< notify: No connected clients.");
@@ -624,9 +645,13 @@ void BLECharacteristic::setReadProperty(bool value) {
624645
* @param [in] length The length of the data in bytes.
625646
*/
626647
void BLECharacteristic::setValue(uint8_t *data, size_t length) {
648+
// The call to BLEUtils::buildHexData() doesn't output anything if the log level is not
649+
// "VERBOSE". As it is quite CPU intensive, it is much better to not call it if not needed.
650+
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_VERBOSE
627651
char *pHex = BLEUtils::buildHexData(nullptr, data, length);
628652
log_v(">> setValue: length=%d, data=%s, characteristic UUID=%s", length, pHex, getUUID().toString().c_str());
629653
free(pHex);
654+
#endif
630655
if (length > ESP_GATT_MAX_ATTR_LEN) {
631656
log_e("Size %d too large, must be no bigger than %d", length, ESP_GATT_MAX_ATTR_LEN);
632657
return;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /