Skip to content

Commit 54ac720

Browse files
committed
JSON converter: replace snprintf with branchlut2 for identifier conversion
snprintf() was significant bottleneck when numeric identifiers were used.
1 parent e81a6c4 commit 54ac720

2 files changed

Lines changed: 14 additions & 8 deletions

File tree

src/converters/branchlut2.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#include <stdint.h>
99

10-
const char gDigitsLut[200] = {
10+
static const char gDigitsLut[200] = {
1111
'0','0','0','1','0','2','0','3','0','4','0','5','0','6','0','7','0','8','0','9',
1212
'1','0','1','1','1','2','1','3','1','4','1','5','1','6','1','7','1','8','1','9',
1313
'2','0','2','1','2','2','2','3','2','4','2','5','2','6','2','7','2','8','2','9',

src/converters/json.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include <math.h>
1414
#include <stdlib.h>
1515
#include <inttypes.h>
16+
17+
#include "branchlut2.h"
1618
#include "protocols.h"
1719

1820
/// Base size of the conversion buffer
@@ -1413,18 +1415,22 @@ add_field_name(struct context *buffer, const struct fds_drec_field *field)
14131415
// If definition of field is unknown or if flag FDS_CD2J_NUMERIC_ID is set,
14141416
// then identifier will be in format "enXX:idYY"
14151417
if ((def == NULL) || (num_id)) {
1416-
static const size_t scope_size = 32;
1417-
char raw_name[scope_size];
1418-
14191418
// Max length of identifier in format "enXX:idYY"
14201419
// is "\"en" + 10x <en> + ":id" + 5x <id> + '\"\0'
1421-
snprintf(raw_name, scope_size, "\"en%" PRIu32 ":id%" PRIu16 "\":", field->info->en,
1422-
field->info->id);
1423-
1424-
int ret_code = buffer_append(buffer, raw_name);
1420+
static const size_t id_size = 32;
1421+
int ret_code = buffer_reserve(buffer, buffer_used(buffer) + id_size);
14251422
if (ret_code != FDS_OK) {
14261423
return ret_code;
14271424
}
1425+
1426+
memcpy(buffer->write_begin, "\"en", 3U);
1427+
buffer->write_begin += 3U;
1428+
buffer->write_begin = u32toa_branchlut2(field->info->en, buffer->write_begin);
1429+
memcpy(buffer->write_begin, ":id", 3U);
1430+
buffer->write_begin += 3U;
1431+
buffer->write_begin = u32toa_branchlut2(field->info->id, buffer->write_begin);
1432+
*(buffer->write_begin++) = '"';
1433+
*(buffer->write_begin++) = ':';
14281434
return FDS_OK;
14291435
}
14301436

0 commit comments

Comments
 (0)