-
-
Notifications
You must be signed in to change notification settings - Fork 380
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(DTO): Add DTO codegen backend #2388
Conversation
Apply some micro-optimizations to _transfer_instance_data
Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>
…actored) (#2176) 'Refactored by Sourcery' Co-authored-by: Sourcery AI <>
Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>
Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>
Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>
Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>
Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>
Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>
Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>
…efactored) (#2230) 'Refactored by Sourcery' Co-authored-by: Sourcery AI <>
Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>
Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>
Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>
Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>
Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>
Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>
Signed-off-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>
Given that these are hidden behind a feature flag, is there any reason not to move forward with this now? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dont trust me
Actually no, you're right (= |
Kudos, SonarCloud Quality Gate passed! |
Documentation preview will be available shortly at https://litestar-org.github.io/litestar-docs-preview/2388 |
Pull Request Checklist
Add a new
_DTOCodegenBackend
that aims to improve performance by generating optimised transfer functions ahead of time.Some performance impressions:
(times shown are for encoding the same data set 100,000 times)
What it does
Mapping
or regular object types, specific field types, which also involves avoid callingisinstance
and friends)try/except
block is faster if we expect no exception to be raised most of the time, and ahasattr
/<field name> in <object>
is faster for the average case where a field might not be present. Since we can guess which case we'll hit more frequently on average (by checking whether it's optional/excluded/UNSET
/Optional[]
) we can use the most performant method for every individual fieldExamples
This is the core loop of the regular DTO backend that we perform these operations for each field, every time we transfer data:
Assuming our source is a
Mapping
, and we don't expect an optional value, we can generate the following code for this:The actual code will differ from this, as for example
_transfer_type_data
will be inlined as well with the appropriate function for the type and theif
branch might be skipped as well, but this gives a good impression of what sort of things can be done.Below is a full example of the generated code with some formatting and comments added.
Full example
Limitations
Inlining for nested collections isn't possible; Instead, for each collection a separate function is generated which will be called within a comprehension of the appropriate type. In theory this could still be optimised for collections of known size (e.g. tuples or collections that have a constraint on their length).
Drawbacks
This implementation is a bit harder to reason about than the regular backend and therefore might increase the maintenance effort.