Repository navigation
Work out each declared param's key and empty value when the endpoint compiles - #3074
Open
ericproulx wants to merge 1 commit into
Open
ericproulx wants to merge 1 commit into
ericproulx wants to merge 1 commit into
Conversation
ericproulx
force-pushed
the
perf/declared-params-compiled
branch
3 times, most recently
from
November 10, 2026 10:52
5043c22 to
b58229a
Compare
…compiles `declared` answers, for each declared param, the key it is filed under (its name, or the one `as:` gave it) and the empty value it stands for when it was not passed (an Array, a Set or a Hash, after its documented type). Neither depends on the request, and both were worked out again on every call: the path to the param built to look its new name up, its documented name spelled out to look its type up, that type matched against the ones that have an empty value, and for a Hash every documented param scanned for a child. `Grape::DeclaredParams` holds them per endpoint, built by `Endpoint#compile!` as `Validations::UndeclaredParams` is, and `DeclaredParamsHandler` walks its params. A param a `desc` documents by hand with a type that is not a String, or with no Hash of details, has no empty value. `declared` raised on it when it was not passed. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
ericproulx
force-pushed
the
perf/declared-params-compiled
branch
from
November 11, 2026 10:46
b58229a to
47ccfd2
Compare
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.
declaredanswers, for each param an endpoint declares, the key it is filed under (its name, or the oneas:gave it) and the empty value it stands for when it was not sent (an Array, a Set or a Hash, after its documented type). Neither depends on the request, yet both were worked out again on every call, for every declared param:address[geo][lat]) was spelled out to look its type up,Grape::DeclaredParamsnow holds those answers per endpoint.Endpoint#compile!builds it, as it buildsValidations::UndeclaredParams, andDeclaredParamsHandlerwalks its params against the ones the request sent. An empty value is copied on every call, so no two requests share one.Numbers
Interleaved A/B from one checkout, median of per-round pairs over 6 rounds. In parentheses, the rounds the branch won.
POST, 23 params declared, 4 of the 15 top-level ones sent,declared(params)declared(params, include_missing: false)POST, 10 params with a Hash and an Array of three Hashes, all sent,declared(params)PUTby id, 9 params, one a Hash, all sent,declared(params, include_missing: false)declared(control)On the first row
declareditself goes from 29.9 to about 13 µs on the interpreter.Cost
The params are compiled for every endpoint, whether or not it calls
declared: nothing at compile time says that it will. On an API of 600 endpoints declaring 12 params each, compiling goes from 289 to 300 ms and retained memory from 11.6 to 12.2 MB.What changes
Compared with master over random declarations, payloads and every combination of
declared's options, about 900,000 calls: the same answer wherever master answered. In two cases master raised, and the branch answers:desc ..., params:documents by hand with atypethat is not a String (a Class, say), or with something other than a Hash of details, has no empty value.declaredraisedNoMethodErrororTypeErrorwhen such a param was not sent.configurationhas no name while the API is called unmounted.declaredraisedNoMethodErroronnil.to_symthere, and now files the param undernil. Mounted, nothing changes.DeclaredParamsHandler#calltakes the compiled params in place of the declared params, the documented ones and the renames. Nothing else in Grape, grape-entity or grape-swagger calls it.No contract change, so no UPGRADING entry.
🤖 Generated with Claude Code