Skip to content

Commit 09791ed

Browse files
authored
ext/pdo: Convert database_object_handle zval to zend_object* (#17629)
This saves 8 bytes on the PDO statement struct. We change the PGSQL PDO driver API to take a zend_object* instead of a zval* at the same time.
1 parent ef10339 commit 09791ed

File tree

6 files changed

+22
-19
lines changed

6 files changed

+22
-19
lines changed

ext/pdo/pdo_dbh.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ void pdo_throw_exception(unsigned int driver_errcode, char *driver_errmsg, pdo_e
6767

6868
PDO_API bool php_pdo_stmt_valid_db_obj_handle(const pdo_stmt_t *stmt)
6969
{
70-
return !Z_ISUNDEF(stmt->database_object_handle)
71-
&& IS_OBJ_VALID(EG(objects_store).object_buckets[Z_OBJ_HANDLE(stmt->database_object_handle)])
72-
&& !(OBJ_FLAGS(Z_OBJ(stmt->database_object_handle)) & IS_OBJ_FREE_CALLED);
70+
return stmt->database_object_handle != NULL
71+
&& IS_OBJ_VALID(EG(objects_store).object_buckets[stmt->database_object_handle->handle])
72+
&& !(OBJ_FLAGS(stmt->database_object_handle) & IS_OBJ_FREE_CALLED);
7373
}
7474

7575
void pdo_raise_impl_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, pdo_error_type sqlstate, const char *supp) /* {{{ */
@@ -657,7 +657,8 @@ PHP_METHOD(PDO, prepare)
657657
stmt->default_fetch_type = dbh->default_fetch_type;
658658
stmt->dbh = dbh;
659659
/* give it a reference to me */
660-
ZVAL_OBJ_COPY(&stmt->database_object_handle, &dbh_obj->std);
660+
GC_ADDREF(&dbh_obj->std);
661+
stmt->database_object_handle = &dbh_obj->std;
661662
/* we haven't created a lazy object yet */
662663
ZVAL_UNDEF(&stmt->lazy_object_ref);
663664

@@ -1222,7 +1223,8 @@ PHP_METHOD(PDO, query)
12221223
stmt->default_fetch_type = dbh->default_fetch_type;
12231224
stmt->dbh = dbh;
12241225
/* give it a reference to me */
1225-
ZVAL_OBJ_COPY(&stmt->database_object_handle, &dbh_obj->std);
1226+
GC_ADDREF(&dbh_obj->std);
1227+
stmt->database_object_handle = &dbh_obj->std;
12261228
/* we haven't created a lazy object yet */
12271229
ZVAL_UNDEF(&stmt->lazy_object_ref);
12281230

@@ -1252,8 +1254,8 @@ PHP_METHOD(PDO, query)
12521254
/* something broke */
12531255
dbh->query_stmt = stmt;
12541256
ZVAL_OBJ(&dbh->query_stmt_zval, Z_OBJ_P(return_value));
1255-
Z_DELREF(stmt->database_object_handle);
1256-
ZVAL_UNDEF(&stmt->database_object_handle);
1257+
GC_DELREF(stmt->database_object_handle);
1258+
stmt->database_object_handle = NULL;
12571259
PDO_HANDLE_STMT_ERR();
12581260
} else {
12591261
PDO_HANDLE_DBH_ERR();

ext/pdo/pdo_stmt.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2021,7 +2021,7 @@ static zend_function *dbstmt_method_get(zend_object **object_pp, zend_string *me
20212021
/* not a pre-defined method, nor a user-defined method; check
20222022
* the driver specific methods */
20232023
if (!stmt->dbh->cls_methods[PDO_DBH_DRIVER_METHOD_KIND_STMT]) {
2024-
if (!pdo_hash_methods(Z_PDO_OBJECT_P(&stmt->database_object_handle),
2024+
if (!pdo_hash_methods(php_pdo_dbh_fetch_object(stmt->database_object_handle),
20252025
PDO_DBH_DRIVER_METHOD_KIND_STMT)
20262026
|| !stmt->dbh->cls_methods[PDO_DBH_DRIVER_METHOD_KIND_STMT]) {
20272027
goto out;
@@ -2048,7 +2048,7 @@ static HashTable *dbstmt_get_gc(zend_object *object, zval **gc_data, int *gc_cou
20482048
enum pdo_fetch_type default_fetch_mode = stmt->default_fetch_type & ~PDO_FETCH_FLAGS;
20492049

20502050
zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();
2051-
zend_get_gc_buffer_add_zval(gc_buffer, &stmt->database_object_handle);
2051+
zend_get_gc_buffer_add_obj(gc_buffer, stmt->database_object_handle);
20522052
if (default_fetch_mode == PDO_FETCH_INTO) {
20532053
zend_get_gc_buffer_add_obj(gc_buffer, stmt->fetch.into);
20542054
} else if (default_fetch_mode == PDO_FETCH_CLASS) {
@@ -2107,8 +2107,9 @@ PDO_API void php_pdo_free_statement(pdo_stmt_t *stmt)
21072107

21082108
do_fetch_opt_finish(stmt, 1);
21092109

2110-
if (!Z_ISUNDEF(stmt->database_object_handle)) {
2111-
zval_ptr_dtor(&stmt->database_object_handle);
2110+
if (stmt->database_object_handle != NULL) {
2111+
OBJ_RELEASE(stmt->database_object_handle);
2112+
stmt->database_object_handle = NULL;
21122113
}
21132114
zend_object_std_dtor(&stmt->std);
21142115
}

ext/pdo/php_pdo_driver.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ struct _pdo_stmt_t {
578578
struct pdo_column_data *columns;
579579

580580
/* we want to keep the dbh alive while we live, so we own a reference */
581-
zval database_object_handle;
581+
zend_object *database_object_handle;
582582
pdo_dbh_t *dbh;
583583

584584
/* keep track of bound input parameters. Some drivers support

ext/pdo_pgsql/pgsql_driver.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,21 +200,21 @@ const php_stream_ops pdo_pgsql_lob_stream_ops = {
200200
NULL
201201
};
202202

203-
php_stream *pdo_pgsql_create_lob_stream(zval *dbh, int lfd, Oid oid)
203+
php_stream *pdo_pgsql_create_lob_stream(zend_object *dbh, int lfd, Oid oid)
204204
{
205205
php_stream *stm;
206206
struct pdo_pgsql_lob_self *self = ecalloc(1, sizeof(*self));
207-
pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)(Z_PDO_DBH_P(dbh))->driver_data;
207+
pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)(php_pdo_dbh_fetch_inner(dbh))->driver_data;
208208

209-
ZVAL_COPY_VALUE(&self->dbh, dbh);
209+
ZVAL_OBJ(&self->dbh, dbh);
210210
self->lfd = lfd;
211211
self->oid = oid;
212212
self->conn = H->server;
213213

214214
stm = php_stream_alloc(&pdo_pgsql_lob_stream_ops, self, 0, "r+b");
215215

216216
if (stm) {
217-
Z_ADDREF_P(dbh);
217+
GC_ADDREF(dbh);
218218
zend_hash_index_add_ptr(H->lob_streams, php_stream_get_resource_id(stm), stm->res);
219219
return stm;
220220
}
@@ -1116,7 +1116,7 @@ void pgsqlLOBOpen_internal(INTERNAL_FUNCTION_PARAMETERS)
11161116
lfd = lo_open(H->server, oid, mode);
11171117

11181118
if (lfd >= 0) {
1119-
php_stream *stream = pdo_pgsql_create_lob_stream(ZEND_THIS, lfd, oid);
1119+
php_stream *stream = pdo_pgsql_create_lob_stream(Z_OBJ_P(ZEND_THIS), lfd, oid);
11201120
if (stream) {
11211121
php_stream_to_zval(stream, return_value);
11221122
return;

ext/pdo_pgsql/pgsql_statement.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ static int pgsql_stmt_get_col(pdo_stmt_t *stmt, int colno, zval *result, enum pd
661661
/* If column was bound as LOB, return a stream. */
662662
int loid = lo_open(S->H->server, oid, INV_READ);
663663
if (loid >= 0) {
664-
php_stream *stream = pdo_pgsql_create_lob_stream(&stmt->database_object_handle, loid, oid);
664+
php_stream *stream = pdo_pgsql_create_lob_stream(stmt->database_object_handle, loid, oid);
665665
if (stream) {
666666
php_stream_to_zval(stream, result);
667667
return 1;

ext/pdo_pgsql/php_pdo_pgsql_int.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ enum pdo_pgsql_specific_constants {
112112
PGSQL_TRANSACTION_UNKNOWN = PQTRANS_UNKNOWN
113113
};
114114

115-
php_stream *pdo_pgsql_create_lob_stream(zval *pdh, int lfd, Oid oid);
115+
php_stream *pdo_pgsql_create_lob_stream(zend_object *pdh, int lfd, Oid oid);
116116
extern const php_stream_ops pdo_pgsql_lob_stream_ops;
117117

118118
void pdo_pgsql_cleanup_notice_callback(pdo_pgsql_db_handle *H);

0 commit comments

Comments
 (0)