[PATCH] Optimize reading ELF objects with many group sections

Jens Widell jl@opera.com
Mon Jan 8 09:54:00 GMT 2018


When processing a section that is a member of a group, the group
that contains it is looked up using a linear search. The resulting
O(n^2) complexity causes significant performance issues when
dealing with object files with very many groups.

By remembering the index of the last found group and restarting
the next search from that index, the search instead becomes O(n)
in common cases.

Signed-off-by: Jens Widell <jl@opera.com>
---
Background: This turned out to be a significant issue when building
Chromium with debug fission, which means clang calls objcopy twice at
the end of a compilation to extract debug information into a separate
file, and unity building, which combines many source files into a
single compilation unit to reduce overall build time, thus procuding
some very large object files with many sections.

 bfd/elf-bfd.h |  1 +
 bfd/elf.c     | 15 ++++++++++++---
 2 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/bfd/elf-bfd.h b/bfd/elf-bfd.h
index 9c9b0c7..07879e5 100644
--- a/bfd/elf-bfd.h
+++ b/bfd/elf-bfd.h
@@ -1908,6 +1908,7 @@ struct elf_obj_tdata
 
   Elf_Internal_Shdr **group_sect_ptr;
   int num_group;
+  unsigned int group_search_offset;
 
   unsigned int symtab_section, dynsymtab_section;
   unsigned int dynversym_section, dynverdef_section, dynverref_section;
diff --git a/bfd/elf.c b/bfd/elf.c
index 9f44ff9..d7d084e 100644
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -737,10 +737,17 @@ setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
 
   if (num_group != (unsigned) -1)
     {
-      unsigned int i;
+      unsigned int search_offset = elf_tdata (abfd)->group_search_offset;
+      unsigned int j;
 
-      for (i = 0; i < num_group; i++)
+      for (j = 0; j < num_group; j++)
 	{
+	  /* Begin search from previous found group. */
+	  unsigned i = j + search_offset;
+
+	  if (i >= num_group)
+	    i -= num_group;
+
 	  Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
 	  Elf_Internal_Group *idx;
 	  bfd_size_type n_elt;
@@ -803,7 +810,9 @@ setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
 		if (shdr->bfd_section != NULL)
 		  elf_next_in_group (shdr->bfd_section) = newsect;
 
-		i = num_group - 1;
+		elf_tdata (abfd)->group_search_offset = i;
+
+		j = num_group - 1;
 		break;
 	      }
 	}
-- 
2.7.4



More information about the Binutils mailing list