From: "Lorenzo Stoakes (ARM)" <ljs@kernel.org>
To: "Linus Torvalds" <torvalds@linux-foundation.org>,
"Nathan Chancellor" <nathan@kernel.org>,
"Nicolas Schier" <nsc@kernel.org>,
"Nick Desaulniers" <ndesaulniers@google.com>,
"Bill Wendling" <morbo@google.com>,
"Justin Stitt" <justinstitt@google.com>,
"Masahiro Yamada" <masahiroy@kernel.org>,
"Alexey Gladkov" <legion@kernel.org>,
"Thomas Gleixner" <tglx@kernel.org>,
"Ingo Molnar" <mingo@redhat.com>,
"Borislav Petkov" <bp@alien8.de>,
"Dave Hansen" <dave.hansen@linux.intel.com>,
x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>,
"Paul Walmsley" <pjw@kernel.org>,
"Palmer Dabbelt" <palmer@dabbelt.com>,
"Albert Ou" <aou@eecs.berkeley.edu>,
"Alexandre Ghiti" <alex@ghiti.fr>,
"Arnd Bergmann" <arnd@arndb.de>,
"Catalin Marinas" <catalin.marinas@arm.com>,
"Will Deacon" <will@kernel.org>,
"Mark Rutland" <mark.rutland@arm.com>,
"Ard Biesheuvel" <ardb@kernel.org>,
"Ilias Apalodimas" <ilias.apalodimas@linaro.org>,
"Josh Poimboeuf" <jpoimboe@kernel.org>,
"Peter Zijlstra" <peterz@infradead.org>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
"Daniel Almeida" <daniel.almeida@collabora.com>,
"Tamir Duberstein" <tamird@kernel.org>,
"Alexandre Courbot" <acourbot@nvidia.com>,
"Onur Özkan" <work@onurozkan.dev>,
"Jonathan Corbet" <corbet@lwn.net>,
"Randy Dunlap" <rdunlap@infradead.org>
Cc: linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org,
llvm@lists.linux.dev, linux-riscv@lists.infradead.org,
linux-arch@vger.kernel.org,
linux-arm-kernel@lists.infradead.org, linux-efi@vger.kernel.org,
rust-for-linux@vger.kernel.org, linux-doc@vger.kernel.org,
Jens Axboe <axboe@kernel.dk>,
"Lorenzo Stoakes (ARM)" <ljs@kernel.org>
Subject: [PATCH 03/23] kallsyms: index symbols by token to speed up table compression
Date: Tue, 08 Sep 2026 21:55:03 +0100 [thread overview]
Message-ID: <20260908-build-speedup-v1-3-5dc1ac01672d@kernel.org> (raw)
In-Reply-To: <20260908-build-speedup-v1-0-5dc1ac01672d@kernel.org>
The kallsyms program compresses symbols by figuring out the most commonly
used substrings in all of the input symbols then uses special character
codes to represent them.
For instance, 0xf7 might end up representing "write_", then every single
symbol that contains "write_" can use 0xf7 as a shorthand and save 5 bytes
each time.
'Special' character codes are any byte value that is not used in any
symbol, either due to being an invalid character, or not being present in
any symbol (e.g. if no symbol contains 'z', then 'z' can be used as special
character).
It does this by first figuring out which special characters are available
in insert_real_symbols_in_table(), then iterating through every available
special character, counting how many times each pair of adjacent characters
appear in symbols in build_initial_token_table().
These adjacent pairs are known as 'tokens'.
Token counts are initially obtained by build_initial_token_table(), then
optimize_result() calls find_best_token() to determine the token that
appeared the most number of times and assigns it the next special
character.
Finally, optimize_result() calls compress_symbols() to replace every token
in every symbol with its special character, which updates token_profit[] as
it does so.
This process is repeated for each remaining available special character,
with tokens now perhaps containing previously assigned special
characters (e.g. if 'wr' was assigned 0x80, then the token representing
'wri' would be '\x80i').
This compresses that token by 50% in each symbol it appears in (two bytes
are now represented by one) and thus by repeatedly doing this kallsyms
obtains good symbol compression.
However, compress_symbols() is seriously inefficient - it iterates through
EVERY symbol for EVERY special character assignment, i.e. ~256 *
nr_symbols.
Modern x86-64 kernels, for instance, have ~158,000 symbols, so millions of
iterations are performed, most of which are entirely unnecessary (tokens
don't appear in most symbols).
In practice kallsyms spends half its runtime doing this, two or three times
per vmlinux link step.
Fix this by tracking which symbols each token appears in token_syms[], and
only compress symbols which actually need to be updated.
Each time a token is compressed that token can no longer appear in any
symbol, so that token_syms[] entry can be freed.
However new token_syms[] entries must be created for each new token
containing the assigned special character, but this is bounded by the
number of replacements in the symbol which is very small.
In testing on an x86-64 platform using clang, each kallsyms invocation
dropped from 0.59s to 0.33s with CONFIG_KALLSYMS_ALL set and from 0.38s to
0.22s without it set.
The data was carefully checked and verified to be byte-for-byte identical
for six symbol sets (two vmlinux passes, vmlinux.o, three userspace
binaries) with all option combinations.
As part of this change, additionally refactor the code to be a little
easier to follow.
kallsyms runs two or three times on the serial tail of every build that
links vmlinux, no-op builds do not link and are unchanged.
Whole build, 128-thread Threadripper 9980X, best of N runs:
before after delta
-------------------------------
x86 defconfig, touch mm/vma.c, gcc 11.4s 10.8s -0.55s (-5%)
x86 defconfig, touch mm/vma.c, clang 11.4s 10.7s -0.66s (-6%)
x86 defconfig, clean, gcc 30.3s 29.5s -0.80s (-3%)
x86 defconfig, clean, clang 30.3s 29.7s -0.62s (-2%)
x86 allmodconfig, touch mm/vma.c, gcc 46.2s 45.3s -0.91s (-2%)
x86 allmodconfig, touch mm/vma.c, clang 44.2s 42.9s -1.3s (-3%)
Assisted-by: LLM
Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
---
scripts/kallsyms.c | 138 +++++++++++++++++++++++++++++++++++++++++++++++------
1 file changed, 124 insertions(+), 14 deletions(-)
diff --git a/scripts/kallsyms.c b/scripts/kallsyms.c
index 494852ade6d8..350d118c3b9e 100644
--- a/scripts/kallsyms.c
+++ b/scripts/kallsyms.c
@@ -58,12 +58,47 @@ static unsigned int table_size, table_cnt;
static int all_symbols;
static int pc_relative;
+/* A dynamic array of symbols, encoded by symbol index. */
+struct sym_arr {
+ unsigned int *sym_indexes;
+ unsigned int cnt, cap;
+};
+
static int token_profit[0x10000];
+static struct sym_arr token_syms[0x10000];
/* the table that holds the result of the compression */
static unsigned char best_table[256][2];
static unsigned char best_table_len[256];
+static unsigned int sym_arr_last(const struct sym_arr *arr)
+{
+ return arr->cnt ? arr->sym_indexes[arr->cnt - 1] : UINT_MAX;
+}
+
+static void sym_arr_maybe_expand(struct sym_arr *arr)
+{
+ if (arr->cap > arr->cnt)
+ return;
+
+ arr->cap = arr->cap ? arr->cap * 2 : 16;
+ arr->sym_indexes = xrealloc(arr->sym_indexes,
+ arr->cap * sizeof(*arr->sym_indexes));
+}
+
+static void sym_arr_add(struct sym_arr *arr, unsigned int sym_idx)
+{
+ sym_arr_maybe_expand(arr);
+ arr->sym_indexes[arr->cnt++] = sym_idx;
+}
+
+static void sym_arr_free(struct sym_arr *arr)
+{
+ free(arr->sym_indexes);
+ arr->sym_indexes = NULL;
+ arr->cnt = 0;
+ arr->cap = 0;
+}
static void usage(void)
{
@@ -458,6 +493,15 @@ static void write_src(void)
printf("\n");
}
+static unsigned int token_index(unsigned char first, unsigned char second)
+{
+ return first + (second << 8);
+}
+
+static unsigned int sym_token_index(const unsigned char *symbol, int first_idx)
+{
+ return token_index(symbol[first_idx], symbol[first_idx + 1]);
+}
/* table lookup compression functions */
@@ -467,7 +511,7 @@ static void learn_symbol(const unsigned char *symbol, int len)
int i;
for (i = 0; i < len - 1; i++)
- token_profit[ symbol[i] + (symbol[i + 1] << 8) ]++;
+ token_profit[sym_token_index(symbol, i)]++;
}
/* decrease the count for all the possible tokens in a symbol */
@@ -476,16 +520,76 @@ static void forget_symbol(const unsigned char *symbol, int len)
int i;
for (i = 0; i < len - 1; i++)
- token_profit[ symbol[i] + (symbol[i + 1] << 8) ]--;
+ token_profit[sym_token_index(symbol, i)]--;
+}
+
+static void token_add_symbol(unsigned int token_idx, unsigned int sym_idx)
+{
+ struct sym_arr *arr = &token_syms[token_idx];
+
+ /* Symbol indexes kept in sorted order, check for duplicate. */
+ if (sym_arr_last(arr) == sym_idx)
+ return;
+
+ sym_arr_add(arr, sym_idx);
+}
+
+static void symbol_index_all_tokens(const unsigned char *symbol, int len,
+ unsigned int sym_idx)
+{
+ int i;
+
+ for (i = 0; i < len - 1; i++) {
+ const unsigned int token_idx = sym_token_index(symbol, i);
+
+ token_add_symbol(token_idx, sym_idx);
+ }
+}
+
+/*
+ * The symbol just got compressed. The only parts of the symbol that changed
+ * meaningfully are those containing the newly assigned compressed char, so
+ * index those.
+ */
+static void symbol_index_new_tokens(const unsigned char *symbol, int len,
+ unsigned int sym_idx, int compressed_chr)
+{
+ int i;
+
+ for (i = 0; i < len - 1; i++) {
+ const unsigned int token_idx = sym_token_index(symbol, i);
+
+ if (symbol[i] == compressed_chr ||
+ symbol[i + 1] == compressed_chr)
+ token_add_symbol(token_idx, sym_idx);
+ }
}
-/* do the initial token count */
static void build_initial_token_table(void)
{
unsigned int i;
for (i = 0; i < table_cnt; i++)
learn_symbol(table[i]->sym, table[i]->len);
+
+ /*
+ * The initial occurrence counts tell us exactly how much memory should
+ * be reserved for each token's symbol array.
+ */
+ for (i = 0; i < ARRAY_SIZE(token_syms); i++) {
+ const int nr_syms = token_profit[i];
+
+ if (!nr_syms)
+ continue;
+
+ token_syms[i].cap = nr_syms;
+ token_syms[i].sym_indexes =
+ xmalloc(nr_syms * sizeof(unsigned int));
+ }
+
+ /* For every symbol, index every token -> symbol it is present in. */
+ for (i = 0; i < table_cnt; i++)
+ symbol_index_all_tokens(table[i]->sym, table[i]->len, i);
}
static unsigned char *find_token(unsigned char *str, int len,
@@ -502,27 +606,30 @@ static unsigned char *find_token(unsigned char *str, int len,
/* replace a given token in all the valid symbols. Use the sampled symbols
* to update the counts */
-static void compress_symbols(const unsigned char *str, int idx)
+static void compress_symbols(const unsigned char *str, int compressed_chr)
{
- unsigned int i, len, size;
+ const unsigned int token_idx = sym_token_index(str, 0);
+ struct sym_arr *arr = &token_syms[token_idx];
+ unsigned int sym_idx, j, len, size;
unsigned char *p1, *p2;
- for (i = 0; i < table_cnt; i++) {
+ /* Iterate through all symbols this token is found in and compress. */
+ for (j = 0; j < arr->cnt; j++) {
+ sym_idx = arr->sym_indexes[j];
- len = table[i]->len;
- p1 = table[i]->sym;
+ len = table[sym_idx]->len;
+ p1 = table[sym_idx]->sym;
- /* find the token on the symbol */
p2 = find_token(p1, len, str);
if (!p2) continue;
/* decrease the counts for this symbol's tokens */
- forget_symbol(table[i]->sym, len);
+ forget_symbol(table[sym_idx]->sym, len);
size = len;
do {
- *p2 = idx;
+ *p2 = compressed_chr;
p2++;
size -= (p2 - p1);
memmove(p2, p2 + 1, size);
@@ -536,11 +643,14 @@ static void compress_symbols(const unsigned char *str, int idx)
} while (p2);
- table[i]->len = len;
+ table[sym_idx]->len = len;
- /* increase the counts for this symbol's new tokens */
- learn_symbol(table[i]->sym, len);
+ learn_symbol(table[sym_idx]->sym, len);
+ symbol_index_new_tokens(table[sym_idx]->sym, len, sym_idx,
+ compressed_chr);
}
+
+ sym_arr_free(arr); /* No symbol contains this token any more. */
}
/* search the token with the maximum profit */
--
2.55.0
next prev parent reply other threads:[~2026-09-08 20:56 UTC|newest]
Thread overview: 84+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-08 20:55 [PATCH 00/23] kbuild: significantly speed up kernel builds Lorenzo Stoakes (ARM)
2026-09-08 20:55 ` [PATCH 01/23] scripts/mksysmap: drop the MODULE_INFO() symbols from kallsyms Lorenzo Stoakes (ARM)
2026-09-09 19:47 ` Nicolas Schier
2026-09-10 11:00 ` Lorenzo Stoakes (ARM)
2026-09-10 4:19 ` Nathan Chancellor
2026-09-10 11:03 ` Lorenzo Stoakes (ARM)
2026-09-10 19:00 ` Nicolas Schier
2026-09-08 20:55 ` [PATCH 02/23] scripts/mksysmap: fix escape of '$' in the __pi_ pattern Lorenzo Stoakes (ARM)
2026-09-09 19:47 ` Nicolas Schier
2026-09-10 11:04 ` Lorenzo Stoakes (ARM)
2026-09-10 4:19 ` Nathan Chancellor
2026-09-10 11:21 ` Lorenzo Stoakes (ARM)
2026-09-10 19:00 ` Nicolas Schier
2026-09-08 20:55 ` Lorenzo Stoakes (ARM) [this message]
2026-09-08 20:55 ` [PATCH 04/23] kallsyms: output binary data to speed output and kallsyms assembly Lorenzo Stoakes (ARM)
2026-09-09 14:35 ` Linus Torvalds
2026-09-10 9:29 ` David Laight
2026-09-11 11:07 ` Lorenzo Stoakes (ARM)
2026-09-08 20:55 ` [PATCH 05/23] kbuild: do not sort nm output where the order is irrelevant Lorenzo Stoakes (ARM)
2026-09-10 4:19 ` Nathan Chancellor
2026-09-08 20:55 ` [PATCH 06/23] kbuild: only emit vmlinux relocations when required Lorenzo Stoakes (ARM)
2026-09-10 4:19 ` Nathan Chancellor
2026-09-08 20:55 ` [PATCH 07/23] elf-parse: add section flags, symbol binding and a read-only mapping Lorenzo Stoakes (ARM)
2026-09-08 20:55 ` [PATCH 08/23] kallsyms: reimplement mksysmap in C Lorenzo Stoakes (ARM)
2026-09-11 18:52 ` Markus Elfring
2026-09-11 19:15 ` Markus Elfring
2026-09-11 19:42 ` Markus Elfring
2026-09-08 20:55 ` [PATCH 09/23] kbuild: do not allocate .modinfo in vmlinux Lorenzo Stoakes (ARM)
2026-09-10 4:19 ` Nathan Chancellor
2026-09-10 10:59 ` Lorenzo Stoakes (ARM)
2026-09-08 20:55 ` [PATCH 10/23] kbuild: cache list, composite object state per object Lorenzo Stoakes (ARM)
2026-09-08 20:55 ` [PATCH 11/23] kbuild: implement and use depcheck to check dependency timestamps Lorenzo Stoakes (ARM)
2026-09-09 15:26 ` Linus Torvalds
2026-09-08 20:55 ` [PATCH 12/23] kbuild: avoid re-running compiler and linker probes Lorenzo Stoakes (ARM)
2026-09-10 4:19 ` Nathan Chancellor
2026-09-10 15:42 ` Nicolas Schier
2026-09-11 10:30 ` Lorenzo Stoakes (ARM)
2026-09-11 18:10 ` Nicolas Schier
2026-09-11 18:25 ` Lorenzo Stoakes (ARM)
2026-09-11 17:33 ` David Laight
2026-09-11 18:17 ` Nicolas Schier
2026-09-11 18:24 ` Lorenzo Stoakes (ARM)
2026-09-11 19:34 ` Nicolas Schier
2026-09-11 21:01 ` David Laight
2026-09-11 10:26 ` Lorenzo Stoakes (ARM)
2026-09-08 20:55 ` [PATCH 13/23] modpost: hash module source per-file, not per-byte Lorenzo Stoakes (ARM)
2026-09-10 12:52 ` Petr Pavlu
2026-09-11 10:41 ` Lorenzo Stoakes (ARM)
2026-09-11 11:57 ` Petr Pavlu
2026-09-11 12:21 ` Lorenzo Stoakes (ARM)
2026-09-08 20:55 ` [PATCH 14/23] modpost: cache section relocation mismatch state Lorenzo Stoakes (ARM)
2026-09-08 20:55 ` [PATCH 15/23] modpost: emit module descriptors as assembly Lorenzo Stoakes (ARM)
2026-09-09 14:59 ` Linus Torvalds
2026-09-08 20:55 ` [PATCH 16/23] kbuild: batch module finalisation Lorenzo Stoakes (ARM)
2026-09-10 15:48 ` Nicolas Schier
2026-09-11 10:23 ` Lorenzo Stoakes (ARM)
2026-09-08 20:55 ` [PATCH 17/23] modpost: perform srcversion hashing in parallel Lorenzo Stoakes (ARM)
2026-09-10 10:32 ` David Laight
2026-09-11 10:49 ` Lorenzo Stoakes (ARM)
2026-09-08 20:55 ` [PATCH 18/23] objtool: cache relocations and function dead end state, do less work Lorenzo Stoakes (ARM)
2026-09-08 20:55 ` [PATCH 19/23] objtool: decode instructions and resolve branch targets in parallel Lorenzo Stoakes (ARM)
2026-09-08 20:55 ` [PATCH 20/23] kbuild: rust: parallelise rustc front end Lorenzo Stoakes (ARM)
2026-09-08 21:13 ` Miguel Ojeda
2026-09-09 14:22 ` Lorenzo Stoakes (ARM)
2026-09-09 10:22 ` Björn Baron
2026-09-09 12:59 ` Miguel Ojeda
2026-09-09 14:26 ` Lorenzo Stoakes (ARM)
2026-09-10 12:25 ` Nicolas Schier (FRITZ!)
2026-09-11 19:00 ` Nicolas Schier
2026-09-08 20:55 ` [PATCH 21/23] rust: make exports.o depend on the headers generated for it Lorenzo Stoakes (ARM)
2026-09-08 20:55 ` [PATCH 22/23] kbuild: build rust crates in parallel with the rest of the build Lorenzo Stoakes (ARM)
2026-09-08 20:55 ` [PATCH 23/23] kbuild: use pigz for gzip compression if available Lorenzo Stoakes (ARM)
2026-09-10 4:19 ` Nathan Chancellor
2026-09-11 11:03 ` Lorenzo Stoakes (ARM)
2026-09-08 21:06 ` [PATCH 00/23] kbuild: significantly speed up kernel builds Nick Desaulniers
2026-09-09 14:17 ` Lorenzo Stoakes (ARM)
2026-09-09 22:09 ` Nick Desaulniers
2026-09-11 11:25 ` Lorenzo Stoakes (ARM)
2026-09-09 15:37 ` Linus Torvalds
2026-09-09 16:30 ` Lorenzo Stoakes (ARM)
2026-09-09 21:58 ` Florian Fainelli
2026-09-11 11:28 ` Lorenzo Stoakes (ARM)
2026-09-10 4:19 ` Nathan Chancellor
2026-09-11 11:13 ` Lorenzo Stoakes (ARM)
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=20260908-build-speedup-v1-3-5dc1ac01672d@kernel.org \
--to=ljs@kernel.org \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=alex@ghiti.fr \
--cc=aliceryhl@google.com \
--cc=aou@eecs.berkeley.edu \
--cc=ardb@kernel.org \
--cc=arnd@arndb.de \
--cc=axboe@kernel.dk \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=bp@alien8.de \
--cc=catalin.marinas@arm.com \
--cc=corbet@lwn.net \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=dave.hansen@linux.intel.com \
--cc=gary@garyguo.net \
--cc=hpa@zytor.com \
--cc=ilias.apalodimas@linaro.org \
--cc=jpoimboe@kernel.org \
--cc=justinstitt@google.com \
--cc=legion@kernel.org \
--cc=linux-arch@vger.kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-efi@vger.kernel.org \
--cc=linux-kbuild@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=llvm@lists.linux.dev \
--cc=lossin@kernel.org \
--cc=mark.rutland@arm.com \
--cc=masahiroy@kernel.org \
--cc=mingo@redhat.com \
--cc=morbo@google.com \
--cc=nathan@kernel.org \
--cc=ndesaulniers@google.com \
--cc=nsc@kernel.org \
--cc=ojeda@kernel.org \
--cc=palmer@dabbelt.com \
--cc=peterz@infradead.org \
--cc=pjw@kernel.org \
--cc=rdunlap@infradead.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tamird@kernel.org \
--cc=tglx@kernel.org \
--cc=tmgross@umich.edu \
--cc=torvalds@linux-foundation.org \
--cc=will@kernel.org \
--cc=work@onurozkan.dev \
--cc=x86@kernel.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®