Skip to content

Commit 01398ee

Browse files
committed
Only use FCC for SQLite3 user defined functions
1 parent f6c98e3 commit 01398ee

File tree

2 files changed

+22
-35
lines changed

2 files changed

+22
-35
lines changed

ext/sqlite3/php_sqlite3_structs.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ typedef struct _php_sqlite3_func {
5252
const char *func_name;
5353
int argc;
5454

55-
zval func, step, fini;
56-
struct php_sqlite3_fci afunc, astep, afini;
55+
zend_fcall_info_cache func;
56+
zend_fcall_info_cache step;
57+
zend_fcall_info_cache fini;
5758
} php_sqlite3_func;
5859

5960
/* Structure for SQLite collation function */

ext/sqlite3/sqlite3.c

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -727,13 +727,13 @@ PHP_METHOD(SQLite3, querySingle)
727727
}
728728
/* }}} */
729729

730-
static int sqlite3_do_callback(struct php_sqlite3_fci *fc, zval *cb, int argc, sqlite3_value **argv, sqlite3_context *context, int is_agg) /* {{{ */
730+
static int sqlite3_do_callback(zend_fcall_info_cache *fcc, uint32_t argc, sqlite3_value **argv, sqlite3_context *context, int is_agg) /* {{{ */
731731
{
732732
zval *zargs = NULL;
733733
zval retval;
734-
int i;
735-
int ret;
736-
int fake_argc;
734+
uint32_t i;
735+
uint32_t fake_argc;
736+
zend_result ret = SUCCESS;
737737
php_sqlite3_agg_context *agg_context = NULL;
738738

739739
if (is_agg) {
@@ -742,14 +742,7 @@ static int sqlite3_do_callback(struct php_sqlite3_fci *fc, zval *cb, int argc, s
742742

743743
fake_argc = argc + is_agg;
744744

745-
fc->fci.size = sizeof(fc->fci);
746-
ZVAL_COPY_VALUE(&fc->fci.function_name, cb);
747-
fc->fci.object = NULL;
748-
fc->fci.retval = &retval;
749-
fc->fci.param_count = fake_argc;
750-
751745
/* build up the params */
752-
753746
if (fake_argc) {
754747
zargs = (zval *)safe_emalloc(fake_argc, sizeof(zval), 0);
755748
}
@@ -791,24 +784,17 @@ static int sqlite3_do_callback(struct php_sqlite3_fci *fc, zval *cb, int argc, s
791784
}
792785
}
793786

794-
fc->fci.params = zargs;
795-
796-
if ((ret = zend_call_function(&fc->fci, &fc->fcc)) == FAILURE) {
797-
php_error_docref(NULL, E_WARNING, "An error occurred while invoking the callback");
798-
}
787+
zend_call_known_fcc(fcc, &retval, fake_argc, zargs, /* named_params */ NULL);
799788

789+
/* clean up the params */
800790
if (is_agg) {
801791
zval_ptr_dtor(&zargs[0]);
792+
zval_ptr_dtor(&zargs[1]);
802793
}
803-
804-
/* clean up the params */
805794
if (fake_argc) {
806795
for (i = is_agg; i < argc + is_agg; i++) {
807796
zval_ptr_dtor(&zargs[i]);
808797
}
809-
if (is_agg) {
810-
zval_ptr_dtor(&zargs[1]);
811-
}
812798
efree(zargs);
813799
}
814800

@@ -872,7 +858,7 @@ static void php_sqlite3_callback_func(sqlite3_context *context, int argc, sqlite
872858
{
873859
php_sqlite3_func *func = (php_sqlite3_func *)sqlite3_user_data(context);
874860

875-
sqlite3_do_callback(&func->afunc, &func->func, argc, argv, context, 0);
861+
sqlite3_do_callback(&func->func, argc, argv, context, 0);
876862
}
877863
/* }}}*/
878864

@@ -883,7 +869,7 @@ static void php_sqlite3_callback_step(sqlite3_context *context, int argc, sqlite
883869

884870
agg_context->row_count++;
885871

886-
sqlite3_do_callback(&func->astep, &func->step, argc, argv, context, 1);
872+
sqlite3_do_callback(&func->step, argc, argv, context, 1);
887873
}
888874
/* }}} */
889875

@@ -894,7 +880,7 @@ static void php_sqlite3_callback_final(sqlite3_context *context) /* {{{ */
894880

895881
agg_context->row_count = 0;
896882

897-
sqlite3_do_callback(&func->afini, &func->fini, 0, NULL, context, 1);
883+
sqlite3_do_callback(&func->fini, 0, NULL, context, 1);
898884
}
899885
/* }}} */
900886

@@ -974,7 +960,7 @@ PHP_METHOD(SQLite3, createFunction)
974960
if (sqlite3_create_function(db_obj->db, sql_func, sql_func_num_args, flags | SQLITE_UTF8, func, php_sqlite3_callback_func, NULL, NULL) == SQLITE_OK) {
975961
func->func_name = estrdup(sql_func);
976962

977-
ZVAL_COPY(&func->func, &fci.function_name);
963+
zend_fcc_dup(&func->func, &fcc);
978964

979965
func->argc = sql_func_num_args;
980966
func->next = db_obj->funcs;
@@ -1016,8 +1002,8 @@ PHP_METHOD(SQLite3, createAggregate)
10161002
if (sqlite3_create_function(db_obj->db, sql_func, sql_func_num_args, SQLITE_UTF8, func, NULL, php_sqlite3_callback_step, php_sqlite3_callback_final) == SQLITE_OK) {
10171003
func->func_name = estrdup(sql_func);
10181004

1019-
ZVAL_COPY(&func->step, &step_fci.function_name);
1020-
ZVAL_COPY(&func->fini, &fini_fci.function_name);
1005+
zend_fcc_dup(&func->step, &step_fcc);
1006+
zend_fcc_dup(&func->fini, &fini_fcc);
10211007

10221008
func->argc = sql_func_num_args;
10231009
func->next = db_obj->funcs;
@@ -2202,14 +2188,14 @@ static void php_sqlite3_object_free_storage(zend_object *object) /* {{{ */
22022188

22032189
efree((char*)func->func_name);
22042190

2205-
if (!Z_ISUNDEF(func->func)) {
2206-
zval_ptr_dtor(&func->func);
2191+
if (ZEND_FCC_INITIALIZED(func->func)) {
2192+
zend_fcc_dtor(&func->func);
22072193
}
2208-
if (!Z_ISUNDEF(func->step)) {
2209-
zval_ptr_dtor(&func->step);
2194+
if (ZEND_FCC_INITIALIZED(func->step)) {
2195+
zend_fcc_dtor(&func->step);
22102196
}
2211-
if (!Z_ISUNDEF(func->fini)) {
2212-
zval_ptr_dtor(&func->fini);
2197+
if (ZEND_FCC_INITIALIZED(func->fini)) {
2198+
zend_fcc_dtor(&func->fini);
22132199
}
22142200
efree(func);
22152201
}

0 commit comments

Comments
 (0)