public inbox for debugedit@sourceware.org
 help / color / mirror / Atom feed
From: Mark Wielaard <mark@klomp.org>
To: Nikita Popov <npopov@redhat.com>, debugedit@sourceware.org
Subject: Re: [PATCH] Optimize do_read_32_relocated using binary search
Date: Wed, 15 May 2024 15:38:38 +0200	[thread overview]
Message-ID: <6b3b0c0b29bebb2f6986a8379a44804e015f94d4.camel@klomp.org> (raw)
In-Reply-To: <c38ed85a6be447b77634ebd4c1b1271f6bfc4abd.camel@klomp.org>

[-- Attachment #1: Type: text/plain, Size: 182 bytes --]

Hi,

I finally pushed this (already tested in Fedora rawhide).
I have some followup patches to handle relocations and strx better.
But this patch is correct.

Cheers,

Mark

[-- Attachment #2: 0001-Optimize-do_read_32_relocated-using-binary-search.patch --]
[-- Type: text/x-patch, Size: 1843 bytes --]

From fbad879eb03f7ecd58a9919865c84b422a473b37 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov@redhat.com>
Date: Wed, 17 Apr 2024 15:34:19 +0900
Subject: [PATCH] Optimize do_read_32_relocated using binary search

debugedit is currently very slow when processing DWARF 5 debuginfo
produced by clang. For some kernel modules, debugedit processing
takes hours.

The root cause of the issue is the loop for finding the correct
REL entry in do_read_32_relocated. This is currently a simple
linear scan. For large objects, it may loop for hundreds of
thousands of iterations.

As the relocations are sorted, we can use a binary search instead,
which is what this patch implements. The time to run debugedit on
a large kernel module (nouveau.ko) drops down to 3 seconds with
this change.

Signed-off-by: Nikita Popov <npopov@redhat.com>
---
 tools/debugedit.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)

diff --git a/tools/debugedit.c b/tools/debugedit.c
index f16eecd89a61..ae8e38fa58da 100644
--- a/tools/debugedit.c
+++ b/tools/debugedit.c
@@ -335,12 +335,28 @@ strptr (DSO *dso, size_t sec, size_t offset)
 REL *relptr, *relend;
 int reltype;
 
+static inline REL *
+find_rel_for_ptr (unsigned char *xptr)
+{
+  size_t l = 0, r = relend - relptr;
+  while (l < r)
+    {
+      size_t m = (l + r) / 2;
+      if (relptr[m].ptr < xptr)
+	l = m + 1;
+      else if (relptr[m].ptr > xptr)
+	r = m;
+      else
+	return &relptr[m];
+    }
+  return relend;
+}
+
 #define do_read_32_relocated(xptr) ({			\
   uint32_t dret = do_read_32 (xptr);			\
   if (relptr)						\
     {							\
-      while (relptr < relend && relptr->ptr < (xptr))	\
-	++relptr;					\
+      relptr = find_rel_for_ptr (xptr);			\
       if (relptr < relend && relptr->ptr == (xptr))	\
 	{						\
 	  if (reltype == SHT_REL)			\
-- 
2.45.0


      reply	other threads:[~2024-05-15 13:38 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-17  6:34 Nikita Popov
2024-04-17 14:49 ` Mark Wielaard
2024-05-15 13:38   ` Mark Wielaard [this message]

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=6b3b0c0b29bebb2f6986a8379a44804e015f94d4.camel@klomp.org \
    --to=mark@klomp.org \
    --cc=debugedit@sourceware.org \
    --cc=npopov@redhat.com \
    /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 read-only IMAP folder(s) and NNTP newsgroup(s).