From: Ian Rogers <irogers@google.com>
To: Namhyung Kim <namhyung@kernel.org>,
Arnaldo Carvalho de Melo <acme@kernel.org>,
Alireza Haghdoost <haghdoost@uber.com>,
Ian Rogers <irogers@google.com>
Cc: Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>, Jiri Olsa <jolsa@kernel.org>,
Adrian Hunter <adrian.hunter@intel.com>,
James Clark <james.clark@linaro.org>,
linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v9 02/11] perf symbol: Replace sorted boolean with sorted_cnt prefix length
Date: Mon, 23 Nov 2026 19:28:53 -0800 [thread overview]
Message-ID: <20261124032902.3859564-3-irogers@google.com> (raw)
In-Reply-To: <20261124032902.3859564-1-irogers@google.com>
When symbols are appended after an initial sort, re-running full-array
merge sort across the entire symbol table rescans the already-sorted
prefix and allocates a scratch buffer even when only a few symbols were
appended.
Replace the @sorted boolean in struct symbols with @sorted_cnt, the
length of the leading start-address-sorted prefix (so @sorted_cnt ==
@cnt indicates the entire array is sorted):
- Pass first_unsorted to symbols__insertion_sort_range() so
already-sorted prefixes are not rescanned.
- In symbols__sort_locked(), binary-insert in place without allocating
when <= 8 symbols have been appended (or cnt <= 64), and otherwise
sort only the newly appended tail [sorted_cnt .. cnt) before merging
with [0 .. sorted_cnt).
Using 'perf bench internals symbols':
Before:
Average append + lookup: 1551.533 usec (+- 3.379 usec)
After (-90.3%):
Average append + lookup: 150.333 usec (+- 0.273 usec)
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/symbol.c | 75 +++++++++++++++++++++++++++++-----------
tools/perf/util/symbol.h | 21 +++++++----
2 files changed, 68 insertions(+), 28 deletions(-)
diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
index 03c4314935a5..b9ddebdcd900 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -220,8 +220,8 @@ void symbols__init(struct symbols *symbols)
symbols->symbols = NULL;
symbols->symbols_by_name = NULL;
symbols->cnt = 0;
+ symbols->sorted_cnt = 0;
symbols->allocated = 0;
- symbols->sorted = true;
}
static void symbols__delete_locked(struct symbols *symbols)
@@ -233,8 +233,8 @@ static void symbols__delete_locked(struct symbols *symbols)
zfree(&symbols->symbols);
zfree(&symbols->symbols_by_name);
symbols->cnt = 0;
+ symbols->sorted_cnt = 0;
symbols->allocated = 0;
- symbols->sorted = true;
}
void symbols__delete(struct symbols *symbols)
@@ -279,9 +279,10 @@ static unsigned int symbols__upper_bound(struct symbol **arr, unsigned int len,
static void symbols__insertion_sort_range(struct symbol **arr,
unsigned int start,
+ unsigned int first_unsorted,
unsigned int end)
{
- for (unsigned int i = start + 1; i < end; i++) {
+ for (unsigned int i = max(first_unsorted, start + 1); i < end; i++) {
struct symbol *sym = arr[i];
if (!symbol__less_or_equal(arr[i - 1], sym)) {
@@ -355,7 +356,7 @@ static void symbols__sort_range_with_tmp(struct symbol **arr, unsigned int n,
unsigned int i = 0;
if (n <= 32) {
- symbols__insertion_sort_range(arr, 0, n);
+ symbols__insertion_sort_range(arr, 0, 1, n);
return;
}
@@ -379,8 +380,10 @@ static void symbols__sort_range_with_tmp(struct symbol **arr, unsigned int n,
}
if (i - start < 32 && i < n) {
+ unsigned int run_end = i;
+
i = min(start + 32U, n);
- symbols__insertion_sort_range(arr, start, i);
+ symbols__insertion_sort_range(arr, start, run_end, i);
}
runs[nr_runs++] = (struct symbol_run){
@@ -431,22 +434,48 @@ static void symbols__sort_range_with_tmp(struct symbol **arr, unsigned int n,
static void symbols__sort_locked(struct symbols *symbols)
__must_hold(&symbols->lock)
{
- if (symbols->sorted)
+ unsigned int m = symbols->sorted_cnt;
+ unsigned int k;
+
+ if (m >= symbols->cnt) {
+ symbols->sorted_cnt = symbols->cnt;
return;
+ }
+ k = symbols->cnt - m;
- if (symbols->cnt <= 64) {
- symbols__insertion_sort_range(symbols->symbols, 0, symbols->cnt);
- } else {
+ /*
+ * When only a few symbols have been appended since the last sort
+ * (including single-symbol insertions), insert them directly at
+ * their binary-searched sorted positions in O(N) without allocating.
+ */
+ if (k <= 8 || symbols->cnt <= 64) {
+ symbols__insertion_sort_range(symbols->symbols, 0, m, symbols->cnt);
+ symbols->sorted_cnt = symbols->cnt;
+ return;
+ }
+
+ if (m <= 1) {
struct symbol **tmp = malloc(((symbols->cnt + 1) / 2) * sizeof(struct symbol *));
if (tmp) {
symbols__sort_range_with_tmp(symbols->symbols, symbols->cnt, tmp);
free(tmp);
} else {
- symbols__insertion_sort_range(symbols->symbols, 0, symbols->cnt);
+ symbols__insertion_sort_range(symbols->symbols, 0, m, symbols->cnt);
+ }
+ } else {
+ struct symbol **tmp = malloc(k * sizeof(struct symbol *));
+
+ if (tmp) {
+ symbols__sort_range_with_tmp(&symbols->symbols[m], k, tmp);
+ symbols__merge_runs(symbols->symbols, 0, m, symbols->cnt, tmp);
+ free(tmp);
+ } else {
+ symbols__insertion_sort_range(symbols->symbols, 0, m, symbols->cnt);
}
}
- symbols->sorted = true;
+
+ symbols->sorted_cnt = symbols->cnt;
}
/*
@@ -456,7 +485,7 @@ static void symbols__sort_locked(struct symbols *symbols)
void symbols__sort_read_lock(struct symbols *symbols)
{
down_read(&symbols->lock);
- while (!symbols->sorted) {
+ while (symbols->sorted_cnt < symbols->cnt) {
up_read(&symbols->lock);
down_write(&symbols->lock);
symbols__sort_locked(symbols);
@@ -505,6 +534,7 @@ void symbols__fixup_duplicate(struct symbols *symbols)
}
}
symbols->cnt = write_idx + 1;
+ symbols->sorted_cnt = symbols->cnt;
zfree(&symbols->symbols_by_name);
up_write(&symbols->lock);
}
@@ -679,9 +709,10 @@ static int __symbols__insert(struct symbols *symbols, struct symbol *sym)
symbols->allocated = to_allocate;
}
- if (symbols->sorted && symbols->cnt > 0 &&
- !symbol__less_or_equal(symbols->symbols[symbols->cnt - 1], sym))
- symbols->sorted = false;
+ if (symbols->sorted_cnt == symbols->cnt &&
+ (symbols->cnt == 0 ||
+ symbol__less_or_equal(symbols->symbols[symbols->cnt - 1], sym)))
+ symbols->sorted_cnt = symbols->cnt + 1;
symbols->symbols[symbols->cnt++] = sym;
zfree(&symbols->symbols_by_name);
@@ -894,11 +925,13 @@ void dso__delete_symbol(struct dso *dso, struct symbol *sym)
struct symbols *symbols = dso__symbols(dso);
down_write(&symbols->lock);
- for (unsigned int i = 0; i < symbols->cnt; i++) {
+ for (unsigned int i = symbols->cnt; i-- > 0; ) {
if (symbols->symbols[i] == sym) {
memmove(&symbols->symbols[i], &symbols->symbols[i + 1],
(symbols->cnt - i - 1) * sizeof(struct symbol *));
symbols->cnt--;
+ if (i < symbols->sorted_cnt)
+ symbols->sorted_cnt--;
zfree(&symbols->symbols_by_name);
symbol__delete(sym);
break;
@@ -1243,8 +1276,8 @@ static int maps__split_kallsyms_for_kcore(struct maps *kmaps, struct dso *dso)
old_cnt = root->cnt;
root->symbols = NULL;
root->cnt = 0;
+ root->sorted_cnt = 0;
root->allocated = 0;
- root->sorted = true;
zfree(&root->symbols_by_name);
up_write(&root->lock);
@@ -1315,8 +1348,8 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta,
old_allocated = root->allocated;
root->symbols = NULL;
root->cnt = 0;
+ root->sorted_cnt = 0;
root->allocated = 0;
- root->sorted = true;
zfree(&root->symbols_by_name);
up_write(&root->lock);
@@ -1463,8 +1496,8 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta,
if (root->cnt == 0) {
root->symbols = old_symbols;
root->cnt = write_idx;
+ root->sorted_cnt = write_idx;
root->allocated = old_allocated;
- root->sorted = true;
old_symbols = NULL;
} else {
for (unsigned int j = 0; j < write_idx; j++)
@@ -1490,13 +1523,13 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta,
if (root->cnt == 0) {
root->symbols = old_symbols;
root->cnt = write_idx;
+ root->sorted_cnt = (delta == 0) ? write_idx : min(write_idx, 1U);
root->allocated = old_allocated;
- root->sorted = (delta == 0);
old_symbols = NULL;
} else {
for (unsigned int j = 0; j < write_idx; j++)
__symbols__insert(root, old_symbols[j]);
- root->sorted = false;
+ root->sorted_cnt = min(root->cnt, 1U);
}
zfree(&root->symbols_by_name);
up_write(&root->lock);
diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h
index 629f6b2fcd0a..2b20bfb22fd1 100644
--- a/tools/perf/util/symbol.h
+++ b/tools/perf/util/symbol.h
@@ -97,14 +97,18 @@ struct symbol {
/**
* struct symbols - A locked collection of symbols stored in a sorted array.
+ *
+ * The @symbols array of @cnt pointers (within an allocation of @allocated
+ * slots) keeps a start-address-sorted prefix @symbols[0 .. @sorted_cnt)
+ * followed by an optional unsorted tail @symbols[@sorted_cnt .. @cnt) from
+ * bulk symbols__insert() appends. When @sorted_cnt == @cnt, the entire array
+ * is sorted; otherwise the unsorted tail is lazily sorted and merged into the
+ * prefix on lookup or iteration.
*/
struct symbols {
/** @lock: Read/write semaphore synchronizing access to the arrays. */
struct rw_semaphore lock;
- /**
- * @symbols: Dynamically allocated array of @allocated symbol pointers,
- * sorted by start address when @sorted is true.
- */
+ /** @symbols: Dynamically allocated array of @allocated symbol pointers. */
struct symbol **symbols __guarded_by(&lock);
/**
* @symbols_by_name: Optional lazily built array of @cnt symbol pointers
@@ -112,12 +116,15 @@ struct symbols {
* is 0, or when invalidated by a symbol-table mutation.
*/
struct symbol **symbols_by_name __guarded_by(&lock);
- /** @cnt: Number of valid entries in @symbols. */
+ /** @cnt: Total number of valid symbol pointers in @symbols. */
unsigned int cnt __guarded_by(&lock);
+ /**
+ * @sorted_cnt: Length of the leading start-address-sorted prefix in
+ * @symbols. The entire @symbols array is sorted iff @sorted_cnt == @cnt.
+ */
+ unsigned int sorted_cnt __guarded_by(&lock);
/** @allocated: Capacity (in entries) of the @symbols allocation. */
unsigned int allocated __guarded_by(&lock);
- /** @sorted: True when @symbols is sorted by address range. */
- bool sorted __guarded_by(&lock);
};
void symbol__delete(struct symbol *sym);
--
2.56.1.809.ga0b61035b5-goog
next prev parent reply other threads:[~2026-11-24 3:29 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-24 3:28 [PATCH v9 00/11] perf symbol: Sorted array optimizations and memory shrinking Ian Rogers
2026-11-24 3:28 ` [PATCH v9 01/11] perf symbol: Drop num_by_name and sorted_by_name from struct symbols Ian Rogers
2026-11-24 3:28 ` Ian Rogers [this message]
2026-11-24 3:28 ` [PATCH v9 03/11] perf symbol: Add secondary sorted run for dso__insert_symbol() Ian Rogers
2026-11-24 3:28 ` [PATCH v9 04/11] perf test symbols: Add sorted insertion and equal-start alias tests Ian Rogers
2026-11-24 3:28 ` [PATCH v9 05/11] perf dso: Synchronize dso_cache access and make dso->loaded atomic Ian Rogers
2026-11-24 3:28 ` [PATCH v9 06/11] perf symbol: Add symbols__shrink() and symbols__try_delete_unannotated() Ian Rogers
2026-11-24 3:28 ` [PATCH v9 07/11] perf dso: Add dso__shrink() and dso__try_evict() Ian Rogers
2026-11-24 3:28 ` [PATCH v9 08/11] perf session: Add dsos, machine, and perf_session shrinking Ian Rogers
2026-11-24 3:29 ` [PATCH v9 09/11] perf test symbols: Add symbol and DSO shrinking tests Ian Rogers
2026-11-24 3:29 ` [PATCH v9 10/11] perf session: Add dso__session() and session-scoped allocation wrappers Ian Rogers
2026-11-24 3:29 ` [PATCH v9 11/11] perf test symbols: Add session allocation wrapper and lock contention tests Ian Rogers
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261124032902.3859564-3-irogers@google.com \
--to=irogers@google.com \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=haghdoost@uber.com \
--cc=james.clark@linaro.org \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®