From 1297cf79768e41566f6e3574309bbd23ab55d250 Mon Sep 17 00:00:00 2001 From: Matteo Beccati Date: Thu, 27 Aug 2020 09:58:07 +0200 Subject: [PATCH 1/3] Optimize dispatch_param_event allowing to skip unused pdo_param_event(s) --- ext/pdo/pdo_stmt.c | 4 ++++ ext/pdo/php_pdo_driver.h | 5 ++++- ext/pdo_mysql/mysql_driver.c | 7 +++++++ ext/pdo_pgsql/pgsql_driver.c | 5 +++++ ext/pdo_sqlite/sqlite_driver.c | 3 +++ 5 files changed, 23 insertions(+), 1 deletion(-) diff --git a/ext/pdo/pdo_stmt.c b/ext/pdo/pdo_stmt.c index 96f7574638e88..b0f7bbeede813 100644 --- a/ext/pdo/pdo_stmt.c +++ b/ext/pdo/pdo_stmt.c @@ -92,6 +92,10 @@ static int dispatch_param_event(pdo_stmt_t *stmt, enum pdo_param_event event_typ struct pdo_bound_param_data *param; HashTable *ht; + if (stmt->dbh->skip_param_evt & (1 << event_type)) { + return 1; + } + if (!stmt->methods->param_hook) { return 1; } diff --git a/ext/pdo/php_pdo_driver.h b/ext/pdo/php_pdo_driver.h index e0b4b3986fc34..960ddec4efe08 100644 --- a/ext/pdo/php_pdo_driver.h +++ b/ext/pdo/php_pdo_driver.h @@ -466,9 +466,12 @@ struct _pdo_dbh_t { /* when set, convert int/floats to strings */ unsigned stringify:1; + /* bitmap for pdo_param_event(s) to skip in dispatch_param_event */ + unsigned skip_param_evt:7; + /* the sum of the number of bits here and the bit fields preceding should * equal 32 */ - unsigned _reserved_flags:21; + unsigned _reserved_flags:14; /* data source string used to open this handle */ const char *data_source; diff --git a/ext/pdo_mysql/mysql_driver.c b/ext/pdo_mysql/mysql_driver.c index cb9a50667346a..9596bdd04f45c 100644 --- a/ext/pdo_mysql/mysql_driver.c +++ b/ext/pdo_mysql/mysql_driver.c @@ -617,6 +617,13 @@ static int pdo_mysql_handle_factory(pdo_dbh_t *dbh, zval *driver_options) dbh->driver_data = H; + dbh->skip_param_evt = + 1 << PDO_PARAM_EVT_FREE | + 1 << PDO_PARAM_EVT_EXEC_POST | + 1 << PDO_PARAM_EVT_FETCH_PRE | + 1 << PDO_PARAM_EVT_FETCH_POST | + 1 << PDO_PARAM_EVT_NORMALIZE; + #ifndef PDO_USE_MYSQLND H->max_buffer_size = 1024*1024; #endif diff --git a/ext/pdo_pgsql/pgsql_driver.c b/ext/pdo_pgsql/pgsql_driver.c index 27af12544eab7..38ea304a30e9b 100644 --- a/ext/pdo_pgsql/pgsql_driver.c +++ b/ext/pdo_pgsql/pgsql_driver.c @@ -1192,6 +1192,11 @@ static int pdo_pgsql_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{{ H = pecalloc(1, sizeof(pdo_pgsql_db_handle), dbh->is_persistent); dbh->driver_data = H; + dbh->skip_param_evt = + 1 << PDO_PARAM_EVT_EXEC_POST | + 1 << PDO_PARAM_EVT_FETCH_PRE | + 1 << PDO_PARAM_EVT_FETCH_POST; + H->einfo.errcode = 0; H->einfo.errmsg = NULL; diff --git a/ext/pdo_sqlite/sqlite_driver.c b/ext/pdo_sqlite/sqlite_driver.c index dcbc604739607..26fa1c00eb829 100644 --- a/ext/pdo_sqlite/sqlite_driver.c +++ b/ext/pdo_sqlite/sqlite_driver.c @@ -773,6 +773,9 @@ static int pdo_sqlite_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{ H->einfo.errmsg = NULL; dbh->driver_data = H; + /* skip all but this one param event */ + dbh->skip_param_evt = 0x7F ^ (1 << PDO_PARAM_EVT_EXEC_PRE); + filename = make_filename_safe(dbh->data_source); if (!filename) { From 658f6ff2dae42c4f2b5c6f8d6adc8c8f09b52b47 Mon Sep 17 00:00:00 2001 From: Matteo Beccati Date: Thu, 27 Aug 2020 16:48:00 +0200 Subject: [PATCH 2/3] Convert emulated bool param to 't'/'f' only once --- ext/pdo_pgsql/pgsql_statement.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/pdo_pgsql/pgsql_statement.c b/ext/pdo_pgsql/pgsql_statement.c index 0edb3d7f8e8d9..88031622a4384 100644 --- a/ext/pdo_pgsql/pgsql_statement.c +++ b/ext/pdo_pgsql/pgsql_statement.c @@ -397,7 +397,7 @@ static int pgsql_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data * } break; } - } else if (param->is_param) { + } else if (param->is_param && event_type == PDO_PARAM_EVT_NORMALIZE) { /* We need to manually convert to a pg native boolean value */ if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_BOOL && ((param->param_type & PDO_PARAM_INPUT_OUTPUT) != PDO_PARAM_INPUT_OUTPUT)) { From 1c42682a8e71e2d37a52d7f78f3d13c379a859c9 Mon Sep 17 00:00:00 2001 From: Matteo Beccati Date: Sat, 29 Aug 2020 09:46:53 +0200 Subject: [PATCH 3/3] Apply optimization to pdo_oci * pdo_dblib doesn't use param_hook * pdo_firebird is messy and seems to even use the NORMALIZE constant in a wrong context --- ext/pdo_oci/oci_driver.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ext/pdo_oci/oci_driver.c b/ext/pdo_oci/oci_driver.c index 3cb2cab56d559..d2115b81b80f3 100644 --- a/ext/pdo_oci/oci_driver.c +++ b/ext/pdo_oci/oci_driver.c @@ -724,6 +724,11 @@ static int pdo_oci_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{{ * H = pecalloc(1, sizeof(*H), dbh->is_persistent); dbh->driver_data = H; + dbh->skip_param_evt = + 1 << PDO_PARAM_EVT_FETCH_PRE | + 1 << PDO_PARAM_EVT_FETCH_POST | + 1 << PDO_PARAM_EVT_NORMALIZE; + H->prefetch = PDO_OCI_PREFETCH_DEFAULT; /* allocate an environment */