Skip to content

[Feature #22186] Copy sharable substrings up to a 256 byte slot - #17723

Merged
peterzhu2118 merged 1 commit into
ruby:masterfrom
himura467:str-subseq-embed-fix
Aug 7, 2026
Merged

peterzhu2118 merged 1 commit into
ruby:masterfrom
himura467:str-subseq-embed-fix

Conversation

@himura467

@himura467 himura467 commented Jul 8, 2026 •

Copy link
Copy Markdown
Contributor

str_subseq() allocates the new string with str_alloc_heap(), which always takes the default sizeof(struct RString) slot, so a sharable substring is copied only while it fits in that slot's embed capacity and is shared otherwise.

This allocates with str_alloc_embed() sized to the substring instead, and copies while the allocation fits in a slot of 256 bytes. When the source is frozen or already shared it can be the shared root itself, so sharing allocates only the substring there and the cap stays at the slot str_alloc_heap() gives.

Benchmark results and discussion are on the Redmine ticket.

@github-actions

github-actions Bot commented Jul 8, 2026

Copy link
Copy Markdown

This pull request references the following Redmine tickets:

@himura467
himura467 force-pushed the str-subseq-embed-fix branch 2 times, most recently from 67479ac to e4d8b33 Compare July 14, 2026 07:37
@himura467
himura467 force-pushed the str-subseq-embed-fix branch 2 times, most recently from 30d75ca to bb4bf1f Compare July 20, 2026 18:23
@himura467
himura467 force-pushed the str-subseq-embed-fix branch 6 times, most recently from 0dc2344 to 11a514e Compare August 5, 2026 17:51
@himura467 himura467 changed the title str_subseq: size the embed allocation to the actual substring length [Feature #22186] Copy sharable substrings up to a 128 byte slot Aug 5, 2026
@himura467
himura467 force-pushed the str-subseq-embed-fix branch 2 times, most recently from 59a8662 to 876f091 Compare August 6, 2026 03:52
@mame
mame requested review from byroot and peterzhu2118 August 6, 2026 08:29
@himura467
himura467 marked this pull request as ready for review August 6, 2026 08:31

@byroot byroot left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm 👍 on this change.

There is definitely some code that will gain from this and some that will lose, but I don't think any benchmark will ever give us a definite answer.

@kou kou left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Comment thread test/ruby/test_string.rb
def test_substring_embed
str = "a" * 256

require 'objspace'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this to top level?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file has three more require 'objspace' inside methods (lines 680, 3513 and 3612). I would rather move them all than leave mine as the only one at the top level. Is that in scope for this ticket, or should it be a separate PR?

Comment thread string.c Outdated
Comment on lines +3175 to +3176
/* Substrings that need a slot larger than this are shared instead of copied. */
#define STR_SUBSEQ_MAX_EMBED_SIZE 128

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a comment why we use 128 here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

Comment thread string.c Outdated
}

/* Substrings that need a slot larger than this are shared instead of copied. */
#define STR_SUBSEQ_MAX_EMBED_SIZE 128

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your ticket seems to have come to the conclusion that 256 byte slot is the optimal size, I also independently came to that conclusion in #18082. Why did you choose 128 here?

It might also make sense to deduplicate these constants.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

128 came from byroot in note-13:

it would be ~128B (or close, it doesn't have to perfectly fit an existing slot size, but might as well)

I have no benchmark that puts 128 ahead of 256, and I agree the two constants should be consistent.

My position is only that the current cutoff is too low and should be raised; I do not think I can tell which of 128 and 256 is better, so I would rather leave that call to the two of you.

@byroot if you are fine with 256 to match STR_DUPLICATE_MAX_EMBED_LEN, I will change it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a strong opinion on 128 or 256. I find it ends up being a tradeoff. If there's any strong reason to choose one over the other, I can swing that way.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, and I understand that 256 comes with an upside and a downside. byroot seemed to weigh the downside seriously in the ticket, the larger slots being allocated less densely and the extra memory retained per slice, so I picked the side where that cost is smaller. There is no stronger reason than that behind 128.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well we do 1024 bytes right now (which is the default GC maximum slot size) and it's not a problem, so 256 bytes should only make it better.

@himura467 himura467 Aug 6, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those 1024 byte slots hold strings that have to be stored somewhere anyway. In rb_str_new() and friends, not embedding means a 40 byte RString plus a malloc'd buffer, so the large slot is just the cheaper of the two ways to keep the bytes.

str_subseq() has an option those sites do not: the bytes already live in the parent, so it can share them instead of copying. Going from 128 to 256 moves substrings of roughly 104 to 231 bytes from a shared reference into a 256 byte slot. note-5 measured retained memory at up to 6.4x master with a 256 cutoff, and 128 halves that.

Speed-wise the micro-benchmarks do favour 256, and I would personally be happy with it. Let's wait for byroot's take.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

128 came from byroot in note-13:

Well, just above I say:

at least ~128B would make a lot of sense.

I think any value between 128 and 256 do make sense.

The reason I didn't suggest 256 is that there's a bit of a CPU/memory tradeoff is the string is appended to, so based on the numbers you provided 128 is definitely a no-brainer, but 256 can be debated (only 7% faster on your benchmark).

But yes, I'm totally fine with 256B.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@byroot
Thanks.

@peterzhu2118
Changed it to 256.

@himura467
himura467 force-pushed the str-subseq-embed-fix branch 3 times, most recently from ba5f204 to cd14626 Compare August 6, 2026 16:05
@himura467 himura467 changed the title [Feature #22186] Copy sharable substrings up to a 128 byte slot [Feature #22186] Copy sharable substrings up to a 256 byte slot Aug 6, 2026
Previously the embedded path allocated a full-size embedded string via str_alloc_heap plus STR_SET_EMBED, which capped the copy at the default struct RString slot whatever the substring length. Size the allocation to the substring with str_alloc_embed instead, and copy while it fits in a slot of 256 bytes, since larger slots are allocated less densely and increase garbage collection.

Keep the default struct RString slot as the cap when the source is frozen or already shared, where sharing allocates the substring alone rather than a frozen root as well.

[Feature #22186]
@himura467
himura467 force-pushed the str-subseq-embed-fix branch from cd14626 to 8fc66f7 Compare August 7, 2026 06:36
@himura467
himura467 requested a review from peterzhu2118 August 7, 2026 07:43
@peterzhu2118
peterzhu2118 merged commit a9017cc into ruby:master Aug 7, 2026
117 checks passed
@himura467
himura467 deleted the str-subseq-embed-fix branch August 7, 2026 14:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants