Browse Source

API enhancement: permit to set initial printbuf size

The default printbuf size of 32 bytes is very conservative and
can cause a lot of malloc() action for all but trivial json objects.
The new json_global_set_printbuf_initial_size() API permits an
application to set a new default size.
pull/208/head
Rainer Gerhards 10 years ago
parent
commit
222e360188
2 changed files with 28 additions and 1 deletions
  1. +7
    -1
      printbuf.c
  2. +21
    -0
      printbuf.h

+ 7
- 1
printbuf.c View File

@@ -28,15 +28,21 @@
#include "debug.h"
#include "printbuf.h"

static int printbuf_initial_size = 32;
static int printbuf_extend(struct printbuf *p, int min_size);

void json_global_set_printbuf_initial_size(int size)
{
printbuf_initial_size = size;
}

struct printbuf* printbuf_new(void)
{
struct printbuf *p;

p = (struct printbuf*)calloc(1, sizeof(struct printbuf));
if(!p) return NULL;
p->size = 32;
p->size = printbuf_initial_size;
p->bpos = 0;
if(!(p->buf = (char*)malloc(p->size))) {
free(p);


+ 21
- 0
printbuf.h View File

@@ -81,6 +81,27 @@ printbuf_reset(struct printbuf *p);
extern void
printbuf_free(struct printbuf *p);

/**
* Set initial size allocation for memory when creating strings,
* as is done for example in json_object_to_json_string(). The
* default size is 32, which is very conservative. If an app
* knows it typically deals with larger strings, performance
* can be improved by setting the initial size to a different
* number, e.g. 1k. Note that this also means that memory
* consumption can increase. How far entriely depens on the
* application and its use of json-c.
*
* Note: each time this function is called, the initial size is
* changed to the given value. Already existing elements are not
* affected. This function is usually meant to be called just once
* at start of an application, but there is no harm calling it more
* than once. Note that the function is NOT thread-safe and must not
* be called on different threads concurrently.
*
* @param size new initial size for printbuf (formatting buffer)
*/
extern void
json_global_set_printbuf_initial_size(int size);
#ifdef __cplusplus
}
#endif


Loading…
Cancel
Save