From 580889f8cb68b146a076df0b230ebdbc091ed3f7 Mon Sep 17 00:00:00 2001 From: Christian Hergert Date: Mon, 15 May 2023 11:20:44 -0700 Subject: [PATCH] libsysprof-analyze: short-circuit when address > max This adds an O(1) check at the head of the lookup to avoid looking at every RB_RIGHT() in the tree when address falls beyond the upper bound of the interval tree. --- src/libsysprof-analyze/sysprof-symbol-cache.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/libsysprof-analyze/sysprof-symbol-cache.c b/src/libsysprof-analyze/sysprof-symbol-cache.c index 3a8c5792..b657a380 100644 --- a/src/libsysprof-analyze/sysprof-symbol-cache.c +++ b/src/libsysprof-analyze/sysprof-symbol-cache.c @@ -178,6 +178,13 @@ sysprof_symbol_cache_lookup (SysprofSymbolCache *self, node = RB_ROOT(&self->head); + /* The root node contains our calculated max as augmented in RBTree. + * Therefore, we can know if @address falls beyond the upper bound + * in O(1) without having to add a branch to the while loop below. + */ + if (node == NULL || node->max < address) + return NULL; + while (node != NULL) { g_assert (RB_LEFT(node, link) == NULL ||