Skip to content

Commit

Permalink
[Feature #18239] Implement VWA for strings
Browse files Browse the repository at this point in the history
This commit adds support for embedded strings with variable capacity and
uses Variable Width Allocation to allocate strings.
  • Loading branch information
peterzhu2118 committed Oct 25, 2021
1 parent 6374be5 commit a5b6598
Show file tree
Hide file tree
Showing 16 changed files with 452 additions and 171 deletions.
2 changes: 2 additions & 0 deletions debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ const union {
enum ruby_robject_consts robject_consts;
enum ruby_rmodule_flags rmodule_flags;
enum ruby_rstring_flags rstring_flags;
#if !USE_RVARGC
enum ruby_rstring_consts rstring_consts;
#endif
enum ruby_rarray_flags rarray_flags;
enum ruby_rarray_consts rarray_consts;
enum {
Expand Down
9 changes: 5 additions & 4 deletions ext/-test-/string/capacity.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
static VALUE
bug_str_capacity(VALUE klass, VALUE str)
{
return
STR_EMBED_P(str) ? INT2FIX(RSTRING_EMBED_LEN_MAX) : \
STR_SHARED_P(str) ? INT2FIX(0) : \
LONG2FIX(RSTRING(str)->as.heap.aux.capa);
if (!STR_EMBED_P(str) && STR_SHARED_P(str)) {
return INT2FIX(0);
}

return LONG2FIX(rb_str_capacity(str));
}

void
Expand Down
10 changes: 9 additions & 1 deletion ext/-test-/string/cstr.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,13 @@ bug_str_unterminated_substring(VALUE str, VALUE vbeg, VALUE vlen)
if (RSTRING_LEN(str) < beg + len) rb_raise(rb_eIndexError, "end: %ld", beg + len);
str = rb_str_new_shared(str);
if (STR_EMBED_P(str)) {
#if USE_RVARGC
RSTRING(str)->as.embed.len = (short)len;
#else
RSTRING(str)->basic.flags &= ~RSTRING_EMBED_LEN_MASK;
RSTRING(str)->basic.flags |= len << RSTRING_EMBED_LEN_SHIFT;
memmove(RSTRING(str)->as.ary, RSTRING(str)->as.ary + beg, len);
#endif
memmove(RSTRING(str)->as.embed.ary, RSTRING(str)->as.embed.ary + beg, len);
}
else {
RSTRING(str)->as.heap.ptr += beg;
Expand Down Expand Up @@ -112,7 +116,11 @@ bug_str_s_cstr_noembed(VALUE self, VALUE str)
Check_Type(str, T_STRING);
FL_SET((str2), STR_NOEMBED);
memcpy(buf, RSTRING_PTR(str), capacity);
#if USE_RVARGC
RBASIC(str2)->flags &= ~(STR_SHARED | FL_USER5 | FL_USER6);
#else
RBASIC(str2)->flags &= ~RSTRING_EMBED_LEN_MASK;
#endif
RSTRING(str2)->as.heap.aux.capa = capacity;
RSTRING(str2)->as.heap.ptr = buf;
RSTRING(str2)->as.heap.len = RSTRING_LEN(str);
Expand Down
Loading

0 comments on commit a5b6598

Please sign in to comment.