Re: [PATCH 2/7] docs: kdoc: micro-optimize KernRe

From: Mauro Carvalho Chehab
Date: Thu Jul 03 2025 - 11:43:19 EST


Em Tue, 1 Jul 2025 14:57:25 -0600
Jonathan Corbet <corbet@xxxxxxx> escreveu:

> Switch KernRe::add_regex() to a try..except block to avoid looking up each
> regex twice.
>
> Signed-off-by: Jonathan Corbet <corbet@xxxxxxx>
> ---
> scripts/lib/kdoc/kdoc_re.py | 6 ++----
> 1 file changed, 2 insertions(+), 4 deletions(-)
>
> diff --git a/scripts/lib/kdoc/kdoc_re.py b/scripts/lib/kdoc/kdoc_re.py
> index e81695b273bf..a467cd2f160b 100644
> --- a/scripts/lib/kdoc/kdoc_re.py
> +++ b/scripts/lib/kdoc/kdoc_re.py
> @@ -29,12 +29,10 @@ class KernRe:
> """
> Adds a new regex or re-use it from the cache.
> """
> -
> - if string in re_cache:
> + try:
> self.regex = re_cache[string]
> - else:
> + except KeyError:
> self.regex = re.compile(string, flags=flags)
> -

Hmm... I opted for this particular way of checking is that I
expect that check inside a hash at dict would be faster than
letting it crash then raise an exception.

Btw, one easy way to check how much it affects performance
(if any) would be to run it in "rogue" mode with:

$ time ./scripts/kernel-doc.py -N .

This will run kernel-doc.py for all files at the entire Kernel
tree, only reporting problems. If you want to do changes like
this that might introduce performance regressions, I suggest
running it once, just to fill disk caches, and then run it
again before/after such changes.

Anyway, I did such measurements before/after your patch.
the difference was not relevant: just one second of difference:

original code:

real 1m20,839s
user 1m19,594s
sys 0m0,998s

after your change:

real 1m21,805s
user 1m20,612s
sys 0m0,929s

I don't mind myself to be one second slower, but this is hardly
a micro-optimization ;-)

-

Disclaimer notice: one second of difference here can be due to
some other background process on this laptop.

Regards,
Mauro