You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@thrift.apache.org by "Dmytro Shteflyuk (Jira)" <ji...@apache.org> on 2026/10/16 18:59:00 UTC

[jira] [Resolved] (THRIFT-6427) Avoid copying binary payload bytes in native Ruby Compact writes

     [ https://issues.apache.org/jira/browse/THRIFT-6427?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Dmytro Shteflyuk resolved THRIFT-6427.
--------------------------------------
    Fix Version/s: 0.26.0
       Resolution: Fixed

> Avoid copying binary payload bytes in native Ruby Compact writes
> ----------------------------------------------------------------
>
>                 Key: THRIFT-6427
>                 URL: https://issues.apache.org/jira/browse/THRIFT-6427
>             Project: Thrift
>          Issue Type: Bug
>          Components: Ruby - Library
>            Reporter: Dmytro Shteflyuk
>            Assignee: Dmytro Shteflyuk
>            Priority: Major
>             Fix For: 0.26.0
>
>          Time Spent: 0.5h
>  Remaining Estimate: 0h
>
> h3. Problem
> Native Ruby CompactProtocol#write_binary already has a binary String, but its final WRITE call creates another String with rb_str_new and copies the payload bytes before calling the transport. This copy is separate from the length-prefix encoding. write_string also reaches this path after its UTF-8 conversion.
> h3. Client impact
> Repeated large binary writes allocate payload-sized temporary storage even when the transport consumes or discards the bytes immediately. This increases memory use and garbage-collection work during serialization.
> h3. Reproduction
> With the native extension built, save the following as compact-payload.rb and run it from the repository root with {{ruby -Ilib/rb/lib -Ilib/rb/ext compact-payload.rb}}:
> {code:ruby}
> require "thrift"
> require "objspace"
> transport = Thrift::BaseTransport.new
> transport.define_singleton_method(:write) { |bytes| nil }
> protocol = Thrift::CompactProtocol.new(transport)
> raise "native extension required" unless protocol.native?
> payload = ("x".b * (1024 * 1024)).freeze
> GC.start
> disabled = GC.disable
> begin
>   before = ObjectSpace.memsize_of_all(String)
>   64.times { protocol.write_binary(payload) }
>   footprint = ObjectSpace.memsize_of_all(String) - before
> ensure
>   GC.enable unless disabled
> end
> puts format("Temporary String footprint: %.3f MiB", footprint.fdiv(1024 * 1024))
> {code}
> Testing on master commit {{5248fb28acda184580bd490cea8a1bea6933535a}} produces:
> {code}
> Temporary String footprint: 64.005 MiB
> {code}
> Observed with CRuby 4.0.7 on aarch64 Linux. GC is disabled only to measure temporary String storage; this is not a measurement of retained memory after collection.
> h3. Expected behavior
> Avoid the eager payload-sized copy where Ruby can share storage while preserving the existing wire bytes, transport dispatch, exception behavior, and separate mutable plain-String callback argument. A transport must still be able to mutate its argument without changing the caller's input, including when that input is frozen.
> Shared storage can keep a larger backing allocation alive when a transport retains a substring. That lifetime tradeoff should be documented and considered alongside the reduction in temporary copying.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)