Browse Source

Optimise json malloc strategy to minimise memory allocations and avoid unnecessary strdups

master
Con Kolivas 10 years ago
parent
commit
94f6236d3c
  1. 13
      src/jansson-2.6/src/dump.c
  2. 3
      src/jansson-2.6/src/jansson_private.h
  3. 25
      src/jansson-2.6/src/memory.c
  4. 5
      src/jansson-2.6/src/strbuffer.c

13
src/jansson-2.6/src/dump.c

@ -1,5 +1,6 @@
/* /*
* Copyright (c) 2009-2013 Petri Lehtinen <petri@digip.org> * Copyright (c) 2009-2013 Petri Lehtinen <petri@digip.org>
* Copyright (c) 2015 Con Kolivas <kernel@kolivas.org>
* *
* Jansson is free software; you can redistribute it and/or modify * Jansson is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details. * it under the terms of the MIT license. See LICENSE for details.
@ -427,13 +428,6 @@ static int do_dump(const json_t *json, size_t flags, int depth,
} }
} }
char *json_dump_dup(const char *str, size_t flags)
{
if (flags & JSON_EOL)
return jsonp_eolstrdup(str);
return jsonp_strdup(str);
}
char *json_dumps(const json_t *json, size_t flags) char *json_dumps(const json_t *json, size_t flags)
{ {
strbuffer_t strbuff; strbuffer_t strbuff;
@ -444,10 +438,11 @@ char *json_dumps(const json_t *json, size_t flags)
if(json_dump_callback(json, dump_to_strbuffer, (void *)&strbuff, flags)) if(json_dump_callback(json, dump_to_strbuffer, (void *)&strbuff, flags))
result = NULL; result = NULL;
else if (flags & JSON_EOL)
result = jsonp_eolstrsteal(&strbuff);
else else
result = json_dump_dup(strbuffer_value(&strbuff), flags); result = jsonp_strsteal(&strbuff);
strbuffer_close(&strbuff);
return result; return result;
} }

3
src/jansson-2.6/src/jansson_private.h

@ -86,7 +86,8 @@ void _jsonp_free(void **ptr);
char *jsonp_strndup(const char *str, size_t length); char *jsonp_strndup(const char *str, size_t length);
char *jsonp_strdup(const char *str); char *jsonp_strdup(const char *str);
char *jsonp_eolstrdup(const char *str); char *jsonp_strsteal(strbuffer_t *strbuff);
char *jsonp_eolstrsteal(strbuffer_t *strbuff);
/* Windows compatibility */ /* Windows compatibility */
#ifdef _WIN32 #ifdef _WIN32

25
src/jansson-2.6/src/memory.c

@ -51,23 +51,22 @@ char *jsonp_strdup(const char *str)
return new_str; return new_str;
} }
char *jsonp_eolstrdup(const char *str) char *jsonp_strsteal(strbuffer_t *strbuff)
{ {
char *new_str; size_t len = strbuff->length + 1;
size_t len; char *ret = realloc(strbuff->value, len);
len = strlen(str); return ret;
if(len == (size_t)-1) }
return NULL;
new_str = jsonp_malloc(len + 2); char *jsonp_eolstrsteal(strbuffer_t *strbuff)
if(!new_str) {
return NULL; size_t len = strbuff->length + 2;
char *ret = realloc(strbuff->value, len);
memcpy(new_str, str, len); ret[strbuff->length] = '\n';
new_str[len] = '\n'; ret[strbuff->length + 1] = '\0';
new_str[len + 1] = '\0'; return ret;
return new_str;
} }
void json_set_alloc_funcs(json_malloc_t malloc_fn, json_free_t free_fn) void json_set_alloc_funcs(json_malloc_t malloc_fn, json_free_t free_fn)

5
src/jansson-2.6/src/strbuffer.c

@ -16,7 +16,7 @@
#include "jansson_private.h" #include "jansson_private.h"
#include "strbuffer.h" #include "strbuffer.h"
#define STRBUFFER_MIN_SIZE 16 #define STRBUFFER_MIN_SIZE 4096
#define STRBUFFER_FACTOR 2 #define STRBUFFER_FACTOR 2
#define STRBUFFER_SIZE_MAX ((size_t)-1) #define STRBUFFER_SIZE_MAX ((size_t)-1)
@ -74,7 +74,8 @@ int strbuffer_append_byte(strbuffer_t *strbuff, char byte)
int strbuffer_append_bytes(strbuffer_t *strbuff, const char *data, size_t size) int strbuffer_append_bytes(strbuffer_t *strbuff, const char *data, size_t size)
{ {
if(size >= strbuff->size - strbuff->length) /* Leave room for EOL and NULL bytes */
if(size + 2 > strbuff->size - strbuff->length)
{ {
int backoff = 1; int backoff = 1;
size_t new_size; size_t new_size;

Loading…
Cancel
Save