Skip to content

Commit a63613b

Browse files
committed
refactor: use suffix trie for library lookup
Replace suffix hashmap with suffix trie for library lookup.
1 parent 8919767 commit a63613b

File tree

1 file changed

+110
-6
lines changed

1 file changed

+110
-6
lines changed

agent/php_execute.c

Lines changed: 110 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,10 @@ static nr_library_table_t libraries[] = {
557557

558558
static size_t num_libraries = sizeof(libraries) / sizeof(nr_library_table_t);
559559

560+
#define EXT_LEN 4
561+
560562
// find the minimum length of the file_to_check
563+
#if 0
561564
static size_t nr_library_table_min_file_to_check_len(
562565
const nr_library_table_t* table,
563566
size_t num_tables) {
@@ -574,7 +577,92 @@ static size_t nr_library_table_min_file_to_check_len(
574577

575578
return min_len;
576579
}
580+
#endif
581+
582+
typedef struct _nr_suffix_trie_node_t nr_suffix_trie_node_t;
583+
584+
struct _nr_suffix_trie_node_t {
585+
nr_suffix_trie_node_t* children[256];
586+
nr_suffix_trie_node_t* parent;
587+
void* value;
588+
};
589+
590+
static nr_suffix_trie_node_t suffix_trie;
591+
592+
static nr_suffix_trie_node_t* nr_suffix_trie_node_create(void) {
593+
nr_suffix_trie_node_t* node = nr_zalloc(sizeof(nr_suffix_trie_node_t));
594+
return node;
595+
}
596+
597+
static void *nr_suffix_trie_lookup(nr_suffix_trie_node_t* root,
598+
const char* filename,
599+
size_t filename_len) {
600+
nr_suffix_trie_node_t *node = root;
601+
nr_suffix_trie_node_t *parent = NULL;
602+
const char *cp = filename + filename_len - (1 + EXT_LEN);
603+
// nrl_always("Looking up filename=%s", filename);
604+
while(node) {
605+
char c = *cp;
606+
if (nr_isupper(c)) {
607+
c = nr_tolower(c);
608+
}
609+
if (!node->children[c]) {
610+
// nrl_always("No match at %c", c);
611+
break;
612+
}
613+
// nrl_always("Match at %c, current node value=%p, moving back", c, node->value);
614+
parent = node;
615+
node = node->children[c];
616+
cp--;
617+
}
618+
// nrl_always("Returning value=%p", parent? parent->value : NULL);
619+
return parent? parent->value : NULL;
620+
}
621+
622+
static void nr_suffix_trie_add(nr_suffix_trie_node_t* root,
623+
const char* suffix,
624+
size_t suffix_len,
625+
void* value) {
626+
char c;
627+
c = suffix[suffix_len - 1];
628+
// nrl_always("Adding suffix=%s, char=%c", suffix, c);
629+
if (NULL == root->children[c]) {
630+
// nrl_always("Creating new node for char=%c", c);
631+
root->children[c] = nr_suffix_trie_node_create();
632+
root->children[c]->parent = root;
633+
}
634+
if (1 == suffix_len) {
635+
nr_library_table_t* library = (nr_library_table_t*)value;
636+
// nrl_always("Adding library=%s", library->file_to_check);
637+
root->value = value;
638+
return;
639+
}
640+
else {
641+
nr_suffix_trie_add(root->children[c], suffix, suffix_len - 1, value);
642+
}
643+
}
644+
645+
static void nr_suffix_trie_add_library(nr_suffix_trie_node_t* root, nr_library_table_t* library) {
646+
const char *suffix = library->file_to_check;
647+
size_t suffix_len = library->file_to_check_len - EXT_LEN;
648+
// char* suffix_sans_ext = nr_alloca(suffix_len + 1);
649+
// nr_strxcpy(suffix_sans_ext, suffix, suffix_len);
650+
// nrl_always("Adding suffix to trie=%s", suffix_sans_ext);
651+
nr_suffix_trie_add(root, suffix, suffix_len, library);
652+
}
577653

654+
static void nr_suffix_trie_destroy(nr_suffix_trie_node_t* root) {
655+
for (int i = 0; i < 256; i++) {
656+
if (root->children[i]) {
657+
nr_suffix_trie_destroy(root->children[i]);
658+
}
659+
}
660+
if (root->parent) {
661+
nr_free(root);
662+
}
663+
}
664+
665+
#if 0
578666
#include "util_hash.h"
579667
#include "util_hashmap_private.h"
580668

@@ -588,6 +676,7 @@ typedef struct _nr_suffix_lookup_hashmap {
588676
static nr_suffix_lookup_hashmap_t library_lookup = {
589677
.log2_num_buckets = 8,
590678
};
679+
#endif
591680
#if 0
592681
static nr_suffix_lookup_hashmap_t logging_library_lookup = {
593682
.log2_num_buckets = 8,
@@ -597,9 +686,7 @@ static nr_suffix_lookup_hashmap_t vuln_mgmt_package_lookup = {
597686
.log2_num_buckets = 8,
598687
};
599688
#endif
600-
601-
#define EXT_LEN 4
602-
689+
#if 0
603690
size_t nr_suffix_lookup_hashmap_hash_key(nr_suffix_lookup_hashmap_t* hashmap,
604691
const char* filename,
605692
const size_t filename_len) {
@@ -680,6 +767,7 @@ void* nr_suffix_lookup_hashmap_get(nr_suffix_lookup_hashmap_t* hashmap,
680767
}
681768
return NULL;
682769
}
770+
#endif
683771

684772
#if 0
685773
// clang-format: off
@@ -754,16 +842,26 @@ nr_status_t nr_suffix_lookup_hashmap_add_package(nr_suffix_lookup_hashmap_t* has
754842
#endif
755843

756844
void nr_php_execute_minit() {
845+
#if 0
757846
library_lookup.h = nr_hashmap_create_buckets(1 << library_lookup.log2_num_buckets, NULL);
758847
library_lookup.suffix_len = nr_library_table_min_file_to_check_len(libraries, num_libraries);
759848

760849
for (size_t i = 0; i < num_libraries; i++) {
761850
nr_suffix_lookup_hashmap_add_library(&library_lookup, &libraries[i]);
762851
}
852+
#else
853+
for (size_t i = 0; i < num_libraries; i++) {
854+
nr_suffix_trie_add_library(&suffix_trie, &libraries[i]);
855+
}
856+
#endif
763857
}
764858

765859
void nr_php_execute_mshutdown() {
860+
#if 0
766861
nr_hashmap_destroy(&library_lookup.h);
862+
#else
863+
nr_suffix_trie_destroy(&suffix_trie);
864+
#endif
767865
}
768866

769867
/*
@@ -1082,9 +1180,15 @@ static inline void nr_execute_handle_library(const char* filename,
10821180
}
10831181
}
10841182
#else
1085-
nr_library_table_t* library = (nr_library_table_t *) nr_suffix_lookup_hashmap_get(&library_lookup, filename, filename_len);
1086-
if (library && library->handler) {
1087-
library->handler(library);
1183+
//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);
1185+
if (library){
1186+
if (!nr_striendswith(STR_AND_LEN(filename), NR_PSTR(".php"))) {
1187+
return;
1188+
}
1189+
if (library->handler) {
1190+
library->handler(library);
1191+
}
10881192
}
10891193
#endif
10901194
}

0 commit comments

Comments
 (0)