fix(zod-openapi): reduce route type instantiations#1988
Open
thekiwi wants to merge 1 commit into
Open
Conversation
🦋 Changeset detectedLatest commit: d9561a8 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1988 +/- ##
=======================================
Coverage 92.05% 92.05%
=======================================
Files 115 115
Lines 4065 4065
Branches 1059 1059
=======================================
Hits 3742 3742
Misses 287 287
Partials 36 36
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Description
This PR reduces the cost of type-checking apps that use
createRoute,.openapi()andRouteHandlerby restructuring a few of the route-typing generics. There is no runtime change, the emitted JS is identical. The inferred route/handler/response/RPC types are also unchanged.Relates to #1918.
Problem
The route-typing generics re-derive several expensive sub-expressions on every use:
ExtractStatusCode<Status>(which expands ranges like'5XX') is re-computed in every response branch, dragging a freshTypedResponsecascade through each comparison....['content'][keyof ...]['schema']) ~6 times per input type.On large RPC apps, this makes type-checking more expensive than it needs to be.
Solution
Bind each expensive sub-expression once (
infer ... extends ...) and share the duplicated pieces, with no change to the resulting types:ExtractStatusCode<Status>once per response branch and reuse the inferredContent(the bulk of the win)ComputeInput<R>and the response-shape conditional via a newHandlerResponse<R>acrossRouteHandler/RouteHook/openapi()/HandlerFromRoute/HookFromRouteInputTypeBase/InputTypeJson/InputTypeForm/ExtractContentRoutingPath(identical to the existingConvertPathType)Results
While the majority of the benchmarking was done with a sizeable closed-source project, I also generated a 100-route synthetic RPC app (CRUD + auth/upload/search/status) and measured with
tsc --extendedDiagnostics. Instantiations & Types are deterministic; check time / memory are medians of 5 runs.The benchmark file is available here: https://gist.github.com/thekiwi/d85b3a0e5ff467c8ffcf21b9e96a8732
Safety / behaviour-neutral
*.test-d.tssuites passtext/plain/application/*+json/ no-content /defaultresponses, the5XXstatus range, form/header/cookie input, and the typed-vs-raw-Responsehandler returnThe author should do the following, if applicable
yarn changesetat the top of this repo and push the changeset