public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Oleg Tolmatcev <oleg.tolmatcev@gmail.com>
To: binutils@sourceware.org
Subject: [PATCH] make coff_compute_checksum faster
Date: Thu, 27 Apr 2023 20:24:48 +0200	[thread overview]
Message-ID: <CACcXsZjzrLPP5Ngo2bt20hkCQF+uvZ2syNvz5rW-bha_mDTfQw@mail.gmail.com> (raw)

This makes linking faster by reading 8 MB of data at once instead of
reading 2 bytes at once. This drastically reduces the number of system
calls.

---
 bfd/coffcode.h | 52 ++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 44 insertions(+), 8 deletions(-)

diff --git a/bfd/coffcode.h b/bfd/coffcode.h
index 19a5ce18..8118ff47 100644
--- a/bfd/coffcode.h
+++ b/bfd/coffcode.h
@@ -3361,30 +3361,66 @@ coff_read_word (bfd *abfd, unsigned int
*value, unsigned int *pelength)
   return true;
 }

+static bool
+coff_read_word_from_buffer (unsigned char *b, int buf_size,
+                            unsigned int *value, unsigned int *pelength)
+{
+
+  if (buf_size < 1)
+    {
+      *value = 0;
+      return false;
+    }
+
+  if (buf_size == 1)
+    *value = (unsigned int)b[0];
+  else
+    *value = (unsigned int)(b[0] + (b[1] << 8));
+
+  *pelength += buf_size == 1 ? 1 : 2;
+
+  return true;
+}
+
 static unsigned int
 coff_compute_checksum (bfd *abfd, unsigned int *pelength)
 {
-  bool more_data;
   file_ptr filepos;
   unsigned int value;
   unsigned int total;
+  unsigned char *buf;
+  unsigned char *cur_buf;
+  int buf_size;
+  int cur_buf_size;

   total = 0;
   *pelength = 0;
-  filepos = (file_ptr) 0;
+  filepos = (file_ptr)0;
+  buf = (unsigned char *)bfd_malloc (0x800000);
+  if (buf == NULL)
+    return 0;

   do
     {
       if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
-    return 0;
+        return 0;

-      more_data = coff_read_word (abfd, &value, pelength);
-      total += value;
-      total = 0xffff & (total + (total >> 0x10));
-      filepos += 2;
+      buf_size = bfd_bread (buf, 0x800000, abfd);
+      cur_buf_size = buf_size;
+      cur_buf = buf;
+      while (cur_buf_size > 0)
+        {
+          coff_read_word_from_buffer (cur_buf, cur_buf_size, &value, pelength);
+          cur_buf += 2;
+          cur_buf_size -= 2;
+          total += value;
+          total = 0xffff & (total + (total >> 0x10));
+        }
+      filepos += buf_size;
     }
-  while (more_data);
+  while (buf_size > 0);

+  free (buf);
   return (0xffff & (total + (total >> 0x10)));
 }

-- 
2.40.0.windows.1

             reply	other threads:[~2023-04-27 18:25 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-27 18:24 Oleg Tolmatcev [this message]
2023-05-03 14:39 ` Nick Clifton
2023-05-03 18:10   ` Oleg Tolmatcev

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=CACcXsZjzrLPP5Ngo2bt20hkCQF+uvZ2syNvz5rW-bha_mDTfQw@mail.gmail.com \
    --to=oleg.tolmatcev@gmail.com \
    --cc=binutils@sourceware.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 read-only IMAP folder(s) and NNTP newsgroup(s).