Blame SOURCES/json-c-0.14-arraylist_optimizations.patch

a0bbcb
From 4a546e7b2f471157c6f479df1ef687864fcbd89e Mon Sep 17 00:00:00 2001
a0bbcb
From: Eric Haszlakiewicz <erh+git@nimenees.com>
a0bbcb
Date: Sun, 24 May 2020 03:53:32 +0000
a0bbcb
Subject: [PATCH] In arraylist, use malloc instead of calloc, avoid clearing
a0bbcb
 with memeset until we really need to, and micro-optimize array_list_add().
a0bbcb
a0bbcb
---
a0bbcb
 arraylist.c | 29 +++++++++++++++++++++++++----
a0bbcb
 1 file changed, 25 insertions(+), 4 deletions(-)
a0bbcb
a0bbcb
diff --git a/arraylist.c b/arraylist.c
a0bbcb
index e5524aca75..3e7bfa8950 100644
a0bbcb
--- a/arraylist.c
a0bbcb
+++ b/arraylist.c
a0bbcb
@@ -40,13 +40,13 @@ struct array_list *array_list_new(array_list_free_fn *free_fn)
a0bbcb
 {
a0bbcb
 	struct array_list *arr;
a0bbcb
 
a0bbcb
-	arr = (struct array_list *)calloc(1, sizeof(struct array_list));
a0bbcb
+	arr = (struct array_list *)malloc(sizeof(struct array_list));
a0bbcb
 	if (!arr)
a0bbcb
 		return NULL;
a0bbcb
 	arr->size = ARRAY_LIST_DEFAULT_SIZE;
a0bbcb
 	arr->length = 0;
a0bbcb
 	arr->free_fn = free_fn;
a0bbcb
-	if (!(arr->array = (void **)calloc(arr->size, sizeof(void *))))
a0bbcb
+	if (!(arr->array = (void **)malloc(arr->size * sizeof(void *))))
a0bbcb
 	{
a0bbcb
 		free(arr);
a0bbcb
 		return NULL;
a0bbcb
@@ -92,11 +92,11 @@ static int array_list_expand_internal(struct array_list *arr, size_t max)
a0bbcb
 	if (!(t = realloc(arr->array, new_size * sizeof(void *))))
a0bbcb
 		return -1;
a0bbcb
 	arr->array = (void **)t;
a0bbcb
-	(void)memset(arr->array + arr->size, 0, (new_size - arr->size) * sizeof(void *));
a0bbcb
 	arr->size = new_size;
a0bbcb
 	return 0;
a0bbcb
 }
a0bbcb
 
a0bbcb
+//static inline int _array_list_put_idx(struct array_list *arr, size_t idx, void *data)
a0bbcb
 int array_list_put_idx(struct array_list *arr, size_t idx, void *data)
a0bbcb
 {
a0bbcb
 	if (idx > SIZE_T_MAX - 1)
a0bbcb
@@ -106,6 +106,17 @@ int array_list_put_idx(struct array_list *arr, size_t idx, void *data)
a0bbcb
 	if (idx < arr->length && arr->array[idx])
a0bbcb
 		arr->free_fn(arr->array[idx]);
a0bbcb
 	arr->array[idx] = data;
a0bbcb
+	if (idx > arr->length)
a0bbcb
+	{
a0bbcb
+		/* Zero out the arraylist slots in between the old length
a0bbcb
+		   and the newly added entry so we know those entries are
a0bbcb
+		   empty.
a0bbcb
+		   e.g. when setting array[7] in an array that used to be 
a0bbcb
+		   only 5 elements longs, array[5] and array[6] need to be
a0bbcb
+		   set to 0.
a0bbcb
+		 */
a0bbcb
+		memset(arr->array + arr->length, 0, (idx - arr->length) * sizeof(void *));
a0bbcb
+	}
a0bbcb
 	if (arr->length <= idx)
a0bbcb
 		arr->length = idx + 1;
a0bbcb
 	return 0;
a0bbcb
@@ -113,7 +124,17 @@ int array_list_put_idx(struct array_list *arr, size_t idx, void *data)
a0bbcb
 
a0bbcb
 int array_list_add(struct array_list *arr, void *data)
a0bbcb
 {
a0bbcb
-	return array_list_put_idx(arr, arr->length, data);
a0bbcb
+	/* Repeat some of array_list_put_idx() so we can skip several
a0bbcb
+	   checks that we know are unnecessary when appending at the end
a0bbcb
+	 */
a0bbcb
+	size_t idx = arr->length;
a0bbcb
+	if (idx > SIZE_T_MAX - 1)
a0bbcb
+		return -1;
a0bbcb
+	if (array_list_expand_internal(arr, idx + 1))
a0bbcb
+		return -1;
a0bbcb
+	arr->array[idx] = data;
a0bbcb
+	arr->length++;
a0bbcb
+	return 0;
a0bbcb
 }
a0bbcb
 
a0bbcb
 void array_list_sort(struct array_list *arr, int (*compar)(const void *, const void *))