-
Notifications
You must be signed in to change notification settings - Fork 238
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(query-engine-wasm): [Performance variant] Refactor psl::ValidatedSchema
for query-engine-wasm
to allow byte schema deserialization
#4708
Closed
Conversation
This file contains 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
…query-engine-wasm, and ValidatedSchema for the other query-engine* crates
…isma/prisma-schema-wasm"
…ased on compile-time check
CodSpeed Performance ReportMerging #4708 will not alter performanceComparing Summary
|
WASM Size
|
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.
WORK IN PROGRESS
This is a variant of #4706, optimised for performance and memory safety.
This PR closes https://github.com/prisma/team-orm/issues/892.
This PR deprecates #4696 and #4603.
TODOs:
#![feature(trait_upcasting)]
prisma/prisma-engines
: integration(query-engine-wasm): Refactorpsl::ValidatedSchema
forquery-engine-wasm
to allow byte schema deserialization #4707prisma/prisma
: chore(Automated Integration PR): update engines to 5.10.0-13.feat-qe-specific-psl-performance-b9f77ddd4b02a49b324c20b050de9087e29da4ed prisma#23045Technical Background
As of February 20, 2024, Prisma reads the
schema.prisma
file into a struct calledpsl_core::ValidatedSchema
. This struct is the result of a multi-stage pipeline, as it:schema.prisma
textual contents viapest
, a parser crate dependency that accounts for ~72 KB of the Wasm Query Engine before gzip compression is applied.The
psl_core::ValidatedSchema
struct is ubiquitous, as it’s used by every engine:In reality, all Query Engines, and the Wasm Query Engine in particular, need fewer schema information than what
psl
parses, constructs, and provides. There is thus a size-optimisation opportunity.In particular:
The
configuration: Configuration
field is overkill, as query engines only need to read thepreview_features
contained therein.The
connector: &'static dyn datamodel_connector::Connector
field is also “too powerful” for the query engine, which, for a given active connector, only needs to:capabilities
(e.g., does the connector support autoincrement columns?)DateTime
)relationMode = "prisma"
)nativeJoins
preview feature is in useAlso, most methods exposed by the
datamodel_connector::Connector
are used for validation purposes, .Problems & Past Attempts
ValidatedSchema
value from theString
content of aschema.prisma
file is an expensive operation, both in terms of size and CPU performance. This operation happens when constructing any newPrismaClient
instance, so, on Edge Cloud platforms, it essentially translates to a PSL parsing step per HTTP request.query-engine-wasm
only, see feat(query-engine-wasm): shrink gzipped Wasm size down to 1.3MB #4552), shoving off ~1 MB before gzip, indicating that PSL validation tend to be very expensive in terms of size. Yet, many more validations are currently in place to construct fields likeconfiguration
.pest
by deserializing a byte-encoded Prisma schema instead (see tmp: Try to encode schema into bincode #4603) with only slightly size improvements, but we only restricted our attention to the AST (which is just a portion of thedb: parser_database::ParserDatabase
field ofpsl::ValidatedSchema
). This means that the PrismaClient constructor would still have to perform additional validations and object constructions in order to create apsl::ValidatedSchema
value.Novel Ideas
psl_core::ValidatedSchema
andpsl_core::datamodel_connector::Connector
for Query Engine.psl_core::ValidatedSchema
, not just the AST, but don’t (de)serialize the fields un-needed by the Query Engine.Results of this PR
psl_core::datamodel_connector::Connector
into two traits:ValidatedConnector
, which only exposes the methods required byquery-engine-wasm
. This split accounts for ~14.5 fewer KB after gzip.psl_core::ValidatedSchema
, calledValidatedSchemaForQE
, that only holds what's necessary toquery-engine-wasm
. (De)serialization is handled viaserde
, as usual.ValidSchema
, is also introduced. This was necessary asquery-engine-node-api
andquery-engine
need a few more functionalities and validations thanquery-engine-wasm
, and because every Query Engine shares a few utilities (whose function signatures I have changed frompsl::ValidatedSchema
todyn psl::ValidSchema
).query-engine-wasm
to load aValidatedSchemaForQE
value directly from a byte buffer, avoiding any validation. Of course, we expectprisma generate
to create a binary dump of itsschema.prisma
file, and ensure its validity.compiler-cli
, a playground for byte (de)serialization ofschema.prisma
files.A few of the new names introduced by this PR can probably be improved, clarity-wise. Comments are welcome.