Skip to content

Commit 5e759eb

Browse files
committed
refactor: use suffix trie for framework lookup
1 parent a63613b commit 5e759eb

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

agent/php_execute.c

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,8 @@ struct _nr_suffix_trie_node_t {
587587
void* value;
588588
};
589589

590-
static nr_suffix_trie_node_t suffix_trie;
590+
static nr_suffix_trie_node_t library_suffix_trie;
591+
static nr_suffix_trie_node_t framework_suffix_trie;
591592

592593
static nr_suffix_trie_node_t* nr_suffix_trie_node_create(void) {
593594
nr_suffix_trie_node_t* node = nr_zalloc(sizeof(nr_suffix_trie_node_t));
@@ -651,6 +652,15 @@ static void nr_suffix_trie_add_library(nr_suffix_trie_node_t* root, nr_library_t
651652
nr_suffix_trie_add(root, suffix, suffix_len, library);
652653
}
653654

655+
static void nr_suffix_trie_add_framework(nr_suffix_trie_node_t* root, const nr_framework_table_t* library) {
656+
const char *suffix = library->file_to_check;
657+
size_t suffix_len = library->file_to_check_len - EXT_LEN;
658+
// char* suffix_sans_ext = nr_alloca(suffix_len + 1);
659+
// nr_strxcpy(suffix_sans_ext, suffix, suffix_len);
660+
// nrl_always("Adding suffix to trie=%s", suffix_sans_ext);
661+
nr_suffix_trie_add(root, suffix, suffix_len, library);
662+
}
663+
654664
static void nr_suffix_trie_destroy(nr_suffix_trie_node_t* root) {
655665
for (int i = 0; i < 256; i++) {
656666
if (root->children[i]) {
@@ -851,7 +861,10 @@ void nr_php_execute_minit() {
851861
}
852862
#else
853863
for (size_t i = 0; i < num_libraries; i++) {
854-
nr_suffix_trie_add_library(&suffix_trie, &libraries[i]);
864+
nr_suffix_trie_add_library(&library_suffix_trie, &libraries[i]);
865+
}
866+
for (size_t i = 0; i < num_all_frameworks; i++) {
867+
nr_suffix_trie_add_framework(&framework_suffix_trie, &all_frameworks[i]);
855868
}
856869
#endif
857870
}
@@ -860,7 +873,8 @@ void nr_php_execute_mshutdown() {
860873
#if 0
861874
nr_hashmap_destroy(&library_lookup.h);
862875
#else
863-
nr_suffix_trie_destroy(&suffix_trie);
876+
nr_suffix_trie_destroy(&library_suffix_trie);
877+
nr_suffix_trie_destroy(&framework_suffix_trie);
864878
#endif
865879
}
866880

@@ -1091,34 +1105,31 @@ static nrframework_t nr_try_detect_framework(
10911105
const char* filename,
10921106
const size_t filename_len TSRMLS_DC) {
10931107
nrframework_t detected = NR_FW_UNSET;
1094-
size_t i;
1108+
nr_framework_table_t* framework = NULL;
10951109

1096-
for (i = 0; i < num_frameworks; i++) {
1097-
if (nr_striendswith(STR_AND_LEN(filename),
1098-
STR_AND_LEN(frameworks[i].file_to_check))) {
1110+
framework = (nr_framework_table_t *) nr_suffix_trie_lookup(&framework_suffix_trie, filename, filename_len);
1111+
if (NULL != framework) {
10991112
/*
11001113
* If we have a special check function and it tells us to ignore
11011114
* the file name because some other condition wasn't met, continue
11021115
* the loop.
11031116
*/
1104-
if (frameworks[i].special) {
1117+
if (framework->special) {
11051118
nr_framework_classification_t special
1106-
= frameworks[i].special(filename TSRMLS_CC);
1119+
= framework->special(filename TSRMLS_CC);
11071120

11081121
if (FRAMEWORK_IS_NORMAL == special) {
1109-
continue;
1122+
goto end;
11101123
}
11111124
}
11121125

1113-
nr_framework_log("detected framework", frameworks[i].framework_name);
1126+
nr_framework_log("detected framework", framework->framework_name);
11141127
nrl_verbosedebug(
11151128
NRL_FRAMEWORK, "framework '%s' detected with %s, which ends with %s",
1116-
frameworks[i].framework_name, filename, frameworks[i].file_to_check);
1129+
framework->framework_name, filename, framework->file_to_check);
11171130

1118-
frameworks[i].enable(TSRMLS_C);
1119-
detected = frameworks[i].detected;
1120-
goto end;
1121-
}
1131+
framework->enable(TSRMLS_C);
1132+
detected = framework->detected;
11221133
}
11231134

11241135
end:
@@ -1181,7 +1192,7 @@ static inline void nr_execute_handle_library(const char* filename,
11811192
}
11821193
#else
11831194
//nr_library_table_t* library = (nr_library_table_t *) nr_suffix_lookup_hashmap_get(&library_lookup, filename, filename_len);
1184-
nr_library_table_t* library = (nr_library_table_t *) nr_suffix_trie_lookup(&suffix_trie, filename, filename_len);
1195+
nr_library_table_t* library = (nr_library_table_t *) nr_suffix_trie_lookup(&library_suffix_trie, filename, filename_len);
11851196
if (library){
11861197
if (!nr_striendswith(STR_AND_LEN(filename), NR_PSTR(".php"))) {
11871198
return;

0 commit comments

Comments
 (0)