Skip to content

Commit

Permalink
Make Huffman methods class methods to avoid unnecessary allocations. (
Browse files Browse the repository at this point in the history
  • Loading branch information
froydnj authored Jul 28, 2024
1 parent 0731294 commit 33ad58a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/protocol/hpack/compressor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def huffman
# @return [String] binary string
def write_string(string, huffman = self.huffman)
if huffman != :never
encoded = Huffman.new.encode(string)
encoded = Huffman.encode(string)

if huffman == :shorter and encoded.bytesize >= string.bytesize
encoded = nil
Expand Down
2 changes: 1 addition & 1 deletion lib/protocol/hpack/decompressor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def read_string

raise CompressionError, "Invalid string length, got #{string.bytesize}, expecting #{length}!" unless string.bytesize == length

string = Huffman.new.decode(string) if huffman
string = Huffman.decode(string) if huffman

return string.force_encoding(Encoding::UTF_8)
end
Expand Down
4 changes: 2 additions & 2 deletions lib/protocol/hpack/huffman.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Huffman
#
# @param str [String]
# @return [String] binary string
def encode(str)
def self.encode(str)
bitstring = str.each_byte.map {|chr| ENCODE_TABLE[chr]}.join
bitstring << '1' * ((8 - bitstring.size) % 8)
[bitstring].pack('B*')
Expand All @@ -33,7 +33,7 @@ def encode(str)
# @param buf [Buffer]
# @return [String] binary string
# @raise [CompressionError] when Huffman coded string is malformed
def decode(buffer)
def self.decode(buffer)
emit = String.new.b
state = 0 # start state

Expand Down
20 changes: 9 additions & 11 deletions test/protocol/hpack/huffman.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
require 'protocol/hpack/huffman'

describe Protocol::HPACK::Huffman do
let(:huffman) {subject.new}

huffman_examples = [ # plain, encoded
['www.example.com', 'f1e3c2e5f23a6ba0ab90f4ff'],
['no-cache', 'a8eb10649cbf'],
Expand All @@ -20,15 +18,15 @@
with '#encode' do
huffman_examples.each do |plain, encoded|
it "should encode #{plain} into #{encoded}" do
expect(huffman.encode(plain).unpack1('H*')).to be == encoded
expect(subject.encode(plain).unpack1('H*')).to be == encoded
end
end
end

with '#decode' do
huffman_examples.each do |plain, encoded|
it "should decode #{encoded} into #{plain}" do
expect(huffman.decode([encoded].pack('H*'))).to be == plain
expect(subject.decode([encoded].pack('H*'))).to be == plain
end
end

Expand All @@ -42,16 +40,16 @@
'UTF-8でエンコードした日本語文字列',
].each do |string|
it "should encode then decode '#{string}' into the same" do
encoded = huffman.encode(string.b)
expect(huffman.decode(encoded)).to be == string.b
encoded = subject.encode(string.b)
expect(subject.decode(encoded)).to be == string.b
end
end

it 'should encode/decode all_possible 2-byte sequences' do
(2**16).times do |n|
string = [n].pack('V')[0, 2].b

expect(huffman.decode(huffman.encode(string))).to be == string
expect(subject.decode(subject.encode(string))).to be == string
end
end

Expand All @@ -60,7 +58,7 @@
encoded = [encoded].pack('H*')

expect do
huffman.decode(encoded[0...-1].b)
subject.decode(encoded[0...-1].b)
end.to raise_exception(Protocol::HPACK::CompressionError, message: be =~ /EOS invalid/)
end

Expand All @@ -70,7 +68,7 @@
encoded = [encoded].pack('H*')

expect do
huffman.decode(encoded.b)
subject.decode(encoded.b)
end.to raise_exception(Protocol::HPACK::CompressionError, message: be =~ /EOS invalid/)
end

Expand All @@ -80,7 +78,7 @@
encoded = [encoded].pack('H*')

expect do
huffman.decode(encoded.b)
subject.decode(encoded.b)
end.to raise_exception(Protocol::HPACK::CompressionError, message: be =~ /EOS invalid/)
end

Expand All @@ -89,7 +87,7 @@
encoded = ['1c7fffffffff'].pack('H*')

expect do
huffman.decode(encoded.b)
subject.decode(encoded.b)
end.to raise_exception(Protocol::HPACK::CompressionError, message: be =~ /EOS found/)
end
end
Expand Down

0 comments on commit 33ad58a

Please sign in to comment.