Skip to content

Skip duplicating the strategy for requests that can't be its own - #1172

Merged
sferik merged 1 commit into
omniauth:masterfrom
sampokuokkanen:skip-dup-off-auth-paths
Oct 21, 2026
Merged

sferik merged 1 commit into
omniauth:masterfrom
sampokuokkanen:skip-dup-off-auth-paths

Conversation

@sampokuokkanen

@sampokuokkanen sampokuokkanen commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Every strategy is a middleware that sees every request, and Strategy#call dups itself (and its options) before #call! looks at the path. On a request that isn't on the strategy's paths, that copy is all the work it does.

#call now asks the strategy's own on_auth_path? first, on a copy that shares the options instead of duplicating them, so the path methods memoize on that copy rather than on the shared instance. If the request isn't on the strategy's paths, it goes straight to the app.

It only does so when the path alone decides what #call! would do: a session is present (otherwise #call! raises NoSessionError, as before), test mode is off, there's no other_phase, the request and callback paths aren't lambdas (they would otherwise run twice on the strategy's own paths), and #call!, #phase_call, #options_call?, #request_call? and #on_auth_path? are OmniAuth's own, wherever they're defined in Strategy or its modules.

On master, including the OmniAuth::Builder fix in c29ad9b, with three strategies, on a request that isn't an auth request (Ruby 3.3.5):

objects/request µs/request
master 145 35
this PR 88 15

That holds both through OmniAuth::Builder and with each strategy added as its own middleware.

The specs check that #call gives the same response and Rack env keys as dup.call! for every combination of option setup (default, custom request and callback paths, path prefix), path (including case, trailing and repeated slashes, and format extensions), SCRIPT_NAME, and GET/POST/OPTIONS. Others check that the strategy is still duplicated in each case above, that nothing is memoized on the shared instance, and that a nil session still raises.

@sferik

sferik commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

Thanks for digging into this, and for the benchmark and the thorough spec! Some feedback:

  1. The biggest cost was elsewhere. Rack::Builder#call ran to_app on every request, rebuilding every strategy in the stack each time. I fixed that on master in c29ad9b, and it should be a much bigger win than skipping the dup. It would be great to see your benchmark rerun on top of it.
  2. It needs a rebase. Strategy has been split into modules on master, and the path methods now live in OmniAuth::Strategy::Paths. So instance_method(m).owner == OmniAuth::Strategy would always be false after a rebase, and the fast path would never run.
  3. The duplicated path matching worries me. passthrough? reimplements request_path, callback_path, and current_path. If the two ever drift apart, requests meant for the strategy will silently go to the app, and the property spec only checks one direction. I'd much rather reuse the real on_request_path?/on_callback_path?, for example by making them work without memoizing onto the shared instance, so there's one source of truth.

If the numbers still justify it after the Builder fix, I'd be happy to review a version along those lines.

Every strategy is a middleware that sees every request, and #call dups
the strategy and its options before #call! looks at the path. On a
request that isn't on the strategy's paths, that copy is all it does.

#call now asks the strategy's own on_auth_path? first, on a copy that
shares the options instead of duplicating them, so the path methods
memoize on it rather than on the shared instance. If env isn't on the
strategy's paths, the request goes straight to the app.

It only does so when the path alone decides what #call! would do: a
session is present, test mode is off, there's no other_phase, the
request and callback paths aren't lambdas, and #call!, #phase_call,
#options_call?, #request_call?, and #on_auth_path? are OmniAuth's own.
@sampokuokkanen
sampokuokkanen force-pushed the skip-dup-off-auth-paths branch from f8f7680 to 8d752e3 Compare October 21, 2026 01:43
@sampokuokkanen

Copy link
Copy Markdown
Contributor Author

Thanks! I reran the benchmark on master with c29ad9b. With three strategies, a request that isn't an auth request still takes 145 objects and ~35µs, so the dup is still most of the cost. This PR brings it to 88 objects and ~15µs.

I rebuilt it on master instead of rebasing. There's no path matching of its own any more: #call asks the real on_auth_path?, on a copy that shares the options, so nothing gets memoized on the shared instance. The override check now accepts methods from Strategy's modules, like Paths. The spec now compares #call against dup.call! for every request, in both directions.

