Repository navigation
Skip duplicating the strategy for requests that can't be its own - #1172
Conversation
|
Thanks for digging into this, and for the benchmark and the thorough spec! Some feedback:
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.
f8f7680 to
8d752e3
Compare
|
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: 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):
(I'm also looking into why Hashie on master allocates more than the released version) |
|
#1173 may be relevant to your interests. |
|
I’d be curious to see the results of your benchmarks run against the UPDATE: I re-ran the benchmark:
The two changes stack nicely, and this PR applies cleanly on top of |
sferik
left a comment
There was a problem hiding this comment.
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!
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.
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.
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.
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.
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.
Every strategy is a middleware that sees every request, and
Strategy#calldups 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.#callnow asks the strategy's ownon_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!raisesNoSessionError, as before), test mode is off, there's noother_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 inStrategyor its modules.On master, including the
OmniAuth::Builderfix in c29ad9b, with three strategies, on a request that isn't an auth request (Ruby 3.3.5):That holds both through
OmniAuth::Builderand with each strategy added as its own middleware.The specs check that
#callgives the same response and Rack env keys asdup.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 anilsession still raises.