From fe70b55253c9ece305cdf1d44c46058001692110 Mon Sep 17 00:00:00 2001 From: c9s Date: Fri, 23 May 2014 12:08:06 +0800 Subject: [PATCH] Add node type for comparison --- bench_str.csv | 6 ++++++ include/r3.h | 9 ++++++--- src/node.c | 20 ++++++++++++++------ src/str.c | 10 +++++----- tests/check_slug.c | 8 ++++---- 5 files changed, 35 insertions(+), 18 deletions(-) diff --git a/bench_str.csv b/bench_str.csv index c0d4f60..e1c5a85 100644 --- a/bench_str.csv +++ b/bench_str.csv @@ -459,3 +459,9 @@ 1400816594,9389101.94,1989498.64,34663.67 1400816645,9201251.49,2105517.01,43240.25 1400816815,9228390.29,2097606.06,23301.69 +1400817268,9242018.45,2109706.16,38479.85 +1400817280,9320308.27,1891100.64,47662.55 +1400817392,6448229.88,1994875.02,34663.67 +1400817671,8654311.52,2094510.75,47662.55 +1400817706,9362050.27,1981074.38,62601.55 +1400818067,9247601.69,1944615.34,35848.75 diff --git a/include/r3.h b/include/r3.h index 5b40380..2c3da24 100644 --- a/include/r3.h +++ b/include/r3.h @@ -40,9 +40,8 @@ struct _node { /** compile-time variables here.... **/ /* the combined regexp pattern string from pattern_tokens */ + int compare_type; char * combined_pattern; - int ov_cnt; - int * ov; pcre * pcre_pattern; pcre_extra * pcre_extra; @@ -174,7 +173,11 @@ route * r3_tree_match_route(const node *n, match_entry * entry); #define METHOD_HEAD 2<<5 #define METHOD_OPTIONS 2<<6 -int r3_pattern_to_opcode(char * pattern, int pattern_len); + + +int r3_pattern_to_opcode(char * pattern); + +enum { NODE_COMPARE_STR, NODE_COMPARE_PCRE, NODE_COMPARE_OPCODE }; enum { OP_EXPECT_DIGITS = 1, OP_EXPECT_WORDS, OP_EXPECT_NOSLASH, OP_EXPECT_NODASH }; diff --git a/src/node.c b/src/node.c index ddf1352..c411f6e 100644 --- a/src/node.c +++ b/src/node.c @@ -58,8 +58,6 @@ node * r3_tree_create(int cap) { n->combined_pattern = NULL; n->pcre_pattern = NULL; n->pcre_extra = NULL; - n->ov_cnt = 0; - n->ov = NULL; return n; } @@ -81,7 +79,6 @@ void r3_tree_free(node * tree) { } #endif zfree(tree->combined_pattern); - zfree(tree->ov); zfree(tree); tree = NULL; } @@ -168,10 +165,19 @@ void r3_tree_compile_patterns(node * n) { p++; edge *e = NULL; + int opcode_cnt = 0; for ( int i = 0 ; i < n->edge_len ; i++ ) { e = n->edges[i]; if ( e->has_slug ) { + // compile "foo/{slug}" to "foo/[^/]+" char * slug_pat = slug_compile(e->pattern, e->pattern_len); + + // if found available opcode + e->opcode = r3_pattern_to_opcode(slug_pat); + if (e->opcode) { + opcode_cnt++; + } + strcat(p, slug_pat); } else { strncat(p++,"(", 1); @@ -189,9 +195,11 @@ void r3_tree_compile_patterns(node * n) { info("pattern: %s\n",cpat); - n->ov_cnt = (1 + n->edge_len) * 3; - n->ov = (int*) zcalloc(sizeof(int) * n->ov_cnt); - + // if all edges use opcode, we should skip the combined_pattern. + if ( opcode_cnt == n->edge_len ) { + zfree(cpat); + return; + } n->combined_pattern = cpat; diff --git a/src/str.c b/src/str.c index 9485b16..2017aaf 100644 --- a/src/str.c +++ b/src/str.c @@ -13,17 +13,17 @@ #include "str_array.h" #include "zmalloc.h" -int r3_pattern_to_opcode(char * pattern, int pattern_len) { - if ( strncmp(pattern, "\\w+", pattern_len) == 0 ) { +int r3_pattern_to_opcode(char * pattern) { + if ( strcmp(pattern, "\\w+") == 0 ) { return OP_EXPECT_WORDS; } - if ( strncmp(pattern, "\\d+", pattern_len) == 0 ) { + if ( strcmp(pattern, "\\d+") == 0 ) { return OP_EXPECT_DIGITS; } - if ( strncmp(pattern, "[^/]+", pattern_len) == 0 ) { + if ( strcmp(pattern, "[^/]+") == 0 ) { return OP_EXPECT_NOSLASH; } - if ( strncmp(pattern, "[^-]+", pattern_len) == 0 ) { + if ( strcmp(pattern, "[^-]+") == 0 ) { return OP_EXPECT_NODASH; } return 0; diff --git a/tests/check_slug.c b/tests/check_slug.c index 64263cb..56d0804 100644 --- a/tests/check_slug.c +++ b/tests/check_slug.c @@ -15,10 +15,10 @@ START_TEST (test_pattern_to_opcode) { - ck_assert( r3_pattern_to_opcode("\\w+", sizeof("\\w+") - 1) == OP_EXPECT_WORDS ); - ck_assert( r3_pattern_to_opcode("\\d+", sizeof("\\d+") - 1) == OP_EXPECT_DIGITS ); - ck_assert( r3_pattern_to_opcode("[^/]+", sizeof("[^/]+") - 1) == OP_EXPECT_NOSLASH ); - ck_assert( r3_pattern_to_opcode("[^-]+", sizeof("[^-]+") - 1) == OP_EXPECT_NODASH ); + ck_assert( r3_pattern_to_opcode("\\w+") == OP_EXPECT_WORDS ); + ck_assert( r3_pattern_to_opcode("\\d+") == OP_EXPECT_DIGITS ); + ck_assert( r3_pattern_to_opcode("[^/]+") == OP_EXPECT_NOSLASH ); + ck_assert( r3_pattern_to_opcode("[^-]+") == OP_EXPECT_NODASH ); } END_TEST