Skip to content

Settings as a Struct (internal) - #207

Merged
HoneyryderChuck merged 6 commits into
igrigorik:mainfrom
HoneyryderChuck:settings-struct
Sep 10, 2026
Merged

Settings as a Struct (internal)#207
HoneyryderChuck merged 6 commits into
igrigorik:mainfrom
HoneyryderChuck:settings-struct

Conversation

@HoneyryderChuck

@HoneyryderChuck HoneyryderChuck commented Sep 10, 2026

Copy link
Copy Markdown
Collaborator

change internal representation of settings to a struct (instead of a plain hash), as it has better performance and storage capabilities.

STRUCT_SETTINGS = HTTP2::Settings.new
HASH_SETTINGS = {
  settings_header_table_size: 4096,
  settings_enable_push: 1, # enabled for servers
  settings_max_concurrent_streams: 100,
  settings_initial_window_size: 65_535,
  settings_max_frame_size: 16_384,
  settings_max_header_list_size: (2 << 30) - 1 # unlimited
}.freeze

uts "Struct size: #{ObjectSpace.memsize_of(STRUCT_SETTINGS)}"
puts "Hash size: #{ObjectSpace.memsize_of(HASH_SETTINGS)}"

Benchmark.ips do |x|
  # Configure the number of seconds used during
  # the warmup phase (default 2) and calculation phase (default 5)
  x.config(warmup: 2, time: 5)

  x.report("Struct lookup") do
    STRUCT_SETTINGS.settings_initial_window_size
  end

  x.report("Hash Lookup") do
    HASH_SETTINGS[:settings_initial_window_size]
  end

  # Compare the iterations per second of the various reports!
  x.compare!
end
Struct size: 80
Hash size: 160

ruby 4.0.6 (2026-07-14 revision 03b6d3f889) +PRISM [x86_64-darwin24]
Warming up --------------------------------------
       Struct lookup     1.928M i/100ms
         Hash Lookup     1.630M i/100ms
Calculating -------------------------------------
       Struct lookup     18.927M (± 8.6%) i/s   (52.83 ns/i) -     96.400M in   5.093237s
         Hash Lookup     16.252M (± 1.4%) i/s   (61.53 ns/i) -     81.504M in   5.015049s

Comparison:
Struct lookup: 18927000.6 i/s
  Hash Lookup: 16251855.2 i/s - 1.16x  slower

Closes #199 199

this changes the internal representation of settings to a struct,
instead of a hash. The advantages are clearer APIs and type definitions,
better performance (set/get are faster), as well as better storage.
not standard gem anymore
it may receive anything with stream 0
@HoneyryderChuck
HoneyryderChuck merged commit 12c5329 into igrigorik:main Sep 10, 2026
9 of 10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The 1.2.2 source gem contains an untracked stale settings.rb

1 participant