mirror of
https://github.com/json-c/json-c.git
synced 2026-04-02 03:49:06 +08:00
Improve json_object -> string performance
Removes variadic prints for ~3x performance improvement.
This commit is contained in:
34
printbuf.c
34
printbuf.c
@@ -85,6 +85,7 @@ int printbuf_memappend(struct printbuf *p, const char *buf, int size)
|
||||
if (printbuf_extend(p, p->bpos + size + 1) < 0)
|
||||
return -1;
|
||||
}
|
||||
|
||||
memcpy(p->buf + p->bpos, buf, size);
|
||||
p->bpos += size;
|
||||
p->buf[p->bpos]= '\0';
|
||||
@@ -111,34 +112,6 @@ int printbuf_memset(struct printbuf *pb, int offset, int charvalue, int len)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sprintbuf(struct printbuf *p, const char *msg, ...)
|
||||
{
|
||||
va_list ap;
|
||||
char *t;
|
||||
int size;
|
||||
char buf[128];
|
||||
|
||||
/* user stack buffer first */
|
||||
va_start(ap, msg);
|
||||
size = vsnprintf(buf, 128, msg, ap);
|
||||
va_end(ap);
|
||||
/* if string is greater than stack buffer, then use dynamic string
|
||||
with vasprintf. Note: some implementation of vsnprintf return -1
|
||||
if output is truncated whereas some return the number of bytes that
|
||||
would have been written - this code handles both cases. */
|
||||
if(size == -1 || size > 127) {
|
||||
va_start(ap, msg);
|
||||
if((size = vasprintf(&t, msg, ap)) < 0) { va_end(ap); return -1; }
|
||||
va_end(ap);
|
||||
printbuf_memappend(p, t, size);
|
||||
free(t);
|
||||
return size;
|
||||
} else {
|
||||
printbuf_memappend(p, buf, size);
|
||||
return size;
|
||||
}
|
||||
}
|
||||
|
||||
void printbuf_reset(struct printbuf *p)
|
||||
{
|
||||
p->buf[0] = '\0';
|
||||
@@ -152,3 +125,8 @@ void printbuf_free(struct printbuf *p)
|
||||
free(p);
|
||||
}
|
||||
}
|
||||
|
||||
inline int sprintbuf(struct printbuf *p, const char *buf)
|
||||
{
|
||||
return printbuf_memappend(p, buf, strlen(buf));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user