From: Dmitry Volyntsev Date: Tue, 16 Jun 2020 13:55:25 +0000 (+0000) Subject: Optimizing njs_native_frame_t structure. X-Git-Tag: 0.4.2~25 X-Git-Url: http://www.kaiwu.me/postgresql/commit/static/gitweb.js?a=commitdiff_plain;h=827f664cd0c8ee3e34f524b44da5a5cd8f7769cc;p=njs.git Optimizing njs_native_frame_t structure. Moving njs_exception_t from njs_native_frame_t to njs_frame_t, as the exception structure is only needed for lambda frames. --- diff --git a/src/njs_function.c b/src/njs_function.c index 66a70fad..ddb41771 100644 --- a/src/njs_function.c +++ b/src/njs_function.c @@ -370,6 +370,7 @@ njs_function_native_frame(njs_vm_t *vm, njs_function_t *function, frame->function = function; frame->nargs = function->args_offset + nargs; frame->ctor = ctor; + frame->native = 1; value = (njs_value_t *) ((u_char *) frame + NJS_NATIVE_FRAME_SIZE); frame->arguments = value; @@ -452,6 +453,7 @@ njs_function_lambda_frame(njs_vm_t *vm, njs_function_t *function, native_frame->function = target; native_frame->nargs = nargs; native_frame->ctor = ctor; + native_frame->native = 0; /* Function arguments. */ @@ -501,6 +503,8 @@ njs_function_lambda_frame(njs_vm_t *vm, njs_function_t *function, } frame = (njs_frame_t *) native_frame; + frame->exception.catch = NULL; + frame->exception.next = NULL; frame->local = value; frame->previous_active_frame = vm->active_frame; diff --git a/src/njs_function.h b/src/njs_function.h index 159f47ce..95d9d6ab 100644 --- a/src/njs_function.h +++ b/src/njs_function.h @@ -42,18 +42,6 @@ struct njs_function_lambda_s { #define NJS_FRAME_SPARE_SIZE 512 -typedef struct njs_exception_s njs_exception_t; - -struct njs_exception_s { - /* - * The next field must be the first to alias it with restart address - * because it is not used to detect catch block existance in the frame. - */ - njs_exception_t *next; - u_char *catch; -}; - - struct njs_native_frame_s { u_char *free; @@ -63,13 +51,13 @@ struct njs_native_frame_s { njs_value_t *arguments; njs_object_t *arguments_object; - njs_exception_t exception; njs_index_t retval; uint32_t size; uint32_t free_size; uint32_t nargs; + uint8_t native; /* 1 bit */ /* Function is called as constructor with "new" keyword. */ uint8_t ctor; /* 1 bit */ @@ -78,9 +66,19 @@ struct njs_native_frame_s { }; +typedef struct njs_exception_s njs_exception_t; + +struct njs_exception_s { + njs_exception_t *next; + u_char *catch; +}; + + struct njs_frame_s { njs_native_frame_t native; + njs_exception_t exception; + njs_frame_t *previous_active_frame; njs_value_t *local; @@ -169,7 +167,7 @@ njs_function_frame_invoke(njs_vm_t *vm, njs_index_t retval) frame = vm->top_frame; frame->retval = retval; - if (frame->function->native) { + if (frame->native) { return njs_function_native_call(vm); } else { diff --git a/src/njs_vmcode.c b/src/njs_vmcode.c index 67078543..f2eb831a 100644 --- a/src/njs_vmcode.c +++ b/src/njs_vmcode.c @@ -90,7 +90,7 @@ njs_vmcode_interpreter(njs_vm_t *vm, u_char *pc) njs_frame_t *frame; njs_jump_off_t ret; njs_vmcode_this_t *this; - njs_native_frame_t *previous; + njs_native_frame_t *previous, *native; njs_property_next_t *next; njs_vmcode_generic_t *vmcode; njs_vmcode_prop_get_t *get; @@ -867,8 +867,8 @@ next: ret = njs_vmcode_try_end(vm, value1, value2); } else { - vm->top_frame->exception.catch = - pc + (njs_jump_off_t) value2; + frame = (njs_frame_t *) vm->top_frame; + frame->exception.catch = pc + (njs_jump_off_t) value2; ret = sizeof(njs_vmcode_catch_t); } @@ -906,28 +906,31 @@ error: } for ( ;; ) { - frame = (njs_frame_t *) vm->top_frame; + native = vm->top_frame; - catch = frame->native.exception.catch; + if (!native->native) { + frame = (njs_frame_t *) native; + catch = frame->exception.catch; - if (catch != NULL) { - pc = catch; + if (catch != NULL) { + pc = catch; - goto next; + goto next; + } } - previous = frame->native.previous; + previous = native->previous; if (previous == NULL) { break; } - lambda_call = (frame == vm->active_frame); + lambda_call = (native == &vm->active_frame->native); - njs_vm_scopes_restore(vm, &frame->native, previous); + njs_vm_scopes_restore(vm, native, previous); - if (frame->native.size != 0) { - vm->stack_size -= frame->native.size; - njs_mp_free(vm->mem_pool, frame); + if (native->size != 0) { + vm->stack_size -= native->size; + njs_mp_free(vm->mem_pool, native); } if (lambda_call) { @@ -1715,21 +1718,24 @@ njs_vmcode_try_start(njs_vm_t *vm, njs_value_t *exception_value, njs_value_t *offset, u_char *pc) { njs_value_t *exit_value; + njs_frame_t *frame; njs_exception_t *e; njs_vmcode_try_start_t *try_start; - if (vm->top_frame->exception.catch != NULL) { + frame = (njs_frame_t *) vm->top_frame; + + if (frame->exception.catch != NULL) { e = njs_mp_alloc(vm->mem_pool, sizeof(njs_exception_t)); if (njs_slow_path(e == NULL)) { njs_memory_error(vm); return NJS_ERROR; } - *e = vm->top_frame->exception; - vm->top_frame->exception.next = e; + *e = frame->exception; + frame->exception.next = e; } - vm->top_frame->exception.catch = pc + (njs_jump_off_t) offset; + frame->exception.catch = pc + (njs_jump_off_t) offset; njs_set_invalid(exception_value); @@ -1784,15 +1790,17 @@ njs_vmcode_try_continue(njs_vm_t *vm, njs_value_t *exit_value, static njs_jump_off_t njs_vmcode_try_end(njs_vm_t *vm, njs_value_t *invld, njs_value_t *offset) { + njs_frame_t *frame; njs_exception_t *e; - e = vm->top_frame->exception.next; + frame = (njs_frame_t *) vm->top_frame; + e = frame->exception.next; if (e == NULL) { - vm->top_frame->exception.catch = NULL; + frame->exception.catch = NULL; } else { - vm->top_frame->exception = *e; + frame->exception = *e; njs_mp_free(vm->mem_pool, e); }