From: "himura467 (Akito Shitara) via ruby-core" Date: 2026-06-09T08:15:14+00:00 Subject: [ruby-core:125646] [Ruby Feature#19315] Lazy substrings in CRuby Issue #19315 has been updated by himura467 (Akito Shitara). IMO, backward compatibility of `RSTRING_PTR` should be preserved within the scope of this feature. Eregon (Benoit Daloze) wrote in #note-30: > I' believe migrating usages of `RSTRING_PTR()` which want `\0`-terminated to `StringValueCStr()` is the right thing to do. > And in fact current usages of `RSTRING_PTR()` that expect `\0`-termination are incorrect, if `\0` can be in the middle of the String (though it results in a smaller C string rather than reading out of bounds). > > How about this: > 1. Deprecate `RSTRING_PTR()` with a message saying to use either `StringValueCStr()` (if want to use it as a NUL-terminated C string) or `RSTRING_START()` (for efficiency & fixing the warning, must be paired with either `RSTRING_LEN()` or `RSTRING_END()`). > ... > 3. Then some release(s) later enable `SHARABLE_MIDDLE_SUBSTRING`. `RSTRING_PTR()` at that point is the same as `RSTRING_START()` (so no issues with using a different buffer than `RSTRING_END()` and already-correct usages of `RSTRING_PTR` are preserved even if they didn't migrate), but no longer guarantees to be NUL-terminated (which was already incorrect for the `\0` in the middle case and should have used `StringValueCStr()` already). I understand the motivation, but I think that broadens the impact of this feature well beyond what it warrants. The primary goal here is enabling zero-copy substring reuse via `SHARABLE_MIDDLE_SUBSTRING`, with `RSTRING_RAW_PTR` (or `RSTRING_START`) as the new API. Ensuring `RSTRING_PTR` still guarantees null termination is a necessary protective measure to minimize breakage of existing C extensions. Deprecating and changing the behavioral guarantees of `RSTRING_PTR` itself would affect all existing callers of `RSTRING_PTR`, which is a much larger blast radius than this feature itself justifies. I think our basic policy should be adding `RSTRING_RAW_PTR`, while keeping `RSTRING_PTR` compatible by default, and leaving any deprecation as a separate future discussion. ---------------------------------------- Feature #19315: Lazy substrings in CRuby https://bugs.ruby-lang.org/issues/19315#change-117500 * 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/