From: "himura467 (Akito Shitara) via ruby-core" Date: 2026-06-13T07:37:54+00:00 Subject: [ruby-core:125735] [Ruby Feature#19315] Lazy substrings in CRuby Issue #19315 has been updated by himura467 (Akito Shitara). mame (Yusuke Endoh) wrote in #note-43: > Please provide a quantitative evaluation of performance and compatibility at the application level. Does this change bring an observable performance improvement? Does memory usage increase or decrease? > It would also be useful to observe how often `RSTRING_PTR` is actually called without the GVL in practice, for example by inserting `rb_bug` into that code path and running test suites. Evaluating with yjit-bench might be a good idea. Thanks, I'll share the results. Eregon (Benoit Daloze) wrote in #note-44: > mame (Yusuke Endoh) wrote in #note-43: > > By the way, this did not come up during the meeting, but I think the fact that `RSTRING_PTR` can now trigger GC could itself be a serious compatibility problem. There is probably a fair amount of code written under the assumption that `RSTRING_PTR` never triggers GC, and such code may start to fail with very low probability, depending on subtle behaviors of the conservative GC. Evaluating this risk is very difficult. > > Regarding that, I think there is no need for `RSTRING_PTR` to trigger GC, so the implementation could make sure it never does. Makes sense. I'll work on updating the implementation with these in mind. ---------------------------------------- Feature #19315: Lazy substrings in CRuby https://bugs.ruby-lang.org/issues/19315#change-117621 * 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. -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/