Skip to content

Commit bcb8936

Browse files
byroottenderlove
andcommitted
BufferedIO: Use a buffer with Encoding.default_external
redis-client when parsing a string first attempt to encode it as `Encoding.default_external` and if invalid, fallbacks to encode it as binary. By having the buffer encoded with the default external encoding we save having to change the encoding once on every parsed string. Co-authored-by: Aaron Patterson <[email protected]>
1 parent 41b3abe commit bcb8936

File tree

4 files changed

+25
-27
lines changed

4 files changed

+25
-27
lines changed

benchmark/drivers_ruby.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,53 +7,53 @@ redis-server: `Redis server v=7.0.12 sha=00000000:0 malloc=libc bits=64 build=a1
77

88
```
99
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [arm64-darwin23]
10-
hiredis: 5346.6 i/s
11-
ruby: 2984.3 i/s - 1.79x slower
10+
hiredis: 5369.2 i/s
11+
ruby: 3095.8 i/s - 1.73x slower
1212
1313
```
1414

1515
### large string x 100
1616

1717
```
1818
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [arm64-darwin23]
19-
hiredis: 304.2 i/s
20-
ruby: 204.1 i/s - 1.49x slower
19+
hiredis: 303.9 i/s
20+
ruby: 217.9 i/s - 1.39x slower
2121
2222
```
2323

2424
### small list x 100
2525

2626
```
2727
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [arm64-darwin23]
28-
hiredis: 2612.0 i/s
29-
ruby: 1240.8 i/s - 2.11x slower
28+
hiredis: 2706.0 i/s
29+
ruby: 1325.4 i/s - 2.04x slower
3030
3131
```
3232

3333
### large list
3434

3535
```
3636
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [arm64-darwin23]
37-
hiredis: 6772.7 i/s
38-
ruby: 1540.7 i/s - 4.40x slower
37+
hiredis: 6827.5 i/s
38+
ruby: 1755.0 i/s - 3.89x slower
3939
4040
```
4141

4242
### small hash x 100
4343

4444
```
4545
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [arm64-darwin23]
46-
hiredis: 3293.2 i/s
47-
ruby: 1234.0 i/s - 2.67x slower
46+
hiredis: 3453.2 i/s
47+
ruby: 1359.5 i/s - 2.54x slower
4848
4949
```
5050

5151
### large hash
5252

5353
```
5454
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [arm64-darwin23]
55-
hiredis: 1421.7 i/s
56-
ruby: 1481.0 i/s - same-ish: difference falls within error
55+
hiredis: 1655.5 i/s
56+
ruby: 1666.5 i/s - same-ish: difference falls within error
5757
5858
```
5959

benchmark/drivers_yjit.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,53 +7,53 @@ redis-server: `Redis server v=7.0.12 sha=00000000:0 malloc=libc bits=64 build=a1
77

88
```
99
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) +YJIT [arm64-darwin23]
10-
hiredis: 6723.1 i/s
11-
ruby: 5507.5 i/s - 1.22x slower
10+
hiredis: 6795.5 i/s
11+
ruby: 5696.7 i/s - 1.19x slower
1212
1313
```
1414

1515
### large string x 100
1616

1717
```
1818
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) +YJIT [arm64-darwin23]
19-
hiredis: 290.8 i/s
20-
ruby: 335.2 i/s - same-ish: difference falls within error
19+
hiredis: 291.0 i/s
20+
ruby: 339.3 i/s - same-ish: difference falls within error
2121
2222
```
2323

2424
### small list x 100
2525

2626
```
2727
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) +YJIT [arm64-darwin23]
28-
hiredis: 3686.7 i/s
29-
ruby: 2437.1 i/s - 1.51x slower
28+
hiredis: 3661.1 i/s
29+
ruby: 2351.8 i/s - 1.56x slower
3030
3131
```
3232

3333
### large list
3434

3535
```
3636
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) +YJIT [arm64-darwin23]
37-
hiredis: 6725.9 i/s
38-
ruby: 4990.0 i/s - 1.35x slower
37+
hiredis: 6839.3 i/s
38+
ruby: 5330.8 i/s - 1.28x slower
3939
4040
```
4141

4242
### small hash x 100
4343

4444
```
4545
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) +YJIT [arm64-darwin23]
46-
hiredis: 3893.0 i/s
47-
ruby: 2994.7 i/s - 1.30x slower
46+
hiredis: 3966.9 i/s
47+
ruby: 3243.4 i/s - 1.22x slower
4848
4949
```
5050

5151
### large hash
5252

5353
```
5454
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) +YJIT [arm64-darwin23]
55-
hiredis: 4303.3 i/s
56-
ruby: 4244.6 i/s - same-ish: difference falls within error
55+
hiredis: 4609.7 i/s
56+
ruby: 4612.8 i/s - same-ish: difference falls within error
5757
5858
```
5959

lib/redis_client/ruby_connection/buffered_io.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class BufferedIO
1212

1313
def initialize(io, read_timeout:, write_timeout:, chunk_size: 4096)
1414
@io = io
15-
@buffer = "".b
15+
@buffer = "".dup.force_encoding(Encoding.default_external)
1616
@offset = 0
1717
@chunk_size = chunk_size
1818
@read_timeout = read_timeout

lib/redis_client/ruby_connection/resp3.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ def parse(io)
144144

145145
def parse_string(io)
146146
str = io.gets_chomp
147-
str.force_encoding(Encoding.default_external)
148147
str.force_encoding(Encoding::BINARY) unless str.valid_encoding?
149148
str.freeze
150149
end
@@ -221,7 +220,6 @@ def parse_blob(io)
221220
return if bytesize < 0 # RESP2 nil type
222221

223222
str = io.read_chomp(bytesize)
224-
str.force_encoding(Encoding.default_external)
225223
str.force_encoding(Encoding::BINARY) unless str.valid_encoding?
226224
str
227225
end

0 commit comments

Comments
 (0)