Add json_object_array_shrink() (and array_list_shrink()) and use it in json_tokener to minimize the amount of memory used. This results in a 39%-50% reduction in memory use (peak RSS, peak heap usage) on the jc-bench benchmark and 9% shorter runtime.

Also add the json_object_new_array_ext, array_list_new2, and array_list_shrink functions.
This commit is contained in:
Eric Haszlakiewicz
2020-06-20 18:03:04 +00:00
parent 99bb2121c6
commit e26a1195f4
6 changed files with 95 additions and 3 deletions

View File

@@ -37,13 +37,18 @@
#include "arraylist.h"
struct array_list *array_list_new(array_list_free_fn *free_fn)
{
return array_list_new2(free_fn, ARRAY_LIST_DEFAULT_SIZE);
}
struct array_list *array_list_new2(array_list_free_fn *free_fn, int initial_size)
{
struct array_list *arr;
arr = (struct array_list *)malloc(sizeof(struct array_list));
if (!arr)
return NULL;
arr->size = ARRAY_LIST_DEFAULT_SIZE;
arr->size = initial_size;
arr->length = 0;
arr->free_fn = free_fn;
if (!(arr->array = (void **)malloc(arr->size * sizeof(void *))))
@@ -96,6 +101,26 @@ static int array_list_expand_internal(struct array_list *arr, size_t max)
return 0;
}
int array_list_shrink(struct array_list *arr, size_t empty_slots)
{
void *t;
size_t new_size;
new_size = arr->length + empty_slots;
if (new_size == arr->size)
return 0;
if (new_size > arr->size)
return array_list_expand_internal(arr, new_size);
if (new_size == 0)
new_size = 1;
if (!(t = realloc(arr->array, new_size * sizeof(void *))))
return -1;
arr->array = (void **)t;
arr->size = new_size;
return 0;
}
//static inline int _array_list_put_idx(struct array_list *arr, size_t idx, void *data)
int array_list_put_idx(struct array_list *arr, size_t idx, void *data)
{
@@ -165,6 +190,8 @@ int array_list_del_idx(struct array_list *arr, size_t idx, size_t count)
return -1;
for (i = idx; i < stop; ++i)
{
// Because put_idx can skip entries, we need to check if
// there's actually anything in each slot we're erasing.
if (arr->array[i])
arr->free_fn(arr->array[i]);
}