[njs] njs_array_realloc() has been changed to njs_array_expand().

Igor Sysoev igor at sysoev.ru
Mon Oct 24 12:03:52 UTC 2016


details:   http://hg.nginx.org/njs/rev/50555ae1d978
branches:  
changeset: 213:50555ae1d978
user:      Igor Sysoev <igor at sysoev.ru>
date:      Mon Oct 24 14:56:28 2016 +0300
description:
njs_array_realloc() has been changed to njs_array_expand().

diffstat:

 njs/njs_array.c  |  58 +++++++++++++++++++++++++++----------------------------
 njs/njs_array.h  |   2 +-
 njs/njs_string.c |   8 ++----
 njs/njs_vm.c     |  17 ++++++---------
 4 files changed, 39 insertions(+), 46 deletions(-)

diffs (178 lines):

diff -r bf848210269c -r 50555ae1d978 njs/njs_array.c
--- a/njs/njs_array.c	Mon Oct 24 14:12:12 2016 +0300
+++ b/njs/njs_array.c	Mon Oct 24 14:56:28 2016 +0300
@@ -148,17 +148,14 @@ njs_array_add(njs_vm_t *vm, njs_array_t 
 {
     njs_ret_t  ret;
 
-    if (array->size == array->length) {
-        ret = njs_array_realloc(vm, array, 0, array->size + 1);
-        if (nxt_slow_path(ret != NXT_OK)) {
-            return ret;
-        }
+    ret = njs_array_expand(vm, array, 0, 1);
+
+    if (nxt_fast_path(ret == NXT_OK)) {
+        /* GC: retain value. */
+        array->start[array->length++] = *value;
     }
 
-    /* GC: retain value. */
-    array->start[array->length++] = *value;
-
-    return NXT_OK;
+    return ret;
 }
 
 
@@ -168,31 +165,34 @@ njs_array_string_add(njs_vm_t *vm, njs_a
 {
     njs_ret_t  ret;
 
-    if (array->size == array->length) {
-        ret = njs_array_realloc(vm, array, 0, array->size + 1);
-        if (nxt_slow_path(ret != NXT_OK)) {
-            return ret;
-        }
+    ret = njs_array_expand(vm, array, 0, 1);
+
+    if (nxt_fast_path(ret == NXT_OK)) {
+        return njs_string_create(vm, &array->start[array->length++],
+                                 start, size, length);
     }
 
-    return njs_string_create(vm, &array->start[array->length++],
-                            start, size, length);
+    return ret;
 }
 
 
 njs_ret_t
-njs_array_realloc(njs_vm_t *vm, njs_array_t *array, uint32_t prepend,
+njs_array_expand(njs_vm_t *vm, njs_array_t *array, uint32_t prepend,
     uint32_t size)
 {
     njs_value_t  *start, *old;
 
-    if (size != array->size) {
-        if (size < 16) {
-            size *= 2;
+    size += array->length;
 
-        } else {
-            size += size / 2;
-        }
+    if (nxt_fast_path(size <= array->size && prepend == 0)) {
+        return NXT_OK;
+    }
+
+    if (size < 16) {
+        size *= 2;
+
+    } else {
+        size += size / 2;
     }
 
     start = nxt_mem_cache_align(vm->mem_cache_pool, sizeof(njs_value_t),
@@ -438,11 +438,9 @@ njs_array_prototype_push(njs_vm_t *vm, n
         array = args[0].data.u.array;
 
         if (nargs != 0) {
-            if (nargs > array->size - array->length) {
-                ret = njs_array_realloc(vm, array, 0, array->size + nargs);
-                if (nxt_slow_path(ret != NXT_OK)) {
-                    return ret;
-                }
+            ret = njs_array_expand(vm, array, 0, nargs);
+            if (nxt_slow_path(ret != NXT_OK)) {
+                return ret;
             }
 
             for (i = 1; i < nargs; i++) {
@@ -500,7 +498,7 @@ njs_array_prototype_unshift(njs_vm_t *vm
 
         if (n != 0) {
             if ((intptr_t) n > (array->start - array->data)) {
-                ret = njs_array_realloc(vm, array, n, 0);
+                ret = njs_array_expand(vm, array, n, 0);
                 if (nxt_slow_path(ret != NXT_OK)) {
                     return ret;
                 }
@@ -627,7 +625,7 @@ njs_array_prototype_splice(njs_vm_t *vm,
              * Index of the first item is in "n".
              */
             if (delta > 0) {
-                ret = njs_array_realloc(vm, array, 0, array->size + delta);
+                ret = njs_array_expand(vm, array, 0, delta);
                 if (nxt_slow_path(ret != NXT_OK)) {
                     return ret;
                 }
diff -r bf848210269c -r 50555ae1d978 njs/njs_array.h
--- a/njs/njs_array.h	Mon Oct 24 14:12:12 2016 +0300
+++ b/njs/njs_array.h	Mon Oct 24 14:56:28 2016 +0300
@@ -14,7 +14,7 @@
 njs_array_t *njs_array_alloc(njs_vm_t *vm, uint32_t length, uint32_t spare);
 njs_ret_t njs_array_string_add(njs_vm_t *vm, njs_array_t *array, u_char *start,
     size_t size, size_t length);
-njs_ret_t njs_array_realloc(njs_vm_t *vm, njs_array_t *array, uint32_t prepend,
+njs_ret_t njs_array_expand(njs_vm_t *vm, njs_array_t *array, uint32_t prepend,
     uint32_t size);
 njs_ret_t njs_array_constructor(njs_vm_t *vm, njs_value_t *args,
     nxt_uint_t nargs, njs_index_t unused);
diff -r bf848210269c -r 50555ae1d978 njs/njs_string.c
--- a/njs/njs_string.c	Mon Oct 24 14:12:12 2016 +0300
+++ b/njs/njs_string.c	Mon Oct 24 14:56:28 2016 +0300
@@ -1659,11 +1659,9 @@ njs_string_match_multiple(njs_vm_t *vm, 
                                    string.size, vm->single_match_data);
             if (ret >= 0) {
                 if (array != NULL) {
-                    if (array->length == array->size) {
-                        ret = njs_array_realloc(vm, array, 0, array->size + 1);
-                        if (nxt_slow_path(ret != NXT_OK)) {
-                            return ret;
-                        }
+                    ret = njs_array_expand(vm, array, 0, 1);
+                    if (nxt_slow_path(ret != NXT_OK)) {
+                        return ret;
                     }
 
                 } else {
diff -r bf848210269c -r 50555ae1d978 njs/njs_vm.c
--- a/njs/njs_vm.c	Mon Oct 24 14:12:12 2016 +0300
+++ b/njs/njs_vm.c	Mon Oct 24 14:56:28 2016 +0300
@@ -1047,28 +1047,25 @@ static njs_ret_t
 njs_array_property_query(njs_vm_t *vm, njs_property_query_t *pq,
     njs_value_t *object, int32_t index)
 {
-    size_t       size;
+    int32_t      size;
     njs_ret_t    ret;
     njs_value_t  *value;
     njs_array_t  *array;
 
     array = object->data.u.array;
-
-    if ((uint32_t) index >= array->length) {
-
+    size = index - array->length;
+
+    if (size >= 0) {
         if (pq->query != NJS_PROPERTY_QUERY_SET) {
             return NXT_DECLINED;
         }
 
-        if ((uint32_t) index >= array->size) {
-            ret = njs_array_realloc(vm, array, 0, index + 1);
-            if (nxt_slow_path(ret != NXT_OK)) {
-                return ret;
-            }
+        ret = njs_array_expand(vm, array, 0, size + 1);
+        if (nxt_slow_path(ret != NXT_OK)) {
+            return ret;
         }
 
         value = &array->start[array->length];
-        size = index - array->length;
 
         while (size != 0) {
             njs_set_invalid(value);



More information about the nginx-devel mailing list