From aff677f82f241a062f20f292b96eaa0d9ebe3fc8 Mon Sep 17 00:00:00 2001 From: Igor Sysoev Date: Fri, 11 Nov 2016 17:28:47 +0300 Subject: [PATCH] Now the empty regexp pattern is created on compile stage only. This speeds up njs_vm_clone() operation twice. --- njs/njs_builtin.c | 2 +- njs/njs_regexp.c | 29 ++++++++++++++--------------- njs/njs_string.c | 23 ++++++++++++++--------- njs/njs_vm.h | 3 ++- njs/njscript.c | 13 +++++++++++-- 5 files changed, 42 insertions(+), 28 deletions(-) diff --git a/njs/njs_builtin.c b/njs/njs_builtin.c index a87539f5..e3dace3c 100644 --- a/njs/njs_builtin.c +++ b/njs/njs_builtin.c @@ -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 --git a/njs/njs_regexp.c b/njs/njs_regexp.c index 2cea4c4f..52cbafb2 100644 --- a/njs/njs_regexp.c +++ b/njs/njs_regexp.c @@ -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; } diff --git a/njs/njs_string.c b/njs/njs_string.c index c3a6ac84..29a014c4 100644 --- a/njs/njs_string.c +++ b/njs/njs_string.c @@ -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); } diff --git a/njs/njs_vm.h b/njs/njs_vm.h index 82081725..06ed0cf3 100644 --- a/njs/njs_vm.h +++ b/njs/njs_vm.h @@ -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 --git a/njs/njscript.c b/njs/njscript.c index 910686cc..6c530d4e 100644 --- a/njs/njscript.c +++ b/njs/njscript.c @@ -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; -- 2.47.3