From 84ef8348d859941f2e8e5a7de2b52cc7a5940989 Mon Sep 17 00:00:00 2001 From: Brian Duranleau Date: Fri, 1 Dec 2023 10:59:21 -0600 Subject: [PATCH] merge #778 --- agent/fw_wordpress.c | 30 ++++++++++++++++++++---------- agent/php_newrelic.h | 1 + agent/php_rshutdown.c | 1 + 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/agent/fw_wordpress.c b/agent/fw_wordpress.c index cd9361ea7..e74b1df5f 100644 --- a/agent/fw_wordpress.c +++ b/agent/fw_wordpress.c @@ -358,6 +358,10 @@ static void nr_wordpress_call_user_func_array(zend_function* func, nr_php_wrap_callable(func, nr_wordpress_wrap_hook TSRMLS_CC); } +static void free_tag(void* tag) { + nr_free(tag); +} + /* * Some plugins generate transient tag names. We can detect these by checking * the substrings returned from our regex rule. If the tag is transient, we @@ -366,8 +370,7 @@ static void nr_wordpress_call_user_func_array(zend_function* func, * Example: (old) add_option__transient_timeout_twccr_382402301f44c883bc0137_cat * (new) add_option__transient_timeout_twccr_*_cat */ -static char* nr_wordpress_clean_tag(const zval* tag TSRMLS_DC) { - char* orig_tag = NULL; +static char* nr_wordpress_clean_tag(const zval* tag) { char* clean_tag = NULL; nr_regex_t* regex = NULL; nr_regex_substrings_t* ss = NULL; @@ -381,8 +384,16 @@ static char* nr_wordpress_clean_tag(const zval* tag TSRMLS_DC) { return NULL; } - orig_tag = nr_strndup(Z_STRVAL_P(tag), Z_STRLEN_P(tag)); - ss = nr_regex_match_capture(regex, orig_tag, nr_strlen(orig_tag)); + if (NULL == NRPRG(wordpress_clean_tag_cache)) { + NRPRG(wordpress_clean_tag_cache) = nr_hashmap_create(free_tag); + } + + if (nr_hashmap_get_into(NRPRG(wordpress_clean_tag_cache), Z_STRVAL_P(tag), + Z_STRLEN_P(tag), (void**)&clean_tag)) { + return clean_tag; + } + + ss = nr_regex_match_capture(regex, Z_STRVAL_P(tag), Z_STRLEN_P(tag)); clean_tag = nr_regex_substrings_get(ss, 5); /* @@ -403,7 +414,9 @@ static char* nr_wordpress_clean_tag(const zval* tag TSRMLS_DC) { } nr_regex_substrings_destroy(&ss); - nr_free(orig_tag); + + nr_hashmap_set(NRPRG(wordpress_clean_tag_cache), Z_STRVAL_P(tag), + Z_STRLEN_P(tag), clean_tag); return clean_tag; } @@ -429,9 +442,8 @@ NR_PHP_WRAPPER(nr_wordpress_exec_handle_tag) { NRPRG(check_cufa) = true; - NRPRG(wordpress_tag) = nr_wordpress_clean_tag(tag TSRMLS_CC); + NRPRG(wordpress_tag) = nr_wordpress_clean_tag(tag); NR_PHP_WRAPPER_CALL; - nr_free(NRPRG(wordpress_tag)); NRPRG(wordpress_tag) = old_tag; if (NULL == NRPRG(wordpress_tag)) { NRPRG(check_cufa) = false; @@ -517,10 +529,8 @@ NR_PHP_WRAPPER(nr_wordpress_apply_filters) { NRPRG(check_cufa) = true; - NRPRG(wordpress_tag) = nr_wordpress_clean_tag(tag TSRMLS_CC); - + NRPRG(wordpress_tag) = nr_wordpress_clean_tag(tag); NR_PHP_WRAPPER_CALL; - nr_free(NRPRG(wordpress_tag)); NRPRG(wordpress_tag) = old_tag; if (NULL == NRPRG(wordpress_tag)) { NRPRG(check_cufa) = false; diff --git a/agent/php_newrelic.h b/agent/php_newrelic.h index d7e4d625d..43b70db77 100644 --- a/agent/php_newrelic.h +++ b/agent/php_newrelic.h @@ -389,6 +389,7 @@ nr_matcher_t* wordpress_theme_matcher; /* Matcher for theme filenames */ nr_matcher_t* wordpress_core_matcher; /* Matcher for plugin filenames */ nr_hashmap_t* wordpress_file_metadata; /* Metadata for plugin and theme names given a filename */ +nr_hashmap_t* wordpress_clean_tag_cache; /* Cached clean tags */ char* doctrine_dql; /* The current Doctrine DQL. Only non-NULL while a Doctrine object is on the stack. */ diff --git a/agent/php_rshutdown.c b/agent/php_rshutdown.c index a7188e66c..ec199a8c5 100644 --- a/agent/php_rshutdown.c +++ b/agent/php_rshutdown.c @@ -100,6 +100,7 @@ int nr_php_post_deactivate(void) { nr_matcher_destroy(&NRPRG(wordpress_core_matcher)); nr_matcher_destroy(&NRPRG(wordpress_theme_matcher)); nr_hashmap_destroy(&NRPRG(wordpress_file_metadata)); + nr_hashmap_destroy(&NRPRG(wordpress_clean_tag_cache)); nr_free(NRPRG(mysql_last_conn)); nr_free(NRPRG(pgsql_last_conn));