All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] scripts: avoid redundant re-reading makefiles.
@ 2023-01-04 16:04 Alexander Pantyukhin
  2023-01-05 18:44 ` Miguel Ojeda
  0 siblings, 1 reply; 4+ messages in thread
From: Alexander Pantyukhin @ 2023-01-04 16:04 UTC (permalink / raw)
  To: ojeda, rust-for-linux, akpm; +Cc: alexpantyukhin

From: alexpantyukhin <apantykhin@gmail.com>

The main goal of the patch is avoid redundant re-reading makefiles 
if these were read before. This is performance improvement.

Additional code improvements are using 'with' when read file and check
if file exists.

Signed-off-by: Alexander Pantyukhin <apantykhin@gmail.com>
---
 scripts/generate_rust_analyzer.py | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/scripts/generate_rust_analyzer.py b/scripts/generate_rust_analyzer.py
index ecc7ea9a4dcf..e949f32b8123 100755
--- a/scripts/generate_rust_analyzer.py
+++ b/scripts/generate_rust_analyzer.py
@@ -98,13 +98,25 @@ def generate_crates(srctree, objtree, sysroot_src):
     # Then, the rest outside of `rust/`.
     #
     # We explicitly mention the top-level folders we want to cover.
+    makefile_path_content = {}
     for folder in ("samples", "drivers"):
         for path in (srctree / folder).rglob("*.rs"):
             logging.info("Checking %s", path)
             name = path.name.replace(".rs", "")
 
             # Skip those that are not crate roots.
-            if f"{name}.o" not in open(path.parent / "Makefile").read():
+            makefile_path = path.parent / "Makefile"
+
+            if makefile_path not in makefile_path_content:
+                if makefile_path.is_file():
+                    with open(makefile_path) as makefile:
+                        makefile_path_content[makefile_path] = makefile.read()
+
+                else:
+                    # If file is missing we consider it as empty-content file.
+                    makefile_path_content[makefile_path] = ""
+
+            if f"{name}.o" not in makefile_path_content[makefile_path]:
                 continue
 
             logging.info("Adding %s", name)
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] scripts: avoid redundant re-reading makefiles.
  2023-01-04 16:04 [PATCH] scripts: avoid redundant re-reading makefiles Alexander Pantyukhin
@ 2023-01-05 18:44 ` Miguel Ojeda
  2023-01-05 19:51   ` Alexander Pantyukhin
  0 siblings, 1 reply; 4+ messages in thread
From: Miguel Ojeda @ 2023-01-05 18:44 UTC (permalink / raw)
  To: Alexander Pantyukhin; +Cc: ojeda, rust-for-linux, akpm, Asahi Lina

Hi Alex,

The commit message is looking better, thank you. Please see comments
inline below.

On Wed, Jan 4, 2023 at 5:04 PM Alexander Pantyukhin
<apantykhin@gmail.com> wrote:
>
> The main goal of the patch is avoid redundant re-reading makefiles
> if these were read before. This is performance improvement.

Like it was mentioned in v1, could you please give a rough estimate
about what is the improvement from old to new? (in particular, without
the other changes). That way, others can get an idea whether the code
complexity is worth it or not.

> Additional code improvements are using 'with' when read file and check
> if file exists.

Note that others submitted an improvement for the missing file case as
we discussed in v1, so it would be better if they submitted their own
patches for those. I am Cc'ing them so that they have a chance to be
aware of this patch and potentially submit theirs.

In any case, their approach seems more idiomatic for Python [1], so I
think we should do it that way.

Finally, the title of this commit should probably start with the
"scripts: generate_rust_analyzer: " prefix, otherwise it is a bit too
broad.

Thanks!

Cheers,
Miguel

[1] https://docs.python.org/3.11/glossary.html#term-EAFP

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] scripts: avoid redundant re-reading makefiles.
  2023-01-05 18:44 ` Miguel Ojeda
@ 2023-01-05 19:51   ` Alexander Pantyukhin
  2023-01-05 23:01     ` Miguel Ojeda
  0 siblings, 1 reply; 4+ messages in thread
From: Alexander Pantyukhin @ 2023-01-05 19:51 UTC (permalink / raw)
  To: Miguel Ojeda; +Cc: ojeda, rust-for-linux, akpm, Asahi Lina

Hi Miguel.
Please see my comments below.

чт, 5 янв. 2023 г. в 22:44, Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>:
>
> Hi Alex,
>
> The commit message is looking better, thank you. Please see comments
> inline below.
>
> On Wed, Jan 4, 2023 at 5:04 PM Alexander Pantyukhin
> <apantykhin@gmail.com> wrote:
> >
> > The main goal of the patch is avoid redundant re-reading makefiles
> > if these were read before. This is performance improvement.
>
> Like it was mentioned in v1, could you please give a rough estimate
> about what is the improvement from old to new? (in particular, without
> the other changes). That way, others can get an idea whether the code
> complexity is worth it or not.

I checked files now. It seems only one place in /samples  has 2 files
with 1 makefile in the parent folder. So it looks like the patch is
untimely for now.

>
> > Additional code improvements are using 'with' when read file and check
> > if file exists.
>
> Note that others submitted an improvement for the missing file case as
> we discussed in v1, so it would be better if they submitted their own
> patches for those. I am Cc'ing them so that they have a chance to be
> aware of this patch and potentially submit theirs.
>
> In any case, their approach seems more idiomatic for Python [1], so I
> think we should do it that way.
>
> Finally, the title of this commit should probably start with the
> "scripts: generate_rust_analyzer: " prefix, otherwise it is a bit too
> broad.

I see, thanks. Still have a question about using "with". Is it reasonable?

>
> Thanks!
>
> Cheers,
> Miguel
>
> [1] https://docs.python.org/3.11/glossary.html#term-EAFP

Thank you!
Best, Alex.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] scripts: avoid redundant re-reading makefiles.
  2023-01-05 19:51   ` Alexander Pantyukhin
@ 2023-01-05 23:01     ` Miguel Ojeda
  0 siblings, 0 replies; 4+ messages in thread
From: Miguel Ojeda @ 2023-01-05 23:01 UTC (permalink / raw)
  To: Alexander Pantyukhin; +Cc: ojeda, rust-for-linux, akpm, Asahi Lina

On Thu, Jan 5, 2023 at 8:51 PM Alexander Pantyukhin
<apantykhin@gmail.com> wrote:
>
> I checked files now. It seems only one place in /samples  has 2 files
> with 1 makefile in the parent folder. So it looks like the patch is
> untimely for now.

Yeah, though you could do a benchmark with more files to account for
the future :)

> I see, thanks. Still have a question about using "with". Is it reasonable?

It is definitely reasonable and you cannot go wrong with `with`. I am
not sure if matters much in this case (short-lived script, read-only,
not a huge amount of files...), and with CPython the objects are
destroyed immediately anyway I believe.

Cheers,
Miguel

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-01-05 23:02 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-04 16:04 [PATCH] scripts: avoid redundant re-reading makefiles Alexander Pantyukhin
2023-01-05 18:44 ` Miguel Ojeda
2023-01-05 19:51   ` Alexander Pantyukhin
2023-01-05 23:01     ` Miguel Ojeda

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.