Welcome! Log In Create A New Profile

Advanced

[njs] Improved next index allocation.

Dmitry Volyntsev
January 29, 2019 07:02AM
details: https://hg.nginx.org/njs/rev/b4b1280ae790
branches:
changeset: 739:b4b1280ae790
user: hongzhidao <hongzhidao@gmail.com>
date: Sun Jan 27 05:21:32 2019 +0800
description:
Improved next index allocation.

diffstat:

njs/njs_generator.c | 33 +----------------
njs/njs_variable.c | 97 ++++++++++++++++++++++++++++++----------------------
njs/njs_variable.h | 2 +
3 files changed, 60 insertions(+), 72 deletions(-)

diffs (187 lines):

diff -r e98be67a10e2 -r b4b1280ae790 njs/njs_generator.c
--- a/njs/njs_generator.c Mon Jan 28 21:18:43 2019 +0300
+++ b/njs/njs_generator.c Sun Jan 27 05:21:32 2019 +0800
@@ -3053,8 +3053,7 @@ njs_generate_temp_index_get(njs_vm_t *vm
njs_parser_node_t *node)
{
nxt_array_t *cache;
- njs_value_t *value;
- njs_index_t index, *last;
+ njs_index_t *last;
njs_parser_scope_t *scope;

cache = generator->index_cache;
@@ -3073,34 +3072,8 @@ njs_generate_temp_index_get(njs_vm_t *vm
scope = scope->parent;
}

- if (vm->options.accumulative && scope->type == NJS_SCOPE_GLOBAL) {
- /*
- * When non-clonable VM runs in accumulative mode
- * all global variables are allocated in absolute scope
- * to simplify global scope handling.
- */
- value = nxt_mp_align(vm->mem_pool, sizeof(njs_value_t),
- sizeof(njs_value_t));
- if (nxt_slow_path(value == NULL)) {
- return NJS_INDEX_ERROR;
- }
-
- index = (njs_index_t) value;
-
- } else {
- value = nxt_array_add(scope->values[0], &njs_array_mem_proto,
- vm->mem_pool);
- if (nxt_slow_path(value == NULL)) {
- return NJS_INDEX_ERROR;
- }
-
- index = scope->next_index[0];
- scope->next_index[0] += sizeof(njs_value_t);
- }
-
- *value = njs_value_invalid;
-
- return index;
+ return njs_scope_next_index(vm, scope, NJS_SCOPE_INDEX_LOCAL,
+ &njs_value_invalid);
}


