From 5d114c76b3b70165c00c32dd3504e0271f52cd3a Mon Sep 17 00:00:00 2001 From: Valentin Bartenev Date: Sat, 1 Dec 2018 22:32:33 +0300 Subject: [PATCH] Slightly optimized njs_object_keys_array(). There is no need to iterate over the object properties second time if after the first attempt we know that it contains no enumerable properties. Also removed surplus conditions. --- njs/njs_object.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/njs/njs_object.c b/njs/njs_object.c index 09417bfa..7942ed6e 100644 --- a/njs/njs_object.c +++ b/njs/njs_object.c @@ -888,7 +888,7 @@ njs_object_keys(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, njs_array_t * njs_object_keys_array(njs_vm_t *vm, const njs_value_t *value) { - uint32_t i, n, length, keys_length; + uint32_t i, n, length, keys_length, properties; njs_value_t *string; njs_array_t *keys, *array; nxt_lvlhsh_t *hash; @@ -930,15 +930,14 @@ njs_object_keys_array(njs_vm_t *vm, const njs_value_t *value) break; } + /* GCC 4 and Clang 3 complain about uninitialized hash. */ + hash = NULL; + properties = 0; + if (nxt_fast_path(njs_is_object(value))) { nxt_lvlhsh_each_init(&lhe, &njs_object_hash_proto); hash = &value->data.u.object->hash; - } else { - hash = NULL; - } - - if (nxt_fast_path(hash != NULL)) { for ( ;; ) { prop = nxt_lvlhsh_each(hash, &lhe); @@ -947,9 +946,11 @@ njs_object_keys_array(njs_vm_t *vm, const njs_value_t *value) } if (prop->type != NJS_WHITEOUT && prop->enumerable) { - keys_length++; + properties++; } } + + keys_length += properties; } keys = njs_array_alloc(vm, keys_length, NJS_ARRAY_SPARE); @@ -966,13 +967,13 @@ njs_object_keys_array(njs_vm_t *vm, const njs_value_t *value) } } - } else if (length != 0) { + } else { for (i = 0; i < length; i++) { njs_uint32_to_string(&keys->start[n++], i); } } - if (nxt_fast_path(hash != NULL)) { + if (nxt_fast_path(properties != 0)) { nxt_lvlhsh_each_init(&lhe, &njs_object_hash_proto); for ( ;; ) { -- 2.47.3