Skip to content

perf(rails): skip env keys that can't match a tracing header - #395

Open
thomasklemm wants to merge 1 commit into
PostHog:mainfrom
thomasklemm:thomasklemm/tracing-headers-allocations
Open

thomasklemm wants to merge 1 commit into
PostHog:mainfrom
thomasklemm:thomasklemm/tracing-headers-allocations

Conversation

@thomasklemm

Copy link
Copy Markdown

💡 Motivation and Context

When a request has no X-POSTHOG-DISTINCT-ID or X-POSTHOG-SESSION-ID header, as most server-rendered page requests don't, TracingHeaders.extract_header scans the Rack env and builds an upcased, translated copy of every key to compare it. That is two Strings per key for each of the two headers, on every request. While profiling a few Rails apps, this was the top posthog-rails line in Dawarich's request specs.

For an ASCII key, upcase and tr keep its length, so a key whose length matches none of the normalized names cannot match and is now skipped before it is copied. Non-ASCII keys, whose length upcase can change ("st" becomes "ST"), and non-String keys are normalized and compared as before, so Rack env keys match as before. A key object that overrides core methods, such as a to_s that returns something other than its own content, could now be judged differently.

RequestContext#call for a browser request without tracing headers, with the 73-key env a minimal Rails 8.1 app passes to it:

context = PostHog::Rails::RequestContext.new(->(_env) { [200, {}, ["ok"]] })
Benchmark.ips { |x| x.report("RequestContext#call") { context.call(env) } }
benchmark-ips, Ruby 4.0.9, YJIT, one core, best of 3 main branch
RequestContext#call 15.2k i/s 33.6k i/s
Objects and Callgrind instructions per call, Ruby 4.0.9 Objects, main Objects, branch Instructions, main Instructions, branch Change
no JIT 369 93 746,853 455,123 −39.1%
YJIT 369 93 543,683 265,749 −51.1%

💚 How did you test it?

The new spec covers the two kinds of key only the scan finds: a lowercase Rack-style name, and one with a non-ASCII character that upcase turns into two ASCII letters. It fails with a length check that ignores the encoding or forgets the HTTP_ name. I also compared the old and new extract_header on several thousand env shapes (case, HTTP_ prefix, dashes and underscores, frozen, binary and UTF-16 strings, non-ASCII and non-String keys): both return the same value or raise the same error. In Dawarich's request specs, the normalizing line went from 342 to 29 objects per request, with the same test results.

📝 Checklist

  • All commits are signed (see Commit signing)
  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Added a changeset file (.changeset/tracing-headers-allocations.md, posthog-rails patch)

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

Thomas Klemm directed the work; Amp (Claude) found the line with an allocation profiler on a few open-source Rails apps' test suites and prepared the change, the spec and the measurements. A plain length check was rejected because upcase can lengthen or shorten non-ASCII keys, so those keep the old comparison. The agent session is private and can't be linked.

When a tracing header such as X-POSTHOG-SESSION-ID is absent, which is
the case on most server-rendered requests, extract_header falls back to
scanning the Rack env and normalized every key with upcase and tr: two
new Strings per key, for each of the two tracing headers, on every
request (close to 300 objects with the 73 env keys a small Rails 8.1
app has at this middleware).

For an ASCII String key, upcase and tr keep its length, so a key whose
length matches none of the normalized target names cannot match one
and is skipped before it is copied. Non-ASCII keys, whose length upcase
can change (e.g. "st" to "ST"), and non-String keys are still normalized
and compared as before, so Rack env keys match as before. Only a key
object whose to_s returns something other than its own content, or
that doesn't respond to is_a?, can now be judged differently.
@thomasklemm
thomasklemm marked this pull request as ready for review November 26, 2026 13:31
@thomasklemm
thomasklemm requested a review from a team as a code owner November 26, 2026 13:31

This branch has not been deployed

No deployments
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.

1 participant