diff -r e98be67a10e2 -r b4b1280ae790 njs/njs_variable.c
--- a/njs/njs_variable.c Mon Jan 28 21:18:43 2019 +0300
+++ b/njs/njs_variable.c Sun Jan 27 05:21:32 2019 +0800
@@ -286,10 +286,9 @@ njs_variable_resolve(njs_vm_t *vm, njs_p
{
nxt_int_t ret;
nxt_uint_t scope_index;
- nxt_array_t *values;
njs_index_t index;
- njs_value_t *value;
njs_variable_t *var;
+ const njs_value_t *default_value;
njs_variable_reference_t *vr;

vr = &node->u.reference;
@@ -331,48 +330,12 @@ njs_variable_resolve(njs_vm_t *vm, njs_p
goto not_found;
}

- if (vm->options.accumulative && vr->scope->type == NJS_SCOPE_GLOBAL) {
- /*
- * When non-clonable VM runs in accumulative mode all
- * global variables should be allocated in absolute scope
- * to share them among consecutive VM invocations.
- */
- value = nxt_mp_align(vm->mem_pool, sizeof(njs_value_t),
- sizeof(njs_value_t));
- if (nxt_slow_path(value == NULL)) {
- njs_memory_error(vm);
- return NULL;
- }
-
- index = (njs_index_t) value;
-
- } else {
- values = vr->scope->values[scope_index];
+ default_value = njs_is_object(&var->value) ? &var->value : &njs_value_void;

- if (values == NULL) {
- values = nxt_array_create(4, sizeof(njs_value_t),
- &njs_array_mem_proto, vm->mem_pool);
- if (nxt_slow_path(values == NULL)) {
- return NULL;
- }
-
- vr->scope->values[scope_index] = values;
- }
+ index = njs_scope_next_index(vm, vr->scope, scope_index, default_value);

- value = nxt_array_add(values, &njs_array_mem_proto, vm->mem_pool);
- if (nxt_slow_path(value == NULL)) {
- return NULL;
- }
-
- index = vr->scope->next_index[scope_index];
- vr->scope->next_index[scope_index] += sizeof(njs_value_t);
- }
-
- if (njs_is_object(&var->value)) {
- *value = var->value;
-
- } else {
- *value = njs_value_void;
+ if (nxt_slow_path(index == NJS_INDEX_ERROR)) {
+ return NULL;
}

var->index = index;
@@ -448,6 +411,56 @@ njs_variable_reference_resolve(njs_vm_t
}


+njs_index_t
+njs_scope_next_index(njs_vm_t *vm, njs_parser_scope_t *scope,
+ nxt_uint_t scope_index, const njs_value_t *default_value)
+{
+ njs_index_t index;
+ njs_value_t *value;
+ nxt_array_t *values;
+
+ if (vm->options.accumulative && scope->type == NJS_SCOPE_GLOBAL) {
+ /*
+ * When non-clonable VM runs in accumulative mode all
+ * global variables should be allocated in absolute scope
+ * to share them among consecutive VM invocations.
+ */
+ value = nxt_mp_align(vm->mem_pool, sizeof(njs_value_t),
+ sizeof(njs_value_t));
+ if (nxt_slow_path(value == NULL)) {
+ return NJS_INDEX_ERROR;
+ }
+
+ index = (njs_index_t) value;
+
+ } else {
+ values = scope->values[scope_index];
+
+ if (values == NULL) {
+ values = nxt_array_create(4, sizeof(njs_value_t),
+ &njs_array_mem_proto, vm->mem_pool);
+ if (nxt_slow_path(values == NULL)) {
+ return NJS_INDEX_ERROR;
+ }
+
+ scope->values[scope_index] = values;
+ }
+
+ value = nxt_array_add(values, &njs_array_mem_proto, vm->mem_pool);
+ if (nxt_slow_path(value == NULL)) {
+ return NJS_INDEX_ERROR;
+ }
+
+ index = scope->next_index[scope_index];
+ scope->next_index[scope_index] += sizeof(njs_value_t);
+ }
+
+ *value = *default_value;
+
+ return index;
+}
+
+
static njs_variable_t *
njs_variable_alloc(njs_vm_t *vm, nxt_str_t *name, njs_variable_type_t type)
{
diff -r e98be67a10e2 -r b4b1280ae790 njs/njs_variable.h
--- a/njs/njs_variable.h Mon Jan 28 21:18:43 2019 +0300
+++ b/njs/njs_variable.h Sun Jan 27 05:21:32 2019 +0800
@@ -59,6 +59,8 @@ njs_ret_t njs_variable_reference(njs_vm_
njs_reference_type_t type);
njs_ret_t njs_variables_scope_reference(njs_vm_t *vm,
njs_parser_scope_t *scope);
+njs_index_t njs_scope_next_index(njs_vm_t *vm, njs_parser_scope_t *scope,
+ nxt_uint_t scope_index, const njs_value_t *default_value);
njs_ret_t njs_name_copy(njs_vm_t *vm, nxt_str_t *dst, nxt_str_t *src);

extern const nxt_lvlhsh_proto_t njs_variables_hash_proto;
_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

[njs] Improved next index allocation.

Dmitry Volyntsev 259 January 29, 2019 07:02AM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 111
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready