[njs] Optimizing njs_native_frame_t structure.

Dmitry Volyntsev xeioex at nginx.com
Wed Jun 17 14:40:09 UTC 2020


details:   https://hg.nginx.org/njs/rev/d37766e94c82
branches:  
changeset: 1433:d37766e94c82
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Tue Jun 16 13:55:25 2020 +0000
description:
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.

diffstat:

 src/njs_function.c |   4 ++++
 src/njs_function.h |  26 ++++++++++++--------------
 src/njs_vmcode.c   |  50 +++++++++++++++++++++++++++++---------------------
 3 files changed, 45 insertions(+), 35 deletions(-)

diffs (210 lines):

diff -r 3bfa26b91a55 -r d37766e94c82 src/njs_function.c
--- a/src/njs_function.c	Mon Jun 15 18:33:57 2020 +0300
+++ b/src/njs_function.c	Tue Jun 16 13:55:25 2020 +0000
@@ -370,6 +370,7 @@ njs_function_native_frame(njs_vm_t *vm, 
     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, 
     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, 
     }
 
     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 -r 3bfa26b91a55 -r d37766e94c82 src/njs_function.h
--- a/src/njs_function.h	Mon Jun 15 18:33:57 2020 +0300
+++ b/src/njs_function.h	Tue Jun 16 13:55:25 2020 +0000
@@ -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, 
     frame = vm->top_frame;
     frame->retval = retval;
 
-    if (frame->function->native) {
+    if (frame->native) {
         return njs_function_native_call(vm);
 
     } else {
diff -r 3bfa26b91a55 -r d37766e94c82 src/njs_vmcode.c
--- a/src/njs_vmcode.c	Mon Jun 15 18:33:57 2020 +0300
+++ b/src/njs_vmcode.c	Tue Jun 16 13:55:25 2020 +0000
@@ -90,7 +90,7 @@ njs_vmcode_interpreter(njs_vm_t *vm, u_c
     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_v
     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, nj
 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);
     }
 


More information about the nginx-devel mailing list