puma_http11.c: pre-intern env keys#3825
Merged
Conversation
Contributor
Author
|
Actually, I just realized the "common fields" weren't interned either, the gain is much more substantial with them: |
Contributor
Author
|
Somehow TruffleRuby has I'll try to find time tomorrow to fix this. |
26d9567 to
51a9de0
Compare
Contributor
Author
|
Alright, I fixed the Truffle Ruby version. |
byroot
commented
Nov 18, 2025
joshuay03
approved these changes
Nov 18, 2025
51a9de0 to
f340c37
Compare
f340c37 to
7c2bdea
Compare
As explained in https://byroot.github.io/ruby/json/2025/01/12/optimizing-ruby-json-part-6.html when inserting a string key in a hash, Ruby will try to lookup an equivalent string in the interned string table. As such, in C extensions, it's generally advantageous to pre-intern that string, or at least to pre-freeze it. ``` ruby 3.4.6 (2025-09-16 revision dbd83256b1) +YJIT +PRISM [arm64-darwin24] Warming up -------------------------------------- after 30.315k i/100ms Calculating ------------------------------------- after 302.824k (± 1.5%) i/s (3.30 μs/i) - 1.516M in 5.006506s Comparison: before: 265747.8 i/s after: 302823.6 i/s - 1.14x faster ``` ```ruby require "bundler/inline" gemfile do source "https://rubygems.org" gem "benchmark-ips" gem "puma", path: "." end request = <<~HTTP.split("\n").join("\r\n").freeze GET /puma/puma HTTP/1.1 Host: github.com User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:144.0) Gecko/20100101 Firefox/144.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-US,en;q=0.5 Accept-Encoding: gzip, deflate, br, zstd Connection: keep-alive Cookie: user_session=Dsdfsdfdsfdsfdsfdsfdsfdfdfs; __Host-user_session_same_site=Zya7Q7Zndsfsdfdsfdsfdsfdsfdsf; logged_in=yes; dotcom_user=george; _octo=GH1.1.1796688136.1746271857; _device_id=dsfsdfsdfdsfdsfdsf1fb6c729; color_mode=%7B%22color_mode%22%3A%22auto%22%2C%22light_theme%22%3A%7B%22name%22%3A%22light%22%2C%22color_mode%22%3A%22light%22%7D%2C%22dark_theme%22%3A%7B%22name%22%3A%22dark%22%2C%22color_mode%22%3A%22dark%22%7D%7D; GHCC=Required:1-Analytics:0-SocialMedia:0-Advertising:0; cpu_bucket=lg; preferred_color_mode=dark; tz=Europe%2FDublin; _gh_sess=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA Upgrade-Insecure-Requests: 1 Sec-Fetch-Dest: document Sec-Fetch-Mode: navigate Sec-Fetch-Site: cross-site If-None-Match: W/"249a8aa7212f877b7ba603815b6f03d7" Priority: u=0, i HTTP def parse(request) http = Puma::HttpParser.new http.execute({}, request, 0) http.finished? end STAGE = ENV["STAGE"] Benchmark.ips do |x| x.report(STAGE || "parse") do parse(request + "\r\n\r\n") end if STAGE x.save!("/tmp/bench-puma-parser") end x.compare!(order: :baseline) end ```
7c2bdea to
d22e370
Compare
Collaborator
|
Thank you! |
13 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
As explained in https://byroot.github.io/ruby/json/2025/01/12/optimizing-ruby-json-part-6.html when inserting a string key in a hash, Ruby will try to lookup an equivalent string in the interned string table.
As such, in C extensions, it's generally advantageous to pre-intern that string, or at least to pre-freeze it.
The gain is minor, but measurable. The gain is ~17% once common fields are interned too.