Skip to content

Commit d4680c7

Browse files
remove further type punning without harming performance
1 parent 98fcbee commit d4680c7

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

src/dpp/etf.cpp

+12-7
Original file line numberDiff line numberDiff line change
@@ -269,28 +269,33 @@ const char* etf_parser::read_string(uint32_t length) {
269269
return (const char*)str;
270270
}
271271

272-
static const char* atom_null = "null";
273-
static const char* atom_true = "true";
274-
static const char* atom_false = "false";
275-
276272
json etf_parser::process_atom(const char* atom, uint16_t length) {
277273
json j;
278274

279275
if (atom == NULL) {
280276
return j;
281277
}
282278

279+
/**
280+
* WARNING: PERFORMANCE HOTSPOT
281+
* Valgrind cachegrind identified this as a performance hotspot in DPP when using ETF. This gets called a LOT,
282+
* and the difference here between comparing the simple constant types here where the lengths are known in this
283+
* way, compared to using memcpy or strncpy four times, is absolutely huge (factors of ten). Considering this
284+
* can be called hundreds of times a second on a busy bot, the performance of this function should not be
285+
* underestimated. Think carefully before 'tidying' this function further!
286+
*
287+
*/
283288
if (length >= 3 && length <= 5) {
284-
if (length == 4 && *((uint32_t*)atom) == *((uint32_t*)atom_null)) { // "null"
289+
if (length == 4 && atom[0] == 'n' && atom[1] == 'u' && atom[2] == 'l' && atom[3] == 'l') { // "null"
285290
return j;
286291
}
287-
else if(length == 4 && *((uint32_t*)atom) == *((uint32_t*)atom_true)) { // "true"
292+
else if (length == 4 && atom[0] == 't' && atom[1] == 'r' && atom[2] == 'u' && atom[3] == 'e') { // "true"
288293
return true;
289294
}
290295
else if (length == 3 && atom[0] == 'n' && atom[1] == 'i' && atom[2] == 'l') { // "nil"
291296
return j;
292297
}
293-
else if (length == 5 && *((uint32_t*)atom) == *((uint32_t*)atom_false) && atom[4] == 'e') { // "fals","e"
298+
else if (length == 5 && atom[0] == 'f' && atom[1] == 'a' && atom[2] == 'l' && atom[3] == 's' && atom[4] == 'e') { // "fals","e"
294299
return false;
295300
}
296301
}

0 commit comments

Comments
 (0)