[ruby-core:113970] [Ruby master Feature#19315] Lazy substrings in CRuby
From:
"Dan0042 (Daniel DeLorme) via ruby-core" <ruby-core@...>
Date:
2023-06-21 12:45:47 UTC
List:
ruby-core #113970
Issue #19315 has been updated by Dan0042 (Daniel DeLorme). duerst (Martin D=FCrst) wrote in #note-12: > Pat Shaughnessy in his blog describes exactly the same thing as Benoit Da= loze above: Ruby shares string data as long as the ends of the strings alig= n. On first skimming the blog I actually didn't notice that. It's mentioned in= one sentence and everything else is about how great Ruby is for avoiding u= nneeded allocations thanks to copy-on-write. I realize that `RSTRING_PTR` is used everywhere, but would it be in the rea= lm of possibility to deprecate it and replace it by something like `RSTRING= _CSTR` and `RSTRING_PSTR`. ---------------------------------------- Feature #19315: Lazy substrings in CRuby https://bugs.ruby-lang.org/issues/19315#change-103627 * Author: Eregon (Benoit Daloze) * Status: Open * Priority: Normal ---------------------------------------- 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 t= he 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 i= t, it's worth it. There is already code for this (`SHARABLE_MIDDLE_SUBSTRING`), but it's disa= bled 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 a= lways without copy. There are countless workarounds for this missing optimization, all not wort= h 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. --=20 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/postorius/lists/ruby-c= ore.ml.ruby-lang.org/