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) 2015 Con Kolivas <kernel@kolivas.org>
*
* Jansson is free software; you can redistribute it and/or modify
* 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)
{
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))
result = NULL;
else if (flags & JSON_EOL)
result = jsonp_eolstrsteal(&strbuff);
else
result = json_dump_dup(strbuffer_value(&strbuff), flags);
result = jsonp_strsteal(&strbuff);
strbuffer_close(&strbuff);
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_strdup(const char *str);
char *jsonp_eolstrdup(const char *str);
char *jsonp_strsteal(strbuffer_t *strbuff);
char *jsonp_eolstrsteal(strbuffer_t *strbuff);
/* Windows compatibility */
#ifdef _WIN32

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

@ -51,23 +51,22 @@ char *jsonp_strdup(const char *str)
return new_str;
}
char *jsonp_eolstrdup(const char *str)
char *jsonp_strsteal(strbuffer_t *strbuff)
{
char *new_str;
size_t len;
size_t len = strbuff->length + 1;
char *ret = realloc(strbuff->value, len);
len = strlen(str);
if(len == (size_t)-1)
return NULL;
return ret;
}
new_str = jsonp_malloc(len + 2);
if(!new_str)
return NULL;
char *jsonp_eolstrsteal(strbuffer_t *strbuff)
{
size_t len = strbuff->length + 2;
char *ret = realloc(strbuff->value, len);
memcpy(new_str, str, len);
new_str[len] = '\n';
new_str[len + 1] = '\0';
return new_str;
ret[strbuff->length] = '\n';
ret[strbuff->length + 1] = '\0';
return ret;
}
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 "strbuffer.h"
#define STRBUFFER_MIN_SIZE 16
#define STRBUFFER_MIN_SIZE 4096
#define STRBUFFER_FACTOR 2
#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)
{
if(size >= strbuff->size - strbuff->length)
/* Leave room for EOL and NULL bytes */
if(size + 2 > strbuff->size - strbuff->length)
{
int backoff = 1;
size_t new_size;

Loading…
Cancel
Save