summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLourens Naudé <lourens@bearmetal.eu>2019-04-20 00:44:51 +0100
committerNobuyoshi Nakada <nobu@ruby-lang.org>2019-05-07 21:58:55 +0900
commita47f598d77ac97f9fe89fe16aa8bcab4fd262c16 (patch)
tree069ca07cf1e65cc9d253dab5101d23bed933c21a
parent7d805e67f3275aef066d77aa9c32bef715c362ed (diff)
Reduce ONIG_NREGION from 10 to 4: power of 2 and testing revealed most pattern matches are less than or equal to 4 results
Closes: https://github.com/ruby/ruby/pull/2135
-rw-r--r--benchmark/match_gt4.rb1
-rw-r--r--benchmark/match_small.rb1
-rw-r--r--debug_counter.h6
-rw-r--r--gc.c11
-rw-r--r--include/ruby/onigmo.h2
5 files changed, 20 insertions, 1 deletions
diff --git a/benchmark/match_gt4.rb b/benchmark/match_gt4.rb
new file mode 100644
index 0000000000..ffda109912
--- /dev/null
+++ b/benchmark/match_gt4.rb
@@ -0,0 +1 @@
+1000000.times { /(.)(.)(\d+)(\d)/.match("THX1138.") }
diff --git a/benchmark/match_small.rb b/benchmark/match_small.rb
new file mode 100644
index 0000000000..3b743d484a
--- /dev/null
+++ b/benchmark/match_small.rb
@@ -0,0 +1 @@
+1000000.times { 'haystack'.match(/hay/) }
diff --git a/debug_counter.h b/debug_counter.h
index 83ae701524..f0444d7190 100644
--- a/debug_counter.h
+++ b/debug_counter.h
@@ -177,6 +177,9 @@ RB_DEBUG_COUNTER(gc_isptr_maybe)
* * hash_under4: has under 4 entries
* * hash_ge4: has n entries (4<=n<8)
* * hash_ge8: has n entries (8<=n)
+ * * match_under4: has under 4 oniguruma regions allocated
+ * * match_ge4: has n regions allocated (4<=n<8)
+ * * match_ge8: has n regions allocated (8<=n)
* * data_empty: T_DATA but no memory free.
* * data_xfree: free'ed by xfree().
* * data_imm_free: free'ed immediately.
@@ -225,6 +228,9 @@ RB_DEBUG_COUNTER(obj_data_xfree)
RB_DEBUG_COUNTER(obj_data_imm_free)
RB_DEBUG_COUNTER(obj_data_zombie)
+RB_DEBUG_COUNTER(obj_match_under4)
+RB_DEBUG_COUNTER(obj_match_ge4)
+RB_DEBUG_COUNTER(obj_match_ge8)
RB_DEBUG_COUNTER(obj_match_ptr)
RB_DEBUG_COUNTER(obj_file_ptr)
RB_DEBUG_COUNTER(obj_bignum_ptr)
diff --git a/gc.c b/gc.c
index 5bd0cf75b6..b629c03dc0 100644
--- a/gc.c
+++ b/gc.c
@@ -2436,6 +2436,17 @@ obj_free(rb_objspace_t *objspace, VALUE obj)
case T_MATCH:
if (RANY(obj)->as.match.rmatch) {
struct rmatch *rm = RANY(obj)->as.match.rmatch;
+#if USE_DEBUG_COUNTER
+ if (rm->regs.num_regs >= 8) {
+ RB_DEBUG_COUNTER_INC(obj_match_ge8);
+ }
+ else if (rm->regs.num_regs >= 4) {
+ RB_DEBUG_COUNTER_INC(obj_match_ge4);
+ }
+ else if (rm->regs.num_regs >= 1) {
+ RB_DEBUG_COUNTER_INC(obj_match_under4);
+ }
+#endif
onig_region_free(&rm->regs, 0);
if (rm->char_offset)
xfree(rm->char_offset);
diff --git a/include/ruby/onigmo.h b/include/ruby/onigmo.h
index 34b8268d59..6187b37dc3 100644
--- a/include/ruby/onigmo.h
+++ b/include/ruby/onigmo.h
@@ -434,7 +434,7 @@ int onigenc_str_bytelen_null(OnigEncoding enc, const OnigUChar* p);
/* PART: regular expression */
/* config parameters */
-#define ONIG_NREGION 10
+#define ONIG_NREGION 4
#define ONIG_MAX_CAPTURE_GROUP_NUM 32767
#define ONIG_MAX_BACKREF_NUM 1000
#define ONIG_MAX_REPEAT_NUM 100000