From: "byroot (Jean Boussier)" Date: 2022-09-26T14:53:33+00:00 Subject: [ruby-core:110089] [Ruby master Bug#14900] Extra allocation in String#byteslice Issue #14900 has been updated by byroot (Jean Boussier). Status changed from Open to Closed Backport set to 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN Tracker changed from Feature to Bug I think if we stick solely to the ticket description, I think https://github.com/ruby/ruby/pull/6443 fixes the issue (the extra useless allocation). Also note that this extra allocation was a shared string, so it wasn't copying the string content. So we can probably close this as fixed now. If we wish to improve typically IO buffering code, we can open another issue. ---------------------------------------- Bug #14900: Extra allocation in String#byteslice https://bugs.ruby-lang.org/issues/14900#change-99344 * Author: janko (Janko Marohni��) * Status: Closed * Priority: Normal * Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN ---------------------------------------- When executing `String#byteslice` with a range, I noticed that sometimes the original string is allocated again. When I run the following script: ~~~ ruby require "objspace" string = "a" * 100_000 GC.start GC.disable generation = GC.count ObjectSpace.trace_object_allocations do string.byteslice(50_000..-1) ObjectSpace.each_object(String) do |string| p string.bytesize if ObjectSpace.allocation_generation(string) == generation end end ~~~ it outputs ~~~ 50000 100000 6 5 ~~~ The one with 50000 bytes is the result of `String#byteslice`, but the one with 100000 bytes is the duplicated original string. I expected only the result of `String#byteslice` to be amongst new allocations. If instead of the last 50000 bytes I slice the *first* 50000 bytes, the extra duplication doesn't occur. ~~~ ruby # ... string.byteslice(0, 50_000) # ... ~~~ ~~~ 50000 5 ~~~ It's definitely ok if the implementation of `String#bytesize` allocates extra strings as part of the implementation, but it would be nice if they were deallocated before returning the result. EDIT: It seems that `String#slice` has the same issue. -- https://bugs.ruby-lang.org/ Unsubscribe: