From: David Laight <david.laight.linux@gmail.com>
To: Andrew Morton <akpm@linux-foundation.org>,
Petr Mladek <pmladek@suse.com>, Kees Cook <kees@kernel.org>,
David Laight <david.laight.linux@gmail.com>,
linux-kernel@vger.kernel.org, linux-kbuild@vger.kernel.org,
bpf@vger.kernel.org, Jim Cromie <jim.cromie@gmail.com>,
Lorenzo Stoakes <ljs@kernel.org>
Cc: Luis Chamberlain <mcgrof@kernel.org>,
Andrey Grodzovsky <andrey.grodzovsky@crowdstrike.com>,
Steven Rostedt <rostedt@goodmis.org>
Subject: [PATCH v2 next 12/15] kallsyms: scripts/kallsyms: Optimise compress_symbols()
Date: Sun, 18 Oct 2026 16:52:03 +0100 [thread overview]
Message-ID: <20261018155206.132411-13-david.laight.linux@gmail.com> (raw)
In-Reply-To: <20261018155206.132411-1-david.laight.linux@gmail.com>
When (say) axyb is replaced with azb instead of decrementing the
token_profit[] value for every pair of token in the old string before
the substitution and then incrementing it afterwards just decrement
the counts for ax and yb and increment the ones for az and zb.
Set the count for xy (the number of actual substitions) to zero
afterwards.
This pretty much halves the time taken to do the actual compression
which is by far largest part of the entire process.
Output file unchanged.
Signed-off-by: David Laight <david.laight.linux@gmail.com>
---
scripts/kallsyms.c | 74 ++++++++++++++++++----------------------------
1 file changed, 29 insertions(+), 45 deletions(-)
diff --git a/scripts/kallsyms.c b/scripts/kallsyms.c
index 9e1bc0fdaa9f..ee06a9c7435f 100644
--- a/scripts/kallsyms.c
+++ b/scripts/kallsyms.c
@@ -479,13 +479,11 @@ static void learn_symbol(const unsigned char *sym, int len)
token_profit[PAIR(sym[i], sym[i + 1])]++;
}
-/* decrease the count for all the possible tokens in a symbol */
-static void forget_symbol(const unsigned char *sym, int len)
+static void adjust_profit(unsigned int old_left, unsigned int new_left,
+ unsigned int old_right, unsigned int new_right)
{
- int i;
-
- for (i = 0; i < len - 1; i++)
- token_profit[PAIR(sym[i], sym[i + 1])]--;
+ token_profit[PAIR(old_left, old_right)]--;
+ token_profit[PAIR(new_left, new_right)]++;
}
/* do the initial token count */
@@ -497,13 +495,12 @@ static void build_initial_token_table(void)
learn_symbol(symbol[i]->sym, symbol[i]->len);
}
-static unsigned char *find_token(unsigned char *str, int len,
+static unsigned char *find_token(unsigned char *str, unsigned char *end,
unsigned int want)
{
unsigned int token = PAIR(0, *str++);
- int i;
- for (i = 0; i < len - 1; i++) {
+ while (str < end) {
token = PAIR(RIGHT(token), *str++);
if (token == want)
return str - 2;
@@ -512,46 +509,33 @@ 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(unsigned int token_pair, int idx)
+ * to update the counts.
+ * Marked noinline to help register allocation. compress_symbols() is only called
+ * about 200 times whereas find_token() can be called well over a million times. */
+static __attribute__((noinline)) void compress_symbols(unsigned int token_pair, int idx)
{
- unsigned int i, len, size;
- unsigned char *p1, *p2;
+ unsigned int i, len;
+ unsigned char *sym, *p, *end;
for (i = 0; i < symbol_cnt; i++) {
-
- len = symbol[i]->len;
- p1 = symbol[i]->sym;
-
- /* find the token on the symbol */
- p2 = find_token(p1, len, token_pair);
- if (!p2) continue;
-
- /* decrease the counts for this symbol's tokens */
- forget_symbol(symbol[i]->sym, len);
-
- size = len;
-
- do {
- *p2 = idx;
- p2++;
- size -= (p2 - p1);
- memmove(p2, p2 + 1, size);
- p1 = p2;
- len--;
-
- if (size < 2) break;
-
- /* find the token on the symbol */
- p2 = find_token(p1, size, token_pair);
-
- } while (p2);
-
- symbol[i]->len = len;
-
- /* increase the counts for this symbol's new tokens */
- learn_symbol(symbol[i]->sym, len);
+ sym = symbol[i]->sym;
+ end = sym + symbol[i]->len;
+
+ for (p = sym; (p = find_token(p, end, token_pair)); p++) {
+ len = end - p - 2;
+ end--;
+ *p = idx;
+ if (p != sym)
+ adjust_profit(p[-1], p[-1], LEFT(token_pair), idx);
+ if (!len)
+ break;
+ adjust_profit(RIGHT(token_pair), idx, p[2], p[2]);
+ memmove(p + 1, p + 2, len);
+ }
+ symbol[i]->len = end - sym;
}
+
+ token_profit[token_pair] = 0;
}
/* search the token with the maximum profit */
--
2.39.5
next prev parent reply other threads:[~2026-10-18 15:53 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-18 15:51 [PATCH v2 next 00/15] kallsyms: optimise symbol search by name David Laight
2026-10-18 15:51 ` [PATCH v2 next 01/15] kallsyms: scripts/kallsyms.c: Avoid overrunning on-stack buffer David Laight
2026-10-18 15:51 ` [PATCH v2 next 02/15] kallsyms: Fix selftest startup delay David Laight
2026-10-18 15:51 ` [PATCH v2 next 03/15] kallsyms: Improve selftest test_perf_kallsyms_lookup_name() output David Laight
2026-10-19 16:07 ` kernel test robot
2026-10-19 17:08 ` David Laight
2026-10-18 15:51 ` [PATCH v2 next 04/15] kallsyms: scripts/kallsyms: Check for output file write errors David Laight
2026-10-18 15:51 ` [PATCH v2 next 05/15] kallsyms: Match compressed tokens on the fly during binary search David Laight
2026-10-18 15:51 ` [PATCH v2 next 06/15] kallsyms: Optimise symbol name search David Laight
2026-10-18 15:51 ` [PATCH v2 next 07/15] kallsyms: Optimise detection of duplicate symbol names David Laight
2026-10-18 15:51 ` [PATCH v2 next 08/15] kallsyms: Support changing the density of the kallsyms_markers[] array David Laight
2026-10-18 15:52 ` [PATCH v2 next 09/15] kallsyms: Add a kernel parameter for the marker shift David Laight
2026-10-18 15:52 ` [PATCH v2 next 10/15] kallsyms: scripts/kallsyms: Rename some variables for clarity David Laight
2026-10-18 15:52 ` [PATCH v2 next 11/15] kallsyms: scripts/kallsyms: Keep full expansion of tokens David Laight
2026-10-18 15:52 ` David Laight [this message]
2026-10-18 15:52 ` [PATCH v2 next 13/15] kallsyms: scripts/kallsyms: Reuse tokens that have no references David Laight
2026-10-18 15:52 ` [PATCH 14/15] kallsyms: scripts/kallsyms: Reduce the output file size David Laight
2026-10-18 15:52 ` [PATCH 15/15] kallsyms: scripts/kallsyms: Improve compression by about 2% David Laight
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=20261018155206.132411-13-david.laight.linux@gmail.com \
--to=david.laight.linux@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=andrey.grodzovsky@crowdstrike.com \
--cc=bpf@vger.kernel.org \
--cc=jim.cromie@gmail.com \
--cc=kees@kernel.org \
--cc=linux-kbuild@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=ljs@kernel.org \
--cc=mcgrof@kernel.org \
--cc=pmladek@suse.com \
--cc=rostedt@goodmis.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®