aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPetri Lehtinen <petri@digip.org>2009-09-03 22:00:33 +0300
committerPetri Lehtinen <petri@digip.org>2009-09-04 09:52:40 +0300
commit9611780907cdb0154bd41232df7b2a41903a4ca5 (patch)
treecdda0e715692a569563550b7bc5fae6a7cefdc66 /src
parent93c5892bc3a8138ba44a626d0172563a714e5b64 (diff)
downloadjansson-9611780907cdb0154bd41232df7b2a41903a4ca5.zip
jansson-9611780907cdb0154bd41232df7b2a41903a4ca5.tar.gz
jansson-9611780907cdb0154bd41232df7b2a41903a4ca5.tar.bz2
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().
Diffstat (limited to 'src')
-rw-r--r--src/dump.c17
1 files changed, 9 insertions, 8 deletions
diff --git a/src/dump.c b/src/dump.c
index 4831873..24e206f 100644
--- a/src/dump.c
+++ b/src/dump.c
@@ -36,22 +36,23 @@ static int dump_to_file(const char *buffer, int size, void *data)
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)
{
if(JSON_INDENT(flags) > 0)
{
- char *ws_buffer;
- int ws_count = JSON_INDENT(flags) * depth;
+ int i, ws_count = JSON_INDENT(flags);
if(dump("\n", 1, data))
return -1;
- if(ws_count == 0)
- return 0;
-
- ws_buffer = alloca(ws_count);
- memset(ws_buffer, ' ', ws_count);
- return dump(ws_buffer, ws_count, data);
+ for(i = 0; i < depth; i++)
+ {
+ if(dump(whitespace, ws_count, data))
+ return -1;
+ }
}
return 0;
}