Issue #19315 has been updated by Eregon (Benoit Daloze). Yes I think adding `RSTRING_RAW_PTR()` is a good way, I would just suggest another name: `RSTRING_START()`. Actually now I see this name was already proposed in https://bugs.ruby-lang.org/issues/19315#note-13. Why I think `RSTRING_START()` is better than `RSTRING_RAW_PTR()`: * Pairs better with `RSTRING_END()`, which is crucial to use in combination with the new macro/function. * `_RAW_` sounds internal/low-level like `FL_TEST_RAW` (which probably shouldn't be public or is dangerous) which might discourage its use. This new API is not low level, it represents the true start of the Ruby String, which can contain `\0` in the middle and might not end with `\0`, so `RSTRING_END()` must be used. * I think it's good `RSTRING_PTR()` sounds more low-level/internal than `RSTRING_START()`, because it becomes more expensive, just like `RARRAY_PTR` (low-level, slow) vs `RARRAY_AREF()/rb_ary_entry()` (higher-level, fast). @matz How about this for Ruby 4.1: 1. Add `RSTRING_START()` 2. Document `RSTRING_PTR()` as more expensive in some cases and recommend using `RSTRING_START()` instead. People can easily use it on older versions with: ```c #ifndef RSTRING_START #define RSTRING_START RSTRING_PTR #endif ``` 3. Enable `SHARABLE_MIDDLE_SUBSTRING` and change `RSTRING_PTR()` to re-allocate with a final `\0` if it's not already there. We could do 3 later, but given the idea to not actually deprecate `RSTRING_PTR()` I think there is no value to delay it. ---------------------------------------- Feature #19315: Lazy substrings in CRuby https://bugs.ruby-lang.org/issues/19315#change-117315 * 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/