]> git.kaiwu.me - njs.git/commitdiff
Now the empty regexp pattern is created on compile stage only.
authorIgor Sysoev <igor@sysoev.ru>
Fri, 11 Nov 2016 14:28:47 +0000 (17:28 +0300)
committerIgor Sysoev <igor@sysoev.ru>
Fri, 11 Nov 2016 14:28:47 +0000 (17:28 +0300)
This speeds up njs_vm_clone() operation twice.

njs/njs_builtin.c
njs/njs_regexp.c
njs/njs_string.c
njs/njs_vm.h
njs/njscript.c

index a87539f5bb63af4016e57cdb05478345239fcb5c..e3dace3c8a97dc75d4b7c7f7ff85db404c03d050 100644 (file)
@@ -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;
 
index 2cea4c4ff4e5e7f1de5e4c43a90911b80dc55017..52cbafb27727303d4ad48883ac432b53caad083a 100644 (file)
@@ -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_value_t *value, u_char *start,
 
     if (length != 0) {
         pattern = njs_regexp_pattern_create(vm, start, length, flags);
+        if (nxt_slow_path(pattern == NULL)) {
+            return NXT_ERROR;
+        }
 
-        if (nxt_fast_path(pattern != NULL)) {
-            regexp = njs_regexp_alloc(vm, pattern);
+    } else {
+        pattern = vm->shared->empty_regexp_pattern;
+    }
 
-            if (nxt_fast_path(regexp != NULL)) {
-                value->data.u.regexp = regexp;
-                value->type = NJS_REGEXP;
-                value->data.truth = 1;
+    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_ERROR;
+        return NXT_OK;
     }
 
-    *value = vm->empty_regexp;
-
-    return NXT_OK;
+    return NXT_ERROR;
 }
 
 
index c3a6ac8415c8bd30e62e5ef19693309e44deb447..29a014c45189ed6a53a51d5a93e635d9ae27228b 100644 (file)
@@ -1946,9 +1946,11 @@ njs_string_prototype_match(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
     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, njs_value_t *args, nxt_uint_t nargs,
              */
             arguments[0] = args[1];
 
-        } else if (njs_is_string(&args[1])) {
-            /* string1.match(string2) is the same as /string2/.exec(string1). */
+            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);
 }
 
index 82081725f9494c4b242c025f94799eefa8cd0d7c..06ed0cf3d092d43cb1cddf826b6027524a303838 100644 (file)
@@ -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;
 };
 
 
index 910686cccae64ab24de5503a464f3a6e96bf8615..6c530d4e2486691433606448cac654ebb4643a6b 100644 (file)
@@ -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, njs_vm_shared_t **shared,
 
             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;