From: hongzhidao Date: Sat, 26 Jan 2019 21:21:32 +0000 (+0800) Subject: Improved next index allocation. X-Git-Tag: 0.2.8~63 X-Git-Url: http://git.kaiwu.me/postgresql/log/contrib/postgres_fdw/static/gitweb.js?a=commitdiff_plain;h=3ae3d36ba48cba9536c5c6a5f0f16dc13f0ab74b;p=njs.git Improved next index allocation. --- diff --git a/njs/njs_generator.c b/njs/njs_generator.c index 9b392e6b..5856ec2b 100644 --- a/njs/njs_generator.c +++ b/njs/njs_generator.c @@ -3053,8 +3053,7 @@ njs_generate_temp_index_get(njs_vm_t *vm, njs_generator_t *generator, 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, njs_generator_t *generator, 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 --git a/njs/njs_variable.c b/njs/njs_variable.c index a3b44f8d..105d2845 100644 --- a/njs/njs_variable.c +++ b/njs/njs_variable.c @@ -286,10 +286,9 @@ njs_variable_resolve(njs_vm_t *vm, njs_parser_node_t *node) { 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_parser_node_t *node) 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; + default_value = njs_is_object(&var->value) ? &var->value : &njs_value_void; - } else { - values = vr->scope->values[scope_index]; + index = njs_scope_next_index(vm, vr->scope, scope_index, default_value); - 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; - } - - 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 *vm, njs_variable_reference_t *vr, } +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 --git a/njs/njs_variable.h b/njs/njs_variable.h index 78e137f1..a812b9d5 100644 --- a/njs/njs_variable.h +++ b/njs/njs_variable.h @@ -59,6 +59,8 @@ njs_ret_t njs_variable_reference(njs_vm_t *vm, njs_parser_scope_t *scope, 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;