[njs] Avoiding OBJECT COPY instruction each time global object accessed.

Dmitry Volyntsev xeioex at nginx.com
Fri Oct 18 14:00:09 UTC 2019


details:   https://hg.nginx.org/njs/rev/10c10102cad0
branches:  
changeset: 1187:10c10102cad0
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Fri Oct 18 16:28:16 2019 +0300
description:
Avoiding OBJECT COPY instruction each time global object accessed.

diffstat:

 src/njs_builtin.c         |   8 +++++++-
 src/njs_generator.c       |   8 +++++---
 src/njs_lexer.h           |   4 ++--
 src/njs_parser_terminal.c |  11 +++--------
 src/njs_vm.c              |   5 ++++-
 src/njs_vm.h              |  10 ++++++++--
 6 files changed, 29 insertions(+), 17 deletions(-)

diffs (151 lines):

diff -r ed4472f545a1 -r 10c10102cad0 src/njs_builtin.c
--- a/src/njs_builtin.c	Sat Aug 24 05:41:46 2019 +0300
+++ b/src/njs_builtin.c	Fri Oct 18 16:28:16 2019 +0300
@@ -495,7 +495,7 @@ njs_prototype_function(njs_vm_t *vm, njs
  */
 
 njs_int_t
-njs_builtin_objects_clone(njs_vm_t *vm)
+njs_builtin_objects_clone(njs_vm_t *vm, njs_value_t *global)
 {
     size_t        size;
     njs_uint_t    i;
@@ -531,6 +531,12 @@ njs_builtin_objects_clone(njs_vm_t *vm)
         vm->constructors[i].object.__proto__ = function_prototype;
     }
 
+    vm->global_object = vm->shared->objects[0];
+    vm->global_object.__proto__ = &vm->prototypes[NJS_PROTOTYPE_OBJECT].object;
+    vm->global_object.shared = 0;
+
+    njs_set_object(global, &vm->global_object);
+
     vm->string_object = vm->shared->string_object;
     vm->string_object.__proto__ = &vm->prototypes[NJS_PROTOTYPE_STRING].object;
 
diff -r ed4472f545a1 -r 10c10102cad0 src/njs_generator.c
--- a/src/njs_generator.c	Sat Aug 24 05:41:46 2019 +0300
+++ b/src/njs_generator.c	Fri Oct 18 16:28:16 2019 +0300
@@ -442,9 +442,9 @@ njs_generate(njs_vm_t *vm, njs_generator
     case NJS_TOKEN_NON_LOCAL_THIS:
         return njs_generate_name(vm, generator, node);
 
-    case NJS_TOKEN_GLOBAL_THIS:
+    case NJS_TOKEN_GLOBAL_OBJECT:
         if (vm->options.module) {
-            node->index = njs_value_index(vm, &node->u.value,
+            node->index = njs_value_index(vm, &njs_value_undefined,
                                           generator->runtime);
             if (njs_fast_path(node->index != NJS_INDEX_NONE)) {
                 return NJS_OK;
@@ -453,7 +453,9 @@ njs_generate(njs_vm_t *vm, njs_generator
             return NJS_ERROR;
         }
 
-        /* Fall through. */
+        node->index = NJS_INDEX_GLOBAL_OBJECT;
+
+        return NJS_OK;
 
     case NJS_TOKEN_NJS:
     case NJS_TOKEN_PROCESS:
diff -r ed4472f545a1 -r 10c10102cad0 src/njs_lexer.h
--- a/src/njs_lexer.h	Sat Aug 24 05:41:46 2019 +0300
+++ b/src/njs_lexer.h	Fri Oct 18 16:28:16 2019 +0300
@@ -170,9 +170,9 @@ typedef enum {
     NJS_TOKEN_NON_LOCAL_THIS,
     NJS_TOKEN_ARGUMENTS,
 
-#define NJS_TOKEN_FIRST_OBJECT     NJS_TOKEN_GLOBAL_THIS
+#define NJS_TOKEN_FIRST_OBJECT     NJS_TOKEN_GLOBAL_OBJECT
 
-    NJS_TOKEN_GLOBAL_THIS,
+    NJS_TOKEN_GLOBAL_OBJECT,
     NJS_TOKEN_NJS,
     NJS_TOKEN_PROCESS,
     NJS_TOKEN_MATH,
diff -r ed4472f545a1 -r 10c10102cad0 src/njs_parser_terminal.c
--- a/src/njs_parser_terminal.c	Sat Aug 24 05:41:46 2019 +0300
+++ b/src/njs_parser_terminal.c	Fri Oct 18 16:28:16 2019 +0300
@@ -261,14 +261,9 @@ njs_parser_reference(njs_vm_t *vm, njs_p
             break;
         }
 
-        node->token = NJS_TOKEN_GLOBAL_THIS;
+        node->token = NJS_TOKEN_GLOBAL_OBJECT;
 
-        if (vm->options.module) {
-            njs_set_undefined(&node->u.value);
-            break;
-        }
-
-        /* Fall through. */
+        break;
 
     case NJS_TOKEN_NJS:
     case NJS_TOKEN_PROCESS:
@@ -652,7 +647,7 @@ njs_parser_object(njs_vm_t *vm, njs_pars
 
                 if (name.length == 0
                     || lexer->prev_token == NJS_TOKEN_THIS
-                    || lexer->prev_token == NJS_TOKEN_GLOBAL_THIS)
+                    || lexer->prev_token == NJS_TOKEN_GLOBAL_OBJECT)
                 {
                     return NJS_TOKEN_ILLEGAL;
                 }
diff -r ed4472f545a1 -r 10c10102cad0 src/njs_vm.c
--- a/src/njs_vm.c	Sat Aug 24 05:41:46 2019 +0300
+++ b/src/njs_vm.c	Fri Oct 18 16:28:16 2019 +0300
@@ -326,6 +326,7 @@ njs_vm_init(njs_vm_t *vm)
     u_char       *values;
     njs_int_t    ret;
     njs_arr_t    *backtrace;
+    njs_value_t  *global;
     njs_frame_t  *frame;
 
     scope_size = vm->scope_size + NJS_INDEX_GLOBAL_OFFSET;
@@ -362,7 +363,9 @@ njs_vm_init(njs_vm_t *vm)
         return NJS_ERROR;
     }
 
-    ret = njs_builtin_objects_clone(vm);
+    global = (njs_value_t *) (values + NJS_INDEX_GLOBAL_OBJECT_OFFSET);
+
+    ret = njs_builtin_objects_clone(vm, global);
     if (njs_slow_path(ret != NJS_OK)) {
         return NJS_ERROR;
     }
diff -r ed4472f545a1 -r 10c10102cad0 src/njs_vm.h
--- a/src/njs_vm.h	Sat Aug 24 05:41:46 2019 +0300
+++ b/src/njs_vm.h	Fri Oct 18 16:28:16 2019 +0300
@@ -203,7 +203,12 @@ enum njs_function_e {
 #define NJS_INDEX_OBJECT_MEMORY_ERROR                                         \
     njs_global_scope_index(NJS_CONSTRUCTOR_MEMORY_ERROR)
 
-#define NJS_INDEX_GLOBAL_RETVAL  njs_global_scope_index(NJS_CONSTRUCTOR_MAX)
+#define NJS_INDEX_GLOBAL_OBJECT  njs_global_scope_index(NJS_CONSTRUCTOR_MAX)
+#define NJS_INDEX_GLOBAL_OBJECT_OFFSET                                        \
+    njs_scope_index(NJS_CONSTRUCTOR_MAX, 0)
+
+#define NJS_INDEX_GLOBAL_RETVAL                                               \
+    njs_global_scope_index(NJS_CONSTRUCTOR_MAX + 1)
 #define NJS_INDEX_GLOBAL_OFFSET  njs_scope_index(NJS_CONSTRUCTOR_MAX + 1, 0)
 
 
@@ -284,6 +289,7 @@ struct njs_vm_s {
     njs_object_t             memory_error_object;
 
     njs_object_t             string_object;
+    njs_object_t             global_object;
 
     njs_arr_t                *codes;  /* of njs_vm_code_t */
 
@@ -341,7 +347,7 @@ void njs_vm_scopes_restore(njs_vm_t *vm,
 njs_int_t njs_vm_add_backtrace_entry(njs_vm_t *vm, njs_frame_t *frame);
 
 njs_int_t njs_builtin_objects_create(njs_vm_t *vm);
-njs_int_t njs_builtin_objects_clone(njs_vm_t *vm);
+njs_int_t njs_builtin_objects_clone(njs_vm_t *vm, njs_value_t *global);
 njs_int_t njs_builtin_match_native_function(njs_vm_t *vm,
     njs_function_t *function, njs_str_t *name);
 


More information about the nginx-devel mailing list