You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@thrift.apache.org by "kpumuk (via GitHub)" <gi...@apache.org> on 2026/10/18 18:14:36 UTC
[PR] THRIFT-6434: Release exhausted input buffers in Ruby transports [thrift]
kpumuk opened a new pull request, #4024:
URL: https://github.com/apache/thrift/pull/4024
<!-- Explain the changes in the pull request below: -->
BufferedTransport, FramedTransport, HeaderTransport and HTTPClientTransport retain their last input buffer after the caller consumes it. A long-lived idle transport can therefore keep a large frame or HTTP response alive until the next refill or response.
Release the backing storage when input is exhausted, including when a later read operation raises. Replace the backing string so returned data stays valid. Preserve unread and pipelined bytes, Header reply metadata, HTTP status and existing close/error behavior. Regression coverage exercises ownership, read entry points, failures and real HTTP/TCP round trips.
## Benchmarks
Compared master `926dcc971c97aa935f00bc98848352e905c4c755` with that runtime plus this transport patch. Ruby 4.0.7, aarch64 Linux, pure Ruby, YJIT disabled, ordinary GC. Seven interleaved fresh processes per variant and workload; values below are medians. Setup is outside the timer; reading and byte verification are inside it. GC runs after the producer/consumer thread exits, while the transport remains alive.
| Transport / workload | Master ms | Proposed ms | Time delta | Retained String bytes, master → proposed |
|---|---:|---:|---:|---:|
| Buffered, 8 MiB in 16 KiB reads | 14.518 | 16.138 | +11.16% | 33,608 → 800 |
| Buffered, 262,144 byte reads | 33.465 | 37.394 | +11.74% | 9,032 → 800 |
| Framed, 8 MiB in 16 KiB reads | 9.903 | 9.181 | −7.29% | 8,389,453 → 800 |
| Framed, 262,144 byte reads | 34.530 | 37.763 | +9.36% | 262,989 → 800 |
This trades a per-read exhaustion check and empty-buffer allocations for earlier reclamation. Timing is a microbenchmark with verification overhead and host noise; the Framed chunk result is not a general speedup claim. Retained bytes are the process-wide String allocation delta after GC, including fixed runtime overhead; they are not RSS. The transport's exhausted input buffer was empty in every proposed run. Header/HTTP latency and native-extension execution are outside this timing comparison.
Both variants used current master's MemoryBufferTransport and JSONProtocol; the other baseline transport sources match master. Save the following as `/tmp/thrift-6434-bench.rb` and run from the repository root on current master and on current master with this patch applied:
```sh
for trial in 1 2 3 4 5 6 7; do
for kind in buffered framed; do
for operation in chunk byte; do
ruby --disable-yjit -Ilib/rb/lib /tmp/thrift-6434-bench.rb "$kind" "$operation"
done
done
done
```
<details>
<summary>Benchmark script</summary>
```ruby
require 'json'
require 'objspace'
$LOADED_FEATURES << 'thrift_native.rb'
require 'thrift'
kind, operation = ARGV
size = operation == 'byte' ? 262_144 : 8_388_608
klass = kind == 'buffered' ? Thrift::BufferedTransport : Thrift::FramedTransport
GC.start
initial = ObjectSpace.count_objects_size.fetch(:T_STRING)
elapsed, transport = Thread.new do
wire = (kind == 'framed' ? [size].pack('N') : ''.b) + 'x'.b * size
reader = klass.new(Thrift::MemoryBufferTransport.new(wire))
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
if operation == 'byte'
sum = 0
size.times { sum += reader.read_byte }
raise 'incorrect bytes' unless sum == 120 * size
else
(size / 16_384).times do
raise 'incorrect bytes' unless reader.read_all(16_384) == 'x'.b * 16_384
end
end
[(Process.clock_gettime(Process::CLOCK_MONOTONIC) - start) * 1000, reader]
end.value
GC.start
retained = ObjectSpace.count_objects_size.fetch(:T_STRING) - initial
puts JSON.generate(kind: kind, operation: operation, ms: elapsed,
retained_strings: retained, input_bytes: transport.instance_variable_get(:@rbuf).bytesize)
```
</details>
<!-- We recommend you review the checklist/tips before submitting a pull request. -->
- [x] Did you create an [Apache Jira](https://issues.apache.org/jira/projects/THRIFT/issues/) ticket? [THRIFT-6434](https://issues.apache.org/jira/browse/THRIFT-6434). ([Request account here](https://selfserve.apache.org/jira-account.html), not required for trivial changes)
- [x] If a ticket exists: Does your pull request title follow the pattern "THRIFT-NNNN: describe my issue"?
- [x] Did you squash your changes to a single commit? (not required, but preferred)
- [x] Did you do your best to avoid breaking changes? If one was needed, did you label the Jira ticket with "Breaking-Change"?
- [ ] If your change does not involve any code, include `[skip ci]` anywhere in the commit message to free up build resources.
<!--
The Contributing Guide at:
https://github.com/apache/thrift/blob/master/CONTRIBUTING.md
has more details and tips for committing properly.
-->
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@thrift.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] THRIFT-6434: Release exhausted input buffers in Ruby transports [thrift]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #4024:
URL: https://github.com/apache/thrift/pull/4024#discussion_r4308159071
##########
lib/rb/lib/thrift/transport/framed_transport.rb:
##########
@@ -91,6 +95,8 @@ def read_into_buffer(buffer, size)
i += 1
end
i
+ ensure
+ clear_consumed_read_buffer if @rbuf && @index >= @rbuf.size
Review Comment:
This cleanup misses a refill failure within `read_into_buffer`. If a complete frame is followed by a length prefix whose payload read raises, `read_frame` resets `@index` to zero before replacing `@rbuf`. The guard then leaves the exhausted previous frame retained and readable again. Clear the consumed buffer before refilling, and add a regression that crosses a frame boundary and fails while reading the next payload.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@thrift.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] THRIFT-6434: Release exhausted input buffers in Ruby transports [thrift]
Posted by "kpumuk (via GitHub)" <gi...@apache.org>.
kpumuk merged PR #4024:
URL: https://github.com/apache/thrift/pull/4024
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@thrift.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org