summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKunshan Wang <[email protected]>2023-07-21 02:17:38 +0800
committer<[email protected]>2023-07-20 14:17:38 -0400
commit639aa76e820480d707b90028972a52fd2a30957a ()
tree837d8ad1e422534b4ec8abc745811c54d942a4c8
parent460c27dc15b5efc46a74cb2a7809ca3fa6ce72a7 (diff)
Embed struct rmatch into GC slot (#8097)
-rw-r--r--gc.c10
-rw-r--r--include/ruby/internal/core/rmatch.h14
-rw-r--r--re.c29
3 files changed, 25 insertions, 28 deletions
@@ -3509,8 +3509,8 @@ obj_free(rb_objspace_t *objspace, VALUE obj)
if (!rb_data_free(objspace, obj)) return false;
break;
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);
@@ -3525,7 +3525,6 @@ obj_free(rb_objspace_t *objspace, VALUE obj)
onig_region_free(&rm->regs, 0);
if (rm->char_offset)
xfree(rm->char_offset);
- xfree(rm);
RB_DEBUG_COUNTER_INC(obj_match_ptr);
}
@@ -4841,11 +4840,10 @@ obj_memsize_of(VALUE obj, int use_all_types)
if (use_all_types) size += rb_objspace_data_type_memsize(obj);
break;
case T_MATCH:
- if (RMATCH(obj)->rmatch) {
- struct rmatch *rm = RMATCH(obj)->rmatch;
size += onig_region_memsize(&rm->regs);
size += sizeof(struct rmatch_offset) * rm->char_offset_num_allocated;
- size += sizeof(struct rmatch);
}
break;
case T_FILE:
@@ -68,7 +68,7 @@ struct rmatch_offset {
};
/** Represents a match. */
-struct rmatch {
/**
* "Registers" of a match. This is a quasi-opaque struct that holds
* execution result of a match. Roughly resembles `&~`.
@@ -82,6 +82,8 @@ struct rmatch {
int char_offset_num_allocated;
};
/**
* Regular expression execution context. When a regular expression "matches"
* to a string, it generates capture groups etc. This struct holds that info.
@@ -102,16 +104,13 @@ struct RMatch {
VALUE str;
/**
- * The result of this match.
- */
- struct rmatch *rmatch;
-
- /**
* The expression of this match.
*/
VALUE regexp; /* RRegexp */
};
RBIMPL_ATTR_PURE_UNLESS_DEBUG()
RBIMPL_ATTR_ARTIFICIAL()
/**
@@ -139,8 +138,7 @@ static inline struct re_registers *
RMATCH_REGS(VALUE match)
{
RBIMPL_ASSERT_TYPE(match, RUBY_T_MATCH);
- RBIMPL_ASSERT_OR_ASSUME(RMATCH(match)->rmatch != NULL);
- return &RMATCH(match)->rmatch->regs;
}
#endif /* RBIMPL_RMATCH_H */
@@ -961,12 +961,13 @@ VALUE rb_cMatch;
static VALUE
match_alloc(VALUE klass)
{
- NEWOBJ_OF(match, struct RMatch, klass, T_MATCH | (RGENGC_WB_PROTECTED_MATCH ? FL_WB_PROTECTED : 0), sizeof(struct RMatch), 0);
match->str = Qfalse;
- match->rmatch = 0;
match->regexp = Qfalse;
- match->rmatch = ZALLOC(struct rmatch);
return (VALUE)match;
}
@@ -1001,7 +1002,7 @@ pair_byte_cmp(const void *pair1, const void *pair2)
static void
update_char_offset(VALUE match)
{
- struct rmatch *rm = RMATCH(match)->rmatch;
struct re_registers *regs;
int i, num_regs, num_pos;
long c;
@@ -1079,23 +1080,23 @@ match_check(VALUE match)
static VALUE
match_init_copy(VALUE obj, VALUE orig)
{
- struct rmatch *rm;
if (!OBJ_INIT_COPY(obj, orig)) return obj;
RB_OBJ_WRITE(obj, &RMATCH(obj)->str, RMATCH(orig)->str);
RB_OBJ_WRITE(obj, &RMATCH(obj)->regexp, RMATCH(orig)->regexp);
- rm = RMATCH(obj)->rmatch;
if (rb_reg_region_copy(&rm->regs, RMATCH_REGS(orig)))
rb_memerror();
- if (RMATCH(orig)->rmatch->char_offset_num_allocated) {
if (rm->char_offset_num_allocated < rm->regs.num_regs) {
REALLOC_N(rm->char_offset, struct rmatch_offset, rm->regs.num_regs);
rm->char_offset_num_allocated = rm->regs.num_regs;
}
- MEMCPY(rm->char_offset, RMATCH(orig)->rmatch->char_offset,
struct rmatch_offset, rm->regs.num_regs);
RB_GC_GUARD(orig);
}
@@ -1250,8 +1251,8 @@ match_offset(VALUE match, VALUE n)
return rb_assoc_new(Qnil, Qnil);
update_char_offset(match);
- return rb_assoc_new(LONG2NUM(RMATCH(match)->rmatch->char_offset[i].beg),
- LONG2NUM(RMATCH(match)->rmatch->char_offset[i].end));
}
/*
@@ -1309,7 +1310,7 @@ match_begin(VALUE match, VALUE n)
return Qnil;
update_char_offset(match);
- return LONG2NUM(RMATCH(match)->rmatch->char_offset[i].beg);
}
@@ -1335,7 +1336,7 @@ match_end(VALUE match, VALUE n)
return Qnil;
update_char_offset(match);
- return LONG2NUM(RMATCH(match)->rmatch->char_offset[i].end);
}
/*
@@ -1422,7 +1423,7 @@ match_nth_length(VALUE match, VALUE n)
update_char_offset(match);
const struct rmatch_offset *const ofs =
- &RMATCH(match)->rmatch->char_offset[i];
return LONG2NUM(ofs->end - ofs->beg);
}
@@ -1454,7 +1455,7 @@ static void
match_set_string(VALUE m, VALUE string, long pos, long len)
{
struct RMatch *match = (struct RMatch *)m;
- struct rmatch *rmatch = match->rmatch;
RB_OBJ_WRITE(match, &RMATCH(match)->str, string);
RB_OBJ_WRITE(match, &RMATCH(match)->regexp, Qnil);