linux-doc.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
To: Jonathan Corbet <corbet@lwn.net>
Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org,
	Akira Yokosawa <akiyks@gmail.com>
Subject: Re: [PATCH v2 2/7] docs: kdoc: micro-optimize KernRe
Date: Fri, 4 Jul 2025 08:01:40 +0200	[thread overview]
Message-ID: <20250704080140.136b0b38@foz.lan> (raw)
In-Reply-To: <87ms9kkfb2.fsf@trenco.lwn.net>

Em Thu, 03 Jul 2025 17:47:13 -0600
Jonathan Corbet <corbet@lwn.net> escreveu:

> Mauro Carvalho Chehab <mchehab+huawei@kernel.org> writes:
> 
> > Em Thu,  3 Jul 2025 12:43:58 -0600
> > Jonathan Corbet <corbet@lwn.net> escreveu:
> >  
> >> Rework _add_regex() to avoid doing the lookup twice for the (hopefully
> >> common) cache-hit case.
> >> 
> >> Signed-off-by: Jonathan Corbet <corbet@lwn.net>
> >> ---
> >>  scripts/lib/kdoc/kdoc_re.py | 7 ++-----
> >>  1 file changed, 2 insertions(+), 5 deletions(-)
> >> 
> >> diff --git a/scripts/lib/kdoc/kdoc_re.py b/scripts/lib/kdoc/kdoc_re.py
> >> index e81695b273bf..612223e1e723 100644
> >> --- a/scripts/lib/kdoc/kdoc_re.py
> >> +++ b/scripts/lib/kdoc/kdoc_re.py
> >> @@ -29,12 +29,9 @@ class KernRe:
> >>          """
> >>          Adds a new regex or re-use it from the cache.
> >>          """
> >> -
> >> -        if string in re_cache:
> >> -            self.regex = re_cache[string]
> >> -        else:
> >> +        self.regex = re_cache.get(string, None)  
> >
> > With get, None is default...
> >  
> >> +        if not self.regex:
> >>              self.regex = re.compile(string, flags=flags)  
> >
> > ... yet, as you're using get, better to code it as:
> >
> > 	self.regex = re_cache.get(string, re.compile(string, flags=flags))  
> 
> ...but that will recompile the regex each time, defeating the purpose of
> the cache, no?

No. It should do exactly like the previous code:

- if re_cache[string] exists, it returns it. 
- Otherwise, it returns re.compile(string, flags=flags).

https://www.w3schools.com/python/ref_dictionary_get.asp


Thanks,
Mauro

  reply	other threads:[~2025-07-04  6:01 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-03 18:43 [PATCH v2 0/7] Further kernel-doc tweakery Jonathan Corbet
2025-07-03 18:43 ` [PATCH v2 1/7] docs: kdoc: don't reinvent string.strip() Jonathan Corbet
2025-07-03 18:43 ` [PATCH v2 2/7] docs: kdoc: micro-optimize KernRe Jonathan Corbet
2025-07-03 22:31   ` Mauro Carvalho Chehab
2025-07-03 22:32     ` Mauro Carvalho Chehab
2025-07-03 23:47     ` Jonathan Corbet
2025-07-04  6:01       ` Mauro Carvalho Chehab [this message]
2025-07-04 14:59         ` Jonathan Corbet
2025-07-08  8:13           ` Mauro Carvalho Chehab
2025-07-03 18:43 ` [PATCH v2 3/7] docs: kdoc: remove the brcount floor in process_proto_type() Jonathan Corbet
2025-07-03 18:44 ` [PATCH v2 4/7] docs: kdoc: rework type prototype parsing Jonathan Corbet
2025-07-03 18:44 ` [PATCH v2 5/7] docs: kdoc: some tweaks to process_proto_function() Jonathan Corbet
2025-07-03 18:44 ` [PATCH v2 6/7] docs: kdoc: Remove a Python 2 comment Jonathan Corbet
2025-07-03 18:44 ` [PATCH v2 7/7] docs: kdoc: pretty up dump_enum() Jonathan Corbet
2025-07-04  0:45 ` [PATCH v2 0/7] Further kernel-doc tweakery Akira Yokosawa

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=20250704080140.136b0b38@foz.lan \
    --to=mchehab+huawei@kernel.org \
    --cc=akiyks@gmail.com \
    --cc=corbet@lwn.net \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.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;
as well as URLs for NNTP newsgroup(s).