Skip to content

Commit c23050a

Browse files
committed
refactor(ble): Avoid unnecessary String copies
Pass String by const reference to many `setValue()` functions to avoid copying it. Even there are not so much data inside these strings, it just looks too odd to copy them every time. Also this change decreases binary size (~200 bytes in my case).
1 parent 273eedf commit c23050a

File tree

8 files changed

+11
-11
lines changed

8 files changed

+11
-11
lines changed

libraries/BLE/src/BLE2901.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ BLE2901::BLE2901() : BLEDescriptor(BLEUUID((uint16_t)BLE2901_UUID)) {}
4747
/**
4848
* @brief Set the Characteristic User Description
4949
*/
50-
void BLE2901::setDescription(String userDesc) {
50+
void BLE2901::setDescription(const String &userDesc) {
5151
if (userDesc.length() > ESP_GATT_MAX_ATTR_LEN) {
5252
log_e("Size %d too large, must be no bigger than %d", userDesc.length(), ESP_GATT_MAX_ATTR_LEN);
5353
return;

libraries/BLE/src/BLE2901.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class BLE2901 : public BLEDescriptor {
4040
***************************************************************************/
4141

4242
BLE2901();
43-
void setDescription(String desc);
43+
void setDescription(const String &desc);
4444
}; // BLE2901
4545

4646
#endif /* CONFIG_BLUEDROID_ENABLED || CONFIG_NIMBLE_ENABLED */

libraries/BLE/src/BLECharacteristic.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,8 @@ void BLECharacteristic::setValue(const uint8_t *data, size_t length) {
371371
* @param [in] Set the value of the characteristic.
372372
* @return N/A.
373373
*/
374-
void BLECharacteristic::setValue(String value) {
375-
setValue((uint8_t *)(value.c_str()), value.length());
374+
void BLECharacteristic::setValue(const String &value) {
375+
setValue(reinterpret_cast<const uint8_t *>(value.c_str()), value.length());
376376
} // setValue
377377

378378
void BLECharacteristic::setValue(uint16_t data16) {

libraries/BLE/src/BLECharacteristic.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ class BLECharacteristic {
185185
void notify(bool is_notification = true);
186186
void setCallbacks(BLECharacteristicCallbacks *pCallbacks);
187187
void setValue(const uint8_t *data, size_t size);
188-
void setValue(String value);
188+
void setValue(const String &value);
189189
void setValue(uint16_t data16);
190190
void setValue(uint32_t data32);
191191
void setValue(int data32);

libraries/BLE/src/BLEDescriptor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ void BLEDescriptor::setValue(const uint8_t *data, size_t length) {
203203
* @brief Set the value of the descriptor.
204204
* @param [in] value The value of the descriptor in string form.
205205
*/
206-
void BLEDescriptor::setValue(String value) {
206+
void BLEDescriptor::setValue(const String &value) {
207207
setValue((uint8_t *)value.c_str(), value.length());
208208
} // setValue
209209

libraries/BLE/src/BLEDescriptor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class BLEDescriptor {
9595
void setAccessPermissions(uint8_t perm); // Set the permissions of the descriptor.
9696
void setCallbacks(BLEDescriptorCallbacks *pCallbacks); // Set callbacks to be invoked for the descriptor.
9797
void setValue(const uint8_t *data, size_t size); // Set the value of the descriptor as a pointer to data.
98-
void setValue(String value); // Set the value of the descriptor as a data buffer.
98+
void setValue(const String &value); // Set the value of the descriptor as a data buffer.
9999

100100
String toString(); // Convert the descriptor to a string representation.
101101

libraries/BLE/src/BLEValue.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ BLEValue::BLEValue() {
3737
* The accumulation is a growing set of data that is added to until a commit or cancel.
3838
* @param [in] part A message part being added.
3939
*/
40-
void BLEValue::addPart(String part) {
40+
void BLEValue::addPart(const String &part) {
4141
log_v(">> addPart: length=%d", part.length());
4242
m_accumulation += part;
4343
} // addPart
@@ -121,7 +121,7 @@ void BLEValue::setReadOffset(uint16_t readOffset) {
121121
/**
122122
* @brief Set the current value.
123123
*/
124-
void BLEValue::setValue(String value) {
124+
void BLEValue::setValue(const String &value) {
125125
m_value = value;
126126
} // setValue
127127

libraries/BLE/src/BLEValue.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class BLEValue {
3434
***************************************************************************/
3535

3636
BLEValue();
37-
void addPart(String part);
37+
void addPart(const String &part);
3838
void addPart(const uint8_t *pData, size_t length);
3939
void cancel();
4040
void commit();
@@ -43,7 +43,7 @@ class BLEValue {
4343
uint16_t getReadOffset();
4444
String getValue();
4545
void setReadOffset(uint16_t readOffset);
46-
void setValue(String value);
46+
void setValue(const String &value);
4747
void setValue(const uint8_t *pData, size_t length);
4848

4949
private:

0 commit comments

Comments
 (0)