Skip to content

Commit

Permalink
reduce # of WordPress plugin/theme name calculation
Browse files Browse the repository at this point in the history
Rather than figuring out the plugin/theme name at runtime, which is slow even
with memoisation in hashmap, we can do it only at the point an action or
filter is added and store it in the wraprec.
  • Loading branch information
lavarou committed Dec 1, 2023
1 parent 70b6513 commit f20420f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
21 changes: 20 additions & 1 deletion agent/fw_wordpress.c
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,9 @@ static char* nr_wordpress_plugin_from_function(zend_function* func TSRMLS_DC) {
}

NR_PHP_WRAPPER(nr_wordpress_wrap_hook) {
#if ZEND_MODULE_API_NO < ZEND_7_4_X_API_NO
zend_function* func = NULL;
#endif
char* plugin = NULL;

NR_UNUSED_SPECIALFN;
Expand All @@ -364,8 +366,12 @@ NR_PHP_WRAPPER(nr_wordpress_wrap_hook) {
if ((0 == NRINI(wordpress_hooks)) || (NULL == NRPRG(wordpress_tag))) {
NR_PHP_WRAPPER_LEAVE;
}
#if ZEND_MODULE_API_NO < ZEND_7_4_X_API_NO
func = nr_php_execute_function(NR_EXECUTE_ORIG_ARGS TSRMLS_CC);
plugin = nr_wordpress_plugin_from_function(func TSRMLS_CC);
#else
plugin = wraprec->wordpress_plugin_theme;
#endif

NR_PHP_WRAPPER_CALL;

Expand Down Expand Up @@ -629,10 +635,23 @@ NR_PHP_WRAPPER(nr_wordpress_add_filter) {
}

if (true == wrap_hook) {
nruserfn_t* callback_wraprec;
zval* callback = nr_php_arg_get(2, NR_EXECUTE_ORIG_ARGS TSRMLS_CC);
/* the callback here can be any PHP callable. nr_php_wrap_generic_callable
* checks that a valid callable is passed */
nr_php_wrap_generic_callable(callback, nr_wordpress_wrap_hook);
callback_wraprec = nr_php_wrap_generic_callable(callback, nr_wordpress_wrap_hook);

// We can cheat here: wraprecs on callables are always transient, so if
// there's a wordpress_plugin_theme set we know it's from this transaction,
// and we don't have any issues around a possible multi-tenant setup.
if (callback_wraprec && NULL == callback_wraprec->wordpress_plugin_theme) {
// Unlike Drupal, we don't free the wordpress_plugin_theme, since we know
// it's transient anyway, and we only set the field if it was previously
// NULL.
zend_function* fn = nr_php_zval_to_function(callback);
callback_wraprec->wordpress_plugin_theme
= nr_wordpress_plugin_from_function(fn);
}
nr_php_arg_release(&callback);
}
}
Expand Down
3 changes: 3 additions & 0 deletions agent/php_user_instrument.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ typedef struct _nruserfn_t {
nr_string_len_t drupal_module_len;
char* drupal_hook;
nr_string_len_t drupal_hook_len;
#if ZEND_MODULE_API_NO >= ZEND_7_4_X_API_NO
char* wordpress_plugin_theme;
#endif
} nruserfn_t;

extern nruserfn_t* nr_wrapped_user_functions; /* a singly linked list */
Expand Down

0 comments on commit f20420f

Please sign in to comment.