After all that, one hot path seems to actually be in Hashie, so this PR: hashie/hashie#613 should help there if it's merged.

Non-auth request through OmniAuth::Builder with three strategies (Rack 3.2.7):

omniauth hashie 5.1.0 (released) hashie master hashie master + #613
master 145 objects, ~36µs 163 objects, ~34.5µs 115 objects, ~32.6µs
#1172 88 objects, ~15µs 88 objects, ~15.9µs 55 objects, ~15–17µs

(I'm also looking into why Hashie on master allocates more than the released version)

@sferik

sferik commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

#1173 may be relevant to your interests.

@sferik

sferik commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

I’d be curious to see the results of your benchmarks run against the drop-hashie branch.

UPDATE: I re-ran the benchmark:

objects µs/request
master 170 116
this PR 78 44
drop-hashie 97 56
drop-hashie + this PR 78 31

The two changes stack nicely, and this PR applies cleanly on top of drop-hashie.

@sferik sferik left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This addresses everything from my first review. Calling the real on_auth_path? on a copy that shares the options removes my main worry about path matching drifting, and comparing against dup.call! in both directions is exactly the safety net this needed.

After merging, I'll push a small cleanup commit that marks DISPATCH_METHODS and default_dispatch? as private API, and notes in the changelog that methods patched directly onto OmniAuth::Strategy (as opposed to overridden in a subclass) aren't detected.

Thanks for the patch!

@sferik
sferik merged commit 1ee5e75 into omniauth:master Oct 21, 2026
58 checks passed
sferik added a commit that referenced this pull request Oct 21, 2026
DISPATCH_METHODS is now a private constant and default_dispatch? is
tagged @api private, so neither becomes public API that semver would
lock in. The changelog entry for
#1172 now says that methods
patched directly onto OmniAuth::Strategy aren't detected.
sampokuokkanen added a commit to sampokuokkanen/omniauth that referenced this pull request Oct 21, 2026
On a request for the strategy's own paths, #call threw away the copy it
had checked the path on and duplicated the strategy again, so #call!
built a second Rack::Request and worked out the request, callback, and
current paths a second time. Running initialize_copy on the checked copy
completes it into the same copy #dup makes, keeping what it memoized, so
a subclass's initialize_copy still runs on it.

Because #call no longer calls #dup or #initialize_dup on those paths, a
strategy that overrides either now goes through dup.call! for every
request, as it did before omniauth#1172.
sampokuokkanen added a commit to sampokuokkanen/omniauth that referenced this pull request Oct 21, 2026
On a request for the strategy's own paths, #call threw away the copy it
had checked the path on and duplicated the strategy again, so #call!
built a second Rack::Request and worked out the request, callback, and
current paths a second time. Running initialize_copy on the checked copy
completes it into the same copy #dup makes, keeping what it memoized, so
a subclass's initialize_copy still runs on it.

Because #call no longer calls #dup or #initialize_dup on those paths, a
strategy that overrides either now goes through dup.call! for every
request, as it did before omniauth#1172.
sampokuokkanen added a commit to sampokuokkanen/omniauth that referenced this pull request Oct 21, 2026
On a request for the strategy's own paths, #call threw away the copy it
had checked the path on and duplicated the strategy again, so #call!
built a second Rack::Request and worked out the request, callback, and
current paths a second time. Running initialize_copy on the checked copy
completes it into the same copy #dup makes, keeping what it memoized, so
a subclass's initialize_copy still runs on it.

Because #call no longer calls #dup or #initialize_dup on those paths, a
strategy that overrides either now goes through dup.call! for every
request, as it did before omniauth#1172.
sampokuokkanen added a commit to sampokuokkanen/omniauth that referenced this pull request Oct 21, 2026
On a request for the strategy's own paths, #call threw away the copy it
had checked the path on and duplicated the strategy again, so #call!
built a second Rack::Request and worked out the request, callback, and
current paths a second time. Running initialize_copy on the checked copy
completes it into the same copy #dup makes, keeping what it memoized, so
a subclass's initialize_copy still runs on it.

Because #call no longer calls #dup or #initialize_dup on those paths, a
strategy that overrides either now goes through dup.call! for every
request, as it did before omniauth#1172.
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.

2 participants