Skip to content

Commit

Permalink
[ELF] Reduce the size of Subsection struct
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed Sep 29, 2021
1 parent aa46df7 commit a2c9d0a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 23 deletions.
4 changes: 2 additions & 2 deletions elf/icf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ static Digest compute_digest(Context<E> &ctx, InputSection<E> &isec) {
hash((u64)&sym);
} else if (Subsection<E> *subsec = sym.get_subsec()) {
hash('2');
hash_string(subsec->data);
hash((u64)subsec);
} else if (!isec) {
hash('3');
} else if (isec->leader) {
Expand Down Expand Up @@ -298,7 +298,7 @@ static Digest compute_digest(Context<E> &ctx, InputSection<E> &isec) {
SubsectionRef<E> &ref = isec.rel_subsections[subsec_idx++];
hash('a');
isec.get_addend(rel);
hash_string(ref.subsec->data);
hash((u64)ref.subsec);
} else {
hash_symbol(*isec.file.symbols[rel.r_sym]);
}
Expand Down
9 changes: 3 additions & 6 deletions elf/mold.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,15 @@ std::ostream &operator<<(std::ostream &out, const Symbol<E> &sym);

template <typename E>
struct Subsection {
Subsection(MergedSection<E> *sec, std::string_view data)
: output_section(*sec), data(data) {}
Subsection(MergedSection<E> *sec) : output_section(*sec) {}

Subsection(const Subsection &other)
: output_section(other.output_section), data(other.data),
offset(other.offset), alignment(other.alignment.load()),
is_alive(other.is_alive.load()) {}
: output_section(other.output_section), offset(other.offset),
alignment(other.alignment.load()), is_alive(other.is_alive.load()) {}

inline u64 get_addr(Context<E> &ctx) const;

MergedSection<E> &output_section;
std::string_view data;
u32 offset = -1;
std::atomic_uint16_t alignment = 1;
std::atomic_bool is_alive = false;
Expand Down
36 changes: 21 additions & 15 deletions elf/output-chunks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1175,7 +1175,7 @@ MergedSection<E>::insert(std::string_view data, u64 hash, i64 alignment) {

Subsection<E> *subsec;
bool inserted;
std::tie(subsec, inserted) = map.insert(data, hash, Subsection(this, data));
std::tie(subsec, inserted) = map.insert(data, hash, Subsection(this));
assert(subsec);

for (u16 cur = subsec->alignment; cur < alignment;)
Expand All @@ -1193,32 +1193,38 @@ void MergedSection<E>::assign_offsets(Context<E> &ctx) {
i64 shard_size = map.nbuckets / map.NUM_SHARDS;

tbb::parallel_for((i64)0, map.NUM_SHARDS, [&](i64 i) {
std::vector<Subsection<E> *> subsections;
struct KeyVal {
std::string_view key;
Subsection<E> *val;
};

std::vector<KeyVal> subsections;
subsections.reserve(shard_size);

for (i64 j = shard_size * i; j < shard_size * (i + 1); j++)
if (Subsection<E> &subsec = map.values[j]; subsec.is_alive)
subsections.push_back(&subsec);
subsections.push_back({{map.keys[j], map.sizes[j]}, &subsec});

// Sort subsections to make output deterministic.
tbb::parallel_sort(subsections.begin(), subsections.end(),
[](Subsection<E> *a, Subsection<E> *b) {
if (a->alignment != b->alignment)
return a->alignment < b->alignment;
if (a->data.size() != b->data.size())
return a->data.size() < b->data.size();
return a->data < b->data;
[](const KeyVal &a, const KeyVal &b) {
if (a.val->alignment != b.val->alignment)
return a.val->alignment < b.val->alignment;
if (a.key.size() != b.key.size())
return a.key.size() < b.key.size();
return a.key < b.key;
});

// Assign offsets.
i64 offset = 0;
i64 max_alignment = 0;

for (Subsection<E> *subsec : subsections) {
offset = align_to(offset, subsec->alignment);
subsec->offset = offset;
offset += subsec->data.size();
max_alignment = std::max<i64>(max_alignment, subsec->alignment);
for (KeyVal &kv : subsections) {
Subsection<E> &subsec = *kv.val;
offset = align_to(offset, subsec.alignment);
subsec.offset = offset;
offset += kv.key.size();
max_alignment = std::max<i64>(max_alignment, subsec.alignment);
}

sizes[i] = offset;
Expand Down Expand Up @@ -1260,7 +1266,7 @@ void MergedSection<E>::write_to(Context<E> &ctx, u8 *buf) {

for (i64 j = shard_size * i; j < shard_size * (i + 1); j++)
if (Subsection<E> &subsec = map.values[j]; subsec.is_alive)
memcpy(buf + subsec.offset, subsec.data.data(), subsec.data.size());
memcpy(buf + subsec.offset, map.keys[j], map.sizes[j]);
});
}

Expand Down

0 comments on commit a2c9d0a

Please sign in to comment.