[PATCH v9 06/11] perf symbol: Add symbols__shrink() and symbols__try_delete_unannotated()

From: Ian Rogers

Date: Mon Nov 23 2026 - 22:29:45 EST


Add symbol-container memory shrinking helpers to struct symbols:
- symbols__shrink(): Reclaims the secondary name-sorted index
(symbols_by_name, rebuilt on demand by dso__sort_by_name()) and trims
unused over-allocated tail capacity in symbols->symbols while keeping
all live struct symbol entries intact.
- symbols__try_delete_unannotated(): Deletes all symbols under
symbols->lock when none have entries in the annotations hashmap, running
an optional pre_delete callback before freeing the symbols.

Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/perf/util/symbol.c | 61 ++++++++++++++++++++++++++++++++++++++++
tools/perf/util/symbol.h | 3 ++
2 files changed, 64 insertions(+)

diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
index f1abe3333ac0..1bcc36e011fc 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -253,6 +253,67 @@ void symbols__exit(struct symbols *symbols)
exit_rwsem(&symbols->lock);
}

+bool symbols__try_delete_unannotated(struct symbols *symbols,
+ void (*pre_delete)(void *ctx), void *ctx)
+{
+ down_write(&symbols->lock);
+ for (unsigned int i = 0; i < symbols->cnt; i++) {
+ if (symbol__annotated(symbols->symbols[i])) {
+ up_write(&symbols->lock);
+ return false;
+ }
+ }
+
+ if (pre_delete)
+ pre_delete(ctx);
+
+ symbols__delete_locked(symbols);
+ up_write(&symbols->lock);
+ return true;
+}
+
+/*
+ * Reclaim auxiliary memory from @symbols without freeing live symbol
+ * entries (whose lifetimes are owned by the containing struct dso):
+ * - Free the secondary name-sorted index (@symbols_by_name), which is
+ * rebuilt on demand by dso__sort_by_name().
+ * - Trim unused over-allocated tail capacity in @symbols->symbols.
+ *
+ * Returns the number of buffers reclaimed/shrunk.
+ */
+size_t symbols__shrink(struct symbols *symbols)
+{
+ size_t freed = 0;
+
+ down_write(&symbols->lock);
+
+ if (symbols->symbols_by_name) {
+ zfree(&symbols->symbols_by_name);
+ freed++;
+ }
+
+ if (symbols->cnt == 0) {
+ if (symbols->allocated > 0) {
+ zfree(&symbols->symbols);
+ symbols->sorted_cnt = 0;
+ symbols->sorted_tail_cnt = 0;
+ symbols->allocated = 0;
+ freed++;
+ }
+ } else if (symbols->cnt < symbols->allocated) {
+ struct symbol **shrunk = realloc(symbols->symbols,
+ symbols->cnt * sizeof(struct symbol *));
+ if (shrunk) {
+ symbols->symbols = shrunk;
+ symbols->allocated = symbols->cnt;
+ freed++;
+ }
+ }
+
+ up_write(&symbols->lock);
+ return freed;
+}
+
/*
* Order symbols by start address only, preserving insertion order among
* symbols sharing the same start address (matching the previous rbtree
diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h
index 1aa54c1f1680..dd9be561618d 100644
--- a/tools/perf/util/symbol.h
+++ b/tools/perf/util/symbol.h
@@ -153,6 +153,9 @@ void symbol__delete(struct symbol *sym);
void symbols__init(struct symbols *symbols);
void symbols__exit(struct symbols *symbols);
void symbols__delete(struct symbols *symbols);
+bool symbols__try_delete_unannotated(struct symbols *symbols,
+ void (*pre_delete)(void *ctx), void *ctx);
+size_t symbols__shrink(struct symbols *symbols);
void symbols__sort_read_lock(struct symbols *symbols) __acquires_shared(&symbols->lock);

static inline u8 symbol__type(const struct symbol *sym)
--
2.56.1.809.ga0b61035b5-goog