Improve json_object -> string performance

Removes variadic prints for ~3x performance improvement.
This commit is contained in:
Quentin Young
2017-02-02 17:16:26 +00:00
parent 8157784483
commit 9ff0f4987f
5 changed files with 19 additions and 82 deletions

View File

@@ -17,7 +17,7 @@ static void test_basic_printbuf_memset()
printf("%s: starting test\n", __func__);
pb = printbuf_new();
sprintbuf(pb, "blue:%d", 1);
sprintbuf(pb, "blue:1");
printbuf_memset(pb, -1, 'x', 52);
printf("Buffer contents:%.*s\n", printbuf_length(pb), pb->buf);
printbuf_free(pb);
@@ -111,42 +111,6 @@ static void test_printbuf_memappend(int *before_resize)
printf("%s: end test\n", __func__);
}
static void test_sprintbuf(int before_resize);
static void test_sprintbuf(int before_resize)
{
struct printbuf *pb;
printf("%s: starting test\n", __func__);
pb = printbuf_new();
printf("Buffer length: %d\n", printbuf_length(pb));
char *data = malloc(before_resize + 1 + 1);
memset(data, 'X', before_resize + 1 + 1);
data[before_resize + 1] = '\0';
sprintbuf(pb, "%s", data);
free(data);
printf("sprintbuf to just after resize(%d+1): %d, [%s], strlen(buf)=%d\n", before_resize, printbuf_length(pb), pb->buf, (int)strlen(pb->buf));
printbuf_reset(pb);
sprintbuf(pb, "plain");
printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
sprintbuf(pb, "%d", 1);
printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
sprintbuf(pb, "%d", INT_MAX);
printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
sprintbuf(pb, "%d", INT_MIN);
printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
sprintbuf(pb, "%s", "%s");
printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
printbuf_free(pb);
printf("%s: end test\n", __func__);
}
int main(int argc, char **argv)
{
int before_resize = 0;
@@ -159,8 +123,6 @@ int main(int argc, char **argv)
printf("========================================\n");
test_printbuf_memappend(&before_resize);
printf("========================================\n");
test_sprintbuf(before_resize);
printf("========================================\n");
return 0;
}

View File

@@ -20,13 +20,3 @@ Append to just before resize: 31, [XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX]
Append to just after resize: 32, [XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX]
test_printbuf_memappend: end test
========================================
test_sprintbuf: starting test
Buffer length: 0
sprintbuf to just after resize(31+1): 32, [XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX], strlen(buf)=32
5, [plain]
6, [plain1]
16, [plain12147483647]
27, [plain12147483647-2147483648]
29, [plain12147483647-2147483648%s]
test_sprintbuf: end test
========================================