dump: Optimize indenting
Don't alloca() a whitespace buffer and fill it with spaces in each call to dump_indent. Instead, use a static whitespace buffer. As a bonus, this saves the use of poorly portable alloca().
This commit is contained in:
parent
93c5892bc3
commit
9611780907
17
src/dump.c
17
src/dump.c
@ -36,22 +36,23 @@ static int dump_to_file(const char *buffer, int size, void *data)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 256 spaces (the maximum indentation size) */
|
||||||
|
static char whitespace[] = " ";
|
||||||
|
|
||||||
static int dump_indent(uint32_t flags, int depth, dump_func dump, void *data)
|
static int dump_indent(uint32_t flags, int depth, dump_func dump, void *data)
|
||||||
{
|
{
|
||||||
if(JSON_INDENT(flags) > 0)
|
if(JSON_INDENT(flags) > 0)
|
||||||
{
|
{
|
||||||
char *ws_buffer;
|
int i, ws_count = JSON_INDENT(flags);
|
||||||
int ws_count = JSON_INDENT(flags) * depth;
|
|
||||||
|
|
||||||
if(dump("\n", 1, data))
|
if(dump("\n", 1, data))
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
if(ws_count == 0)
|
for(i = 0; i < depth; i++)
|
||||||
return 0;
|
{
|
||||||
|
if(dump(whitespace, ws_count, data))
|
||||||
ws_buffer = alloca(ws_count);
|
return -1;
|
||||||
memset(ws_buffer, ' ', ws_count);
|
}
|
||||||
return dump(ws_buffer, ws_count, data);
|
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user