Skip to content

Commit 048b7b2

Browse files
authored
Merge pull request #1587 from bamo/perf/cache-input-section-name
perf: cache InputSection name to avoid per-call strlen
2 parents 8b5eeb8 + c32946d commit 048b7b2

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

src/input-sections.cc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,15 @@ bool cie_equals(const CieRecord<E> &a, const CieRecord<E> &b) {
3333
template <typename E>
3434
InputSection<E>::InputSection(Context<E> &ctx, ObjectFile<E> &file, i64 shndx)
3535
: file(file), shndx(shndx) {
36-
if (shndx < file.elf_sections.size())
36+
if (shndx < file.elf_sections.size()) {
3737
contents = {(char *)file.mf->data + shdr().sh_offset, (size_t)shdr().sh_size};
38+
const char *p = file.shstrtab.data() + file.elf_sections[shndx].sh_name;
39+
cached_name = std::string_view(p, strlen(p));
40+
} else {
41+
cached_name = (shdr().sh_flags & SHF_TLS)
42+
? std::string_view(".tls_common", 11)
43+
: std::string_view(".common", 7);
44+
}
3845

3946
if (shdr().sh_flags & SHF_COMPRESSED) {
4047
ElfChdr<E> &chdr = *(ElfChdr<E> *)&contents[0];

src/mold.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,9 @@ class __attribute__((aligned(4))) InputSection {
535535

536536
std::string_view contents;
537537

538+
// Cached to avoid per-call strlen on the shstrtab entry.
539+
std::string_view cached_name;
540+
538541
i32 fde_begin = -1;
539542
i32 fde_end = -1;
540543

@@ -2942,9 +2945,7 @@ inline u64 InputSection<E>::get_addr() const {
29422945

29432946
template <typename E>
29442947
inline std::string_view InputSection<E>::name() const {
2945-
if (file.elf_sections.size() <= shndx)
2946-
return (shdr().sh_flags & SHF_TLS) ? ".tls_common" : ".common";
2947-
return file.shstrtab.data() + file.elf_sections[shndx].sh_name;
2948+
return cached_name;
29482949
}
29492950

29502951
template <typename E>

0 commit comments

Comments
 (0)