Issue #19315 has been updated by himura467 (Akito Shitara). That's a fair point. The ruby-bench results do not demonstrate the usefulness of the feature itself, since those workloads don't appear to exercise non-embeddable middle substrings in any significant way. SHARABLE\_MIDDLE\_SUBSTRING only activates for substrings that do not satisfy `STR_EMBEDDABLE_P`, and the applications in ruby-bench (JSON processing, YAML loading, log aggregation, etc.) rarely produce substrings of that size. The micro-benchmarks I shared do show the feature's effectiveness for its target use cases: middle slices go from O(n) to O(1), and RSS for 1,000 x 100 KB slices drops from 145 MB to 30 MB. However, these are synthetic benchmarks designed to highlight the best case, not evidence that real applications benefit. To properly evaluate usefulness, we would need benchmarks drawn from workloads that actually take many non-embeddable substrings (for example, CSV/log parsing or network packet processing over large buffers). If no such benchmark currently exists in ruby-bench, adding one that exercises this pattern would be the right next step. ---------------------------------------- Feature #19315: Lazy substrings in CRuby https://bugs.ruby-lang.org/issues/19315#change-117834 * Author: Eregon (Benoit Daloze) * Status: Open ---------------------------------------- CRuby should implement lazy substrings, i.e., "abcdef"[1..3] must not copy bytes. Currently CRuby only reuse the char* if the substring is until the end of the buffer. But it should also work wherever the substring starts and ends. Yes, it means RSTRING_PTR() might need to allocate to \0-terminate, so be it, it's worth it. There is already code for this (`SHARABLE_MIDDLE_SUBSTRING`), but it's disabled by default and `RSTRING_PTR()` needs to be changed to deal with this. It seems a good idea to introduce a variant of `RSTRING_PTR` which doesn't guarantee \0-termination, so such callers can then use the existing bytes always without copy. There are countless workarounds for this missing optimization, all not worth it with lazy substring and all less readable: * https://bugs.ruby-lang.org/issues/19314 * https://bugs.ruby-lang.org/issues/18598#note-3 * https://github.com/ruby/net-protocol/pull/14 * Manual lazy substrings which track string + index + length * More but I don't remember all now, feel free to comment or link more urls/tickets. ---Files-------------------------------- ruby-bench-ratio.png (230 KB) -- https://bugs.ruby-lang.org/