Skip to content

Commit 42cf22e

Browse files
byrootpeterzhu2118
authored andcommitted
Allow sharing middlesubstring if the terminator is present
Sharing middle substrings have been behind a compilation flags for a very long time, and it's unclear if we'll ever make it the default. However, we can still share middle substrings without breaking the zero-terminated contract if the source string happens to have the necessary NUL characters at that specific offset. This is the case for some file formats such as FlatBuffers, BSON keys and a few others.
1 parent 5f5a220 commit 42cf22e

2 files changed

Lines changed: 41 additions & 14 deletions

File tree

‎string.c‎

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -217,15 +217,30 @@ str_enc_fastpath(VALUE str)
217217

218218
#define STR_ENC_GET(str) get_encoding(str)
219219

220+
static inline bool
221+
zero_filled(const char *s, int n)
222+
{
223+
for (; n > 0; --n) {
224+
if (*s++) return false;
225+
}
226+
return true;
227+
}
228+
220229
#if !defined SHARABLE_MIDDLE_SUBSTRING
221230
# define SHARABLE_MIDDLE_SUBSTRING 0
222231
#endif
223-
#if !SHARABLE_MIDDLE_SUBSTRING
224-
#define SHARABLE_SUBSTRING_P(beg, len, end) ((beg) + (len) == (end))
232+
233+
static inline bool
234+
SHARABLE_SUBSTRING_P(VALUE str, long beg, long len)
235+
{
236+
#if SHARABLE_MIDDLE_SUBSTRING
237+
return true;
225238
#else
226-
#define SHARABLE_SUBSTRING_P(beg, len, end) 1
239+
long end = beg + len;
240+
long source_len = RSTRING_LEN(str);
241+
return end == source_len || zero_filled(RSTRING_PTR(str) + end, TERM_LEN(str));
227242
#endif
228-
243+
}
229244

230245
static inline long
231246
str_embed_capa(VALUE str)
@@ -2827,15 +2842,6 @@ rb_string_value_ptr(volatile VALUE *ptr)
28272842
return RSTRING_PTR(str);
28282843
}
28292844

2830-
static int
2831-
zero_filled(const char *s, int n)
2832-
{
2833-
for (; n > 0; --n) {
2834-
if (*s++) return 0;
2835-
}
2836-
return 1;
2837-
}
2838-
28392845
static const char *
28402846
str_null_char(const char *s, long len, const int minlen, rb_encoding *enc)
28412847
{
@@ -3122,7 +3128,7 @@ str_subseq(VALUE str, long beg, long len)
31223128
RUBY_ASSERT(beg+len <= RSTRING_LEN(str));
31233129

31243130
const int termlen = TERM_LEN(str);
3125-
if (!SHARABLE_SUBSTRING_P(beg, len, RSTRING_LEN(str))) {
3131+
if (!SHARABLE_SUBSTRING_P(str, beg, len)) {
31263132
str2 = rb_str_new(RSTRING_PTR(str) + beg, len);
31273133
RB_GC_GUARD(str);
31283134
return str2;

‎test/ruby/test_string.rb‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3460,6 +3460,27 @@ def test_byteslice
34603460
assert_equal(false, ("\u3042"*10).byteslice(0, 20).valid_encoding?, bug7954)
34613461
end
34623462

3463+
def test_shared_middle_string_terminator
3464+
ten = "0123456789"
3465+
hundred = ten * 10
3466+
str = "#{hundred}\0#{hundred}".freeze
3467+
3468+
require 'objspace'
3469+
3470+
substr = str.byteslice(0, hundred.bytesize)
3471+
assert_equal hundred, substr
3472+
assert_includes ObjectSpace.dump(substr), ' "shared":true,'
3473+
3474+
# Larger terminator
3475+
substr.force_encoding(Encoding::UTF_16BE)
3476+
assert_equal hundred.dup.force_encoding(Encoding::UTF_16BE), substr
3477+
refute_includes ObjectSpace.dump(substr), ' "shared":true,'
3478+
3479+
substr = str.byteslice(0, hundred.bytesize + 1)
3480+
assert_equal hundred + "\0", substr
3481+
refute_includes ObjectSpace.dump(substr), ' "shared":true,'
3482+
end
3483+
34633484
def test_unknown_string_option
34643485
str = nil
34653486
assert_nothing_raised(SyntaxError) do

0 commit comments

Comments
 (0)