Repository navigation
[Feature #22186] Copy sharable substrings up to a 256 byte slot - #17723
Conversation
|
This pull request references the following Redmine tickets: |
67479ac to
e4d8b33
Compare
30d75ca to
bb4bf1f
Compare
0dc2344 to
11a514e
Compare
59a8662 to
876f091
Compare
byroot
left a comment
There was a problem hiding this comment.
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.
| def test_substring_embed | ||
| str = "a" * 256 | ||
|
|
||
| require 'objspace' |
There was a problem hiding this comment.
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?
| /* Substrings that need a slot larger than this are shared instead of copied. */ | ||
| #define STR_SUBSEQ_MAX_EMBED_SIZE 128 |
There was a problem hiding this comment.
Can we add a comment why we use 128 here?
| } | ||
|
|
||
| /* Substrings that need a slot larger than this are shared instead of copied. */ | ||
| #define STR_SUBSEQ_MAX_EMBED_SIZE 128 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
@byroot
Thanks.
@peterzhu2118
Changed it to 256.
ba5f204 to
cd14626
Compare
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]
cd14626 to
8fc66f7
Compare
str_subseq()allocates the new string withstr_alloc_heap(), which always takes the defaultsizeof(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 slotstr_alloc_heap()gives.Benchmark results and discussion are on the Redmine ticket.