Issue #19315 has been updated by rhenium (Kazuki Yamaguchi). kou (Kouhei Sutou) wrote in #note-36:
rhenium (Kazuki Yamaguchi) wrote in #note-35:
If we go down the route of having `RSTRING_PTR()` lazily allocate a NUL-terminated buffer in it, that is a breaking change on its own because it introduces the possibility of GC and longjmp where they previously weren't possible.
What case do you imagine? Calling `RSTRING_PTR()` without GVL case?
On top of my head: I think some users will have to choose between adding new `RB_GC_GUARD()` for temporary objects on the stack or migrating to `RSTRING_RAW_PTR()`. Some users have malloc()'ed memory and currently do not expect `RSTRING_PTR()` might raise an exception.
On a related note, while `StringValueCStr()` can work here, I've sometimes wished for a function to ensure a C string without the `StringValue` part (implicit type conversion by `#to_str`) for clarity. Something like:
```c const char *rb_str_cstr(VALUE caller_must_ensure_t_string); ```
Interesting. How about discussing it as a separated issue? FYI: We have internal `rb_str_to_cstr()` function: https://github.com/ruby/ruby/blob/1c004aa92f8e93750df6edff5de4599912622974/s... It doesn't raise an exception nor report a warning but you want `rb_str_cstr()` to do them, right?
Indeed, I've created a new feature request for this: https://bugs.ruby-lang.org/issues/22102 ---------------------------------------- Feature #19315: Lazy substrings in CRuby https://bugs.ruby-lang.org/issues/19315#change-117548 * 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/