[PATCH v2 04/18] perf map_browser: Compute symbol browser index on demand

From: Ian Rogers

Date: Tue Oct 27 2026 - 19:18:42 EST


To wrap struct symbol in DECLARE_RC_STRUCT(symbol) and reduce per-symbol
memory overhead, negative-offset allocations ahead of struct symbol via
symbol_conf.priv_size and symbol__priv() need to be eliminated.

Remove symbol__browser_index() in ui/browsers/map.c and compute the TUI
map browser index on demand in map_browser__search() by walking the map
browser's symbol entries when the user searches with '/'.

While this changes finding a symbol's display row index in
map_browser__search() from O(1) to O(N) in the number of symbols in that
single map, map_browser__search() is only invoked interactively when a
user opens the TUI "Browse map details" view for a map, presses '/',
types a query, and hits ENTER. Even for a large DSO such as vmlinux
(~100k symbols), scanning the map's symbols once per interactive search
takes well under a millisecond (and becomes a flat pointer array scan
once symbols are stored in struct symbols).

Previously, '/' search in the TUI map browser was gated behind
'verbose > 0' (-v) in builtin-c2c.c, builtin-report.c, and
ui/browsers/map.c solely to avoid adding sizeof(u32) to
symbol_conf.priv_size for every struct symbol across all DSOs in normal
runs. With on-demand index computation there is no per-symbol memory
overhead, so remove the verbose > 0 restriction and allow '/' search in
the map browser unconditionally.

Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/perf/builtin-c2c.c | 14 --------------
tools/perf/builtin-report.c | 14 --------------
tools/perf/ui/browsers/map.c | 37 ++++++++++++++++++------------------
3 files changed, 18 insertions(+), 47 deletions(-)

diff --git a/tools/perf/builtin-c2c.c b/tools/perf/builtin-c2c.c
index f51fa23f3bf1..719f47dac2dc 100644
--- a/tools/perf/builtin-c2c.c
+++ b/tools/perf/builtin-c2c.c
@@ -3412,20 +3412,6 @@ static int perf_c2c__report(int argc, const char **argv)

if (ret < 0)
goto out_mem2node;
- /*
- * For searching by name on the "Browse map details".
- * providing it only in verbose mode not to bloat too
- * much struct symbol.
- */
- if (verbose > 0) {
- /*
- * XXX: Need to provide a less kludgy way to ask for
- * more space per symbol, the u32 is for the index on
- * the ui browser.
- * See symbol__browser_index.
- */
- symbol_conf.priv_size += sizeof(u32);
- }
annotation_config__init();
}

diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c
index 29085fdc91e5..6672a4d16529 100644
--- a/tools/perf/builtin-report.c
+++ b/tools/perf/builtin-report.c
@@ -1885,20 +1885,6 @@ int cmd_report(int argc, const char **argv)
ret = symbol__annotation_init();
if (ret < 0)
goto error;
- /*
- * For searching by name on the "Browse map details".
- * providing it only in verbose mode not to bloat too
- * much struct symbol.
- */
- if (verbose > 0) {
- /*
- * XXX: Need to provide a less kludgy way to ask for
- * more space per symbol, the u32 is for the index on
- * the ui browser.
- * See symbol__browser_index.
- */
- symbol_conf.priv_size += sizeof(u32);
- }
annotation_config__init();
}

diff --git a/tools/perf/ui/browsers/map.c b/tools/perf/ui/browsers/map.c
index 47f2743cae26..2f070a53b5cb 100644
--- a/tools/perf/ui/browsers/map.c
+++ b/tools/perf/ui/browsers/map.c
@@ -39,12 +39,6 @@ static void map_browser__write(struct ui_browser *browser, void *nd, int row)
ui_browser__write_nstring(browser, symbol__name(sym), width);
}

-/* FIXME uber-kludgy, see comment on cmd_report... */
-static u32 *symbol__browser_index(struct symbol *browser)
-{
- return ((void *)browser) - sizeof(struct rb_node) - sizeof(u32);
-}
-
static int map_browser__search(struct map_browser *browser)
{
char target[512];
@@ -62,10 +56,21 @@ static int map_browser__search(struct map_browser *browser)
sym = map__find_symbol_by_name(browser->map, target);

if (sym != NULL) {
- u32 *idx = symbol__browser_index(sym);
-
- browser->b.top = &sym->rb_node;
- browser->b.index = browser->b.top_idx = *idx;
+ struct rb_node *nd;
+ u32 idx = 0;
+
+ /*
+ * Walk the map browser's symbol entries to find the matching
+ * symbol node and its display row index, then position the
+ * browser cursor at that entry.
+ */
+ for (nd = rb_first(browser->b.entries); nd; nd = rb_next(nd), ++idx) {
+ if (&sym->rb_node == nd) {
+ browser->b.top = nd;
+ browser->b.index = browser->b.top_idx = idx;
+ break;
+ }
+ }
} else
ui_helpline__fpush("%s not found!", target);

@@ -77,8 +82,7 @@ static int map_browser__run(struct map_browser *browser)
int key;

if (ui_browser__show(&browser->b, dso__long_name(map__dso(browser->map)),
- "Press ESC to exit, %s / to search",
- verbose > 0 ? "" : "restart with -v to use") < 0)
+ "Press ESC to exit, / to search") < 0)
return -1;

while (1) {
@@ -86,9 +90,8 @@ static int map_browser__run(struct map_browser *browser)

switch (key) {
case '/':
- if (verbose > 0)
- map_browser__search(browser);
- /* fall thru */
+ map_browser__search(browser);
+ continue;
default:
ui_browser__warn_unhandled_hotkey(&browser->b, key, 0, NULL);
continue;
@@ -124,10 +127,6 @@ int map__browse(struct map *map)

if (maxaddr < symbol__end(pos))
maxaddr = symbol__end(pos);
- if (verbose > 0) {
- u32 *idx = symbol__browser_index(pos);
- *idx = mb.b.nr_entries;
- }
++mb.b.nr_entries;
}

--
2.56.0.313.g2479322e68-goog