Class: Quicsilver::Protocol::Qpack::Encoder

Inherits:
Object
  • Object
show all
Defined in:
lib/quicsilver/protocol/qpack/encoder.rb

Constant Summary collapse

STATIC_TABLE =
Protocol::STATIC_TABLE
STATIC_LOOKUP_FULL =

Pre-built hash for O(1) static table lookups

{}
STATIC_LOOKUP_NAME =

“name0value” => index

{}
PREFIX =
"\x00\x00".b.freeze
FIELD_CACHE_MAX =
512
BLOCK_CACHE_MAX =
128

Instance Method Summary collapse

Constructor Details

#initialize(huffman: true) ⇒ Encoder

Returns a new instance of Encoder.



25
26
27
28
29
30
# File 'lib/quicsilver/protocol/qpack/encoder.rb', line 25

def initialize(huffman: true)
  @huffman = huffman
  @field_cache = {}
  @block_cache = {}
  @oid_cache = {}
end

Instance Method Details

#encode(headers) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/quicsilver/protocol/qpack/encoder.rb', line 32

def encode(headers)
  # Fastest path: exact same object as last call
  return @last_result if headers.equal?(@last_headers)

  # Fast path: check object_id cache (same array object reused)
  oid = headers.object_id
  cached = @oid_cache[oid]
  if cached
    @last_headers = headers
    @last_result = cached
    return cached
  end

  if headers.is_a?(Array) && headers.size <= 16
    # Content-based caching for small header sets
    block_key = headers.map { |n, v| "#{n}\0#{v}" }.join("\x01")
    cached_block = @block_cache[block_key]
    if cached_block
      @oid_cache[oid] = cached_block if @oid_cache.size < BLOCK_CACHE_MAX
      return cached_block
    end

    result = encode_fields(headers)
    result_frozen = result.freeze
    if @block_cache.size < BLOCK_CACHE_MAX
      @block_cache[block_key.freeze] = result_frozen
    end
    @oid_cache[oid] = result_frozen if @oid_cache.size < BLOCK_CACHE_MAX
    return result_frozen
  end

  encode_fields(headers)
end

#encode_prefixObject



121
122
123
# File 'lib/quicsilver/protocol/qpack/encoder.rb', line 121

def encode_prefix
  PREFIX.dup
end

#encode_str(value) ⇒ Object

Public API for encoding a single string value (used by tests/external code)



126
127
128
129
130
# File 'lib/quicsilver/protocol/qpack/encoder.rb', line 126

def encode_str(value)
  out = "".b
  encode_str_into(out, value.to_s)
  out
end

#lookup(name, value) ⇒ Object



112
113
114
115
116
117
118
119
# File 'lib/quicsilver/protocol/qpack/encoder.rb', line 112

def lookup(name, value)
  name = name.to_s.downcase
  value = value.to_s
  full_idx = STATIC_LOOKUP_FULL["#{name}\0#{value}"]
  return [full_idx, true] if full_idx
  name_idx = STATIC_LOOKUP_NAME[name]
  name_idx ? [name_idx, false] : nil
end