Skip to content

Commit 0af2a04

Browse files
committed
Remove ownership model for strings.
1 parent 0b6deff commit 0af2a04

File tree

4 files changed

+40
-15
lines changed

4 files changed

+40
-15
lines changed

benchmark/string.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
def generator
3+
100000.times do |i|
4+
yield "foo #{i}"
5+
end
6+
end
7+
8+
def consumer_without_clear
9+
buffer = String.new
10+
generator do |chunk|
11+
buffer << chunk
12+
end
13+
return nil
14+
end
15+
16+
def consumer_with_clear
17+
buffer = String.new
18+
generator do |chunk|
19+
buffer << chunk
20+
chunk.clear
21+
end
22+
return nil
23+
end
24+
25+
require 'benchmark'
26+
27+
Benchmark.bm do |x|
28+
x.report("consumer_with_clear") do
29+
consumer_with_clear
30+
GC.start
31+
32+
end
33+
34+
x.report("consumer_without_clear") do
35+
consumer_without_clear
36+
GC.start
37+
end
38+
end

lib/protocol/http/body/readable.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ def join
9696

9797
self.each do |chunk|
9898
buffer << chunk
99-
chunk.clear
10099
end
101100

102101
if buffer.empty?

lib/protocol/http/body/rewindable.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ def read
5454
@index += 1
5555
else
5656
if chunk = super
57-
@chunks << chunk
57+
@chunks << -chunk
5858
@index += 1
5959
end
6060
end
6161

6262
# We dup them on the way out, so that if someone modifies the string, it won't modify the rewindability.
63-
return chunk&.dup
63+
return chunk
6464
end
6565

6666
def rewind

spec/protocol/http/body/rewindable_spec.rb

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,6 @@
2626
let(:source) {Protocol::HTTP::Body::Buffered.new}
2727
subject {described_class.new(source)}
2828

29-
it "doesn't get affected by clearing chunks" do
30-
source.write("Hello World!")
31-
32-
2.times do
33-
chunk = subject.read
34-
expect(chunk).to be == "Hello World!"
35-
chunk.clear
36-
37-
subject.rewind
38-
end
39-
end
40-
4129
it "can write and read data" do
4230
3.times do |i|
4331
source.write("Hello World #{i}")

0 commit comments

Comments
 (0)