-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
qe: Capabilities-based size optimizations for WASM engine (#4701)
* qe: Capabilities-based size optimizations for WASM engine Core feature of this PR: `can_have_capability` function in `psl-core` crate. For multi-connector builds it always returns `true` and has no effect. If we are inside of a single connector build, however, it will eveluate value based on the actual connector capabilities. In many cases, this will allow optimizer to completely eliminate following code if connector does not support specific feature. Notable exeception to that were relation joins: for some reason, optimizer can not eliminate those functions from SQLite bundle. So, this is the only place in sql-query-connector where we have to introduce conditionally compiled feature. In order to take maximum advantage of this functionality, we have to disable default features in `quaint` and `psl` crates and enable them only in native engines. Close prisma/team-orm#928 * Fix tests & lints * Fix migrate tests * Fix codspeed * Now for real * FFS * Address some of the feedback * Introduce reachble_only_with_capability macro * Compile `returning` uncoditionally * Restore benchmarks * Fix benchmarks, but preserve all size gains * Sigh
- Loading branch information
Showing
74 changed files
with
458 additions
and
458 deletions.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
93 changes: 93 additions & 0 deletions
93
psl/psl-core/src/builtin_connectors/capabilities_support.rs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
use crate::datamodel_connector::{Connector, ConnectorCapabilities, ConnectorCapability}; | ||
use cfg_if::cfg_if; | ||
|
||
cfg_if! { | ||
// if built only for mysql | ||
if #[cfg(all(feature="mysql", not(any(feature = "postgresql", feature="sqlite", feature = "cockroachdb", feature="mssql", feature="mongodb"))))] { | ||
#[inline(always)] | ||
const fn can_have_capability_impl(capability: ConnectorCapability) -> bool { | ||
check_comptime_capability(super::mysql_datamodel_connector::CAPABILITIES, capability) | ||
} | ||
|
||
pub fn has_capability(_: &dyn Connector, capability: ConnectorCapability) -> bool { | ||
can_have_capability_impl(capability) | ||
} | ||
// if built only for sqlite | ||
} else if #[cfg(all(feature="sqlite", not(any(feature = "postgresql", feature="mysql", feature = "cockroachdb", feature="mssql", feature="mongodb"))))] { | ||
#[inline(always)] | ||
const fn can_have_capability_impl(capability: ConnectorCapability) -> bool { | ||
check_comptime_capability(super::sqlite_datamodel_connector::CAPABILITIES, capability) | ||
} | ||
|
||
#[inline(always)] | ||
pub fn has_capability(_: &dyn Connector, capability: ConnectorCapability) -> bool { | ||
can_have_capability_impl(capability) | ||
} | ||
// if built only for postgresql | ||
} else if #[cfg(all(feature="postgresql", not(any(feature = "sqlite", feature="mysql", feature = "cockroachdb", feature="mssql", feature="mongodb"))))] { | ||
#[inline(always)] | ||
const fn can_have_capability_impl(capability: ConnectorCapability) -> bool { | ||
check_comptime_capability(super::postgres_datamodel_connector::CAPABILITIES, capability) | ||
} | ||
|
||
#[inline(always)] | ||
pub fn has_capability(_: &dyn Connector, capability: ConnectorCapability) -> bool { | ||
can_have_capability_impl(capability) | ||
} | ||
// any other build configuration | ||
} else { | ||
#[inline(always)] | ||
const fn can_have_capability_impl(_: ConnectorCapability) -> bool { | ||
true | ||
} | ||
|
||
#[inline(always)] | ||
pub fn has_capability(connector: &dyn Connector, capability: ConnectorCapability) -> bool { | ||
connector.capabilities().contains(capability) | ||
} | ||
} | ||
} | ||
|
||
/// Helper function for determining if engine, compiled with the current settings, | ||
/// can potentially have provided capability on. Useful for single-connector builds and can | ||
/// be used to exclude certain code that we know for sure can't be executed for current connector. | ||
/// Has no effect on multi-connector builds | ||
/// # Example | ||
/// ```ignore | ||
/// if !can_have_capability(ConnectorCapability::FullTextSearch) { | ||
/// unreachable!() | ||
/// } | ||
/// ... // if compiled for a single connector, optimizer will exclude the following code if connector does not support full text search | ||
/// ``` | ||
#[inline(always)] | ||
pub const fn can_have_capability(cap: ConnectorCapability) -> bool { | ||
can_have_capability_impl(cap) | ||
} | ||
|
||
/// Marks the code as reachable only by the connectors, | ||
/// having the specific capability. | ||
/// Optimizer usually can optimize the code away if none of the connectors | ||
/// current build supports the capability. | ||
/// | ||
/// If we are within a single connector build that has no such capability, | ||
/// and the code marked with this macro is reached, it will panic. | ||
#[macro_export] | ||
macro_rules! reachable_only_with_capability { | ||
($cap: expr) => { | ||
if !$crate::builtin_connectors::can_have_capability($cap) { | ||
core::unreachable!() | ||
} | ||
}; | ||
} | ||
|
||
#[inline(always)] | ||
#[allow(dead_code)] // not used if more than one connector is built | ||
const fn check_comptime_capability(capabilities: ConnectorCapabilities, cap: ConnectorCapability) -> bool { | ||
(capabilities.bits_c() & (cap as u64)) > 0 | ||
} | ||
|
||
#[inline(always)] | ||
pub const fn can_support_relation_load_strategy() -> bool { | ||
can_have_capability(ConnectorCapability::LateralJoin) | ||
|| can_have_capability(ConnectorCapability::CorrelatedSubqueries) | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,64 @@ | ||
#[cfg(feature = "cockroachdb")] | ||
pub mod cockroach_datamodel_connector; | ||
pub mod completions; | ||
|
||
#[cfg(feature = "cockroachdb")] | ||
pub use cockroach_datamodel_connector::CockroachType; | ||
#[cfg(feature = "mongodb")] | ||
pub use mongodb::MongoDbType; | ||
#[cfg(feature = "mssql")] | ||
pub use mssql_datamodel_connector::{MsSqlType, MsSqlTypeParameter}; | ||
#[cfg(feature = "mysql")] | ||
pub use mysql_datamodel_connector::MySqlType; | ||
#[cfg(feature = "postgresql")] | ||
pub use postgres_datamodel_connector::{PostgresDatasourceProperties, PostgresType}; | ||
|
||
mod capabilities_support; | ||
#[cfg(feature = "mongodb")] | ||
mod mongodb; | ||
#[cfg(feature = "mssql")] | ||
mod mssql_datamodel_connector; | ||
#[cfg(feature = "mysql")] | ||
mod mysql_datamodel_connector; | ||
mod native_type_definition; | ||
#[cfg(feature = "postgresql")] | ||
mod postgres_datamodel_connector; | ||
#[cfg(feature = "sqlite")] | ||
mod sqlite_datamodel_connector; | ||
mod utils; | ||
pub use capabilities_support::{can_have_capability, can_support_relation_load_strategy, has_capability}; | ||
|
||
use crate::{datamodel_connector::Connector, ConnectorRegistry}; | ||
use crate::ConnectorRegistry; | ||
|
||
pub const POSTGRES: &'static dyn Connector = &postgres_datamodel_connector::PostgresDatamodelConnector; | ||
pub const COCKROACH: &'static dyn Connector = &cockroach_datamodel_connector::CockroachDatamodelConnector; | ||
pub const MYSQL: &'static dyn Connector = &mysql_datamodel_connector::MySqlDatamodelConnector; | ||
pub const SQLITE: &'static dyn Connector = &sqlite_datamodel_connector::SqliteDatamodelConnector; | ||
pub const MSSQL: &'static dyn Connector = &mssql_datamodel_connector::MsSqlDatamodelConnector; | ||
pub const MONGODB: &'static dyn Connector = &mongodb::MongoDbDatamodelConnector; | ||
#[cfg(feature = "postgresql")] | ||
pub const POSTGRES: &'static dyn crate::datamodel_connector::Connector = | ||
&postgres_datamodel_connector::PostgresDatamodelConnector; | ||
#[cfg(feature = "cockroachdb")] | ||
pub const COCKROACH: &'static dyn crate::datamodel_connector::Connector = | ||
&cockroach_datamodel_connector::CockroachDatamodelConnector; | ||
#[cfg(feature = "mysql")] | ||
pub const MYSQL: &'static dyn crate::datamodel_connector::Connector = | ||
&mysql_datamodel_connector::MySqlDatamodelConnector; | ||
#[cfg(feature = "sqlite")] | ||
pub const SQLITE: &'static dyn crate::datamodel_connector::Connector = | ||
&sqlite_datamodel_connector::SqliteDatamodelConnector; | ||
#[cfg(feature = "mssql")] | ||
pub const MSSQL: &'static dyn crate::datamodel_connector::Connector = | ||
&mssql_datamodel_connector::MsSqlDatamodelConnector; | ||
#[cfg(feature = "mongodb")] | ||
pub const MONGODB: &'static dyn crate::datamodel_connector::Connector = &mongodb::MongoDbDatamodelConnector; | ||
|
||
pub static BUILTIN_CONNECTORS: ConnectorRegistry<'static> = &[POSTGRES, MYSQL, SQLITE, MSSQL, COCKROACH, MONGODB]; | ||
pub static BUILTIN_CONNECTORS: ConnectorRegistry<'static> = &[ | ||
#[cfg(feature = "postgresql")] | ||
POSTGRES, | ||
#[cfg(feature = "mysql")] | ||
MYSQL, | ||
#[cfg(feature = "sqlite")] | ||
SQLITE, | ||
#[cfg(feature = "mssql")] | ||
MSSQL, | ||
#[cfg(feature = "cockroachdb")] | ||
COCKROACH, | ||
#[cfg(feature = "mongodb")] | ||
MONGODB, | ||
]; |
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
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
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
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
Oops, something went wrong.