[njs] Now the empty regexp pattern is created on compile stage only.

Igor Sysoev igor at sysoev.ru
Fri Nov 11 14:38:45 UTC 2016


details:   http://hg.nginx.org/njs/rev/9f66db825663
branches:  
changeset: 252:9f66db825663
user:      Igor Sysoev <igor at sysoev.ru>
date:      Fri Nov 11 17:28:47 2016 +0300
description:
Now the empty regexp pattern is created on compile stage only.

This speeds up njs_vm_clone() operation twice.

diffstat:

 njs/njs_builtin.c |   2 +-
 njs/njs_regexp.c  |  31 +++++++++++++++----------------
 njs/njs_string.c  |  23 ++++++++++++++---------
 njs/njs_vm.h      |   3 ++-
 njs/njscript.c    |  13 +++++++++++--
 5 files changed, 43 insertions(+), 29 deletions(-)

diffs (164 lines):

diff -r 0939df226d5c -r 9f66db825663 njs/njs_builtin.c
--- a/njs/njs_builtin.c	Thu Nov 10 19:09:13 2016 +0300
+++ b/njs/njs_builtin.c	Fri Nov 11 17:28:47 2016 +0300
@@ -230,7 +230,7 @@ njs_builtin_objects_create(njs_vm_t *vm)
     }
 
     prototypes[NJS_PROTOTYPE_REGEXP].regexp.pattern =
-                                     vm->empty_regexp.data.u.regexp->pattern;
+                                              vm->shared->empty_regexp_pattern;
 
     constructors = vm->shared->constructors;
 
diff -r 0939df226d5c -r 9f66db825663 njs/njs_regexp.c
--- a/njs/njs_regexp.c	Thu Nov 10 19:09:13 2016 +0300
+++ b/njs/njs_regexp.c	Fri Nov 11 17:28:47 2016 +0300
@@ -65,8 +65,7 @@ njs_regexp_init(njs_vm_t *vm)
 
     vm->regex_context->trace = &vm->trace;
 
-    return njs_regexp_create(vm, &vm->empty_regexp, (u_char *) "(?:)",
-                             sizeof("(?:)") - 1, 0);
+    return NXT_OK;
 }
 
 
@@ -128,25 +127,25 @@ njs_regexp_create(njs_vm_t *vm, njs_valu
 
     if (length != 0) {
         pattern = njs_regexp_pattern_create(vm, start, length, flags);
-
-        if (nxt_fast_path(pattern != NULL)) {
-            regexp = njs_regexp_alloc(vm, pattern);
-
-            if (nxt_fast_path(regexp != NULL)) {
-                value->data.u.regexp = regexp;
-                value->type = NJS_REGEXP;
-                value->data.truth = 1;
-
-                return NXT_OK;
-            }
+        if (nxt_slow_path(pattern == NULL)) {
+            return NXT_ERROR;
         }
 
-        return NXT_ERROR;
+    } else {
+        pattern = vm->shared->empty_regexp_pattern;
     }
 
-    *value = vm->empty_regexp;
+    regexp = njs_regexp_alloc(vm, pattern);
 
-    return NXT_OK;
+    if (nxt_fast_path(regexp != NULL)) {
+        value->data.u.regexp = regexp;
+        value->type = NJS_REGEXP;
+        value->data.truth = 1;
+
+        return NXT_OK;
+    }
+
+    return NXT_ERROR;
 }
 
 
diff -r 0939df226d5c -r 9f66db825663 njs/njs_string.c
--- a/njs/njs_string.c	Thu Nov 10 19:09:13 2016 +0300
+++ b/njs/njs_string.c	Fri Nov 11 17:28:47 2016 +0300
@@ -1946,9 +1946,11 @@ njs_string_prototype_match(njs_vm_t *vm,
     njs_string_prop_t     string;
     njs_regexp_pattern_t  *pattern;
 
-    arguments[0] = vm->empty_regexp;
     arguments[1] = args[0];
 
+    string.start = NULL;
+    string.size = 0;
+
     if (nargs > 1) {
 
         if (njs_is_regexp(&args[1])) {
@@ -1964,21 +1966,24 @@ njs_string_prototype_match(njs_vm_t *vm,
              */
             arguments[0] = args[1];
 
-        } else if (njs_is_string(&args[1])) {
+            goto match;
+        }
+
+        if (njs_is_string(&args[1])) {
             /* string1.match(string2) is the same as /string2/.exec(string1). */
-
             (void) njs_string_prop(&string, &args[1]);
-
-            ret = njs_regexp_create(vm, &arguments[0], string.start,
-                                    string.size, 0);
-            if (nxt_slow_path(ret != NXT_OK)) {
-                return ret;
-            }
         }
 
         /* A void value. */
     }
 
+    ret = njs_regexp_create(vm, &arguments[0], string.start, string.size, 0);
+    if (nxt_slow_path(ret != NXT_OK)) {
+        return ret;
+    }
+
+match:
+
     return njs_regexp_prototype_exec(vm, arguments, nargs, unused);
 }
 
diff -r 0939df226d5c -r 9f66db825663 njs/njs_vm.h
--- a/njs/njs_vm.h	Thu Nov 10 19:09:13 2016 +0300
+++ b/njs/njs_vm.h	Fri Nov 11 17:28:47 2016 +0300
@@ -845,7 +845,6 @@ struct njs_vm_s {
 
     nxt_regex_context_t      *regex_context;
     nxt_regex_match_data_t   *single_match_data;
-    njs_value_t              empty_regexp;
 
     nxt_array_t              *code;  /* of njs_vm_code_t */
 
@@ -875,6 +874,8 @@ struct njs_vm_shared_s {
      */
     njs_object_prototype_t   prototypes[NJS_PROTOTYPE_MAX];
     njs_function_t           constructors[NJS_CONSTRUCTOR_MAX];
+
+    njs_regexp_pattern_t     *empty_regexp_pattern;
 };
 
 
diff -r 0939df226d5c -r 9f66db825663 njs/njscript.c
--- a/njs/njscript.c	Thu Nov 10 19:09:13 2016 +0300
+++ b/njs/njscript.c	Fri Nov 11 17:28:47 2016 +0300
@@ -102,8 +102,9 @@ njs_vm_t *
 njs_vm_create(nxt_mem_cache_pool_t *mcp, njs_vm_shared_t **shared,
     nxt_lvlhsh_t *externals)
 {
-    njs_vm_t   *vm;
-    nxt_int_t  ret;
+    njs_vm_t              *vm;
+    nxt_int_t             ret;
+    njs_regexp_pattern_t  *pattern;
 
     if (mcp == NULL) {
         mcp = nxt_mem_cache_pool_create(&njs_vm_mem_cache_pool_proto, NULL,
@@ -145,6 +146,14 @@ njs_vm_create(nxt_mem_cache_pool_t *mcp,
 
             nxt_lvlhsh_init(&vm->shared->values_hash);
 
+            pattern = njs_regexp_pattern_create(vm, (u_char *) "(?:)",
+                                                sizeof("(?:)") - 1, 0);
+            if (nxt_slow_path(pattern == NULL)) {
+                return NULL;
+            }
+
+            vm->shared->empty_regexp_pattern = pattern;
+
             ret = njs_builtin_objects_create(vm);
             if (nxt_slow_path(ret != NXT_OK)) {
                 return NULL;



More information about the nginx-devel mailing list