Skip to content

Commit a2c9d0a

Browse files
committed
[ELF] Reduce the size of Subsection struct
1 parent aa46df7 commit a2c9d0a

File tree

3 files changed

+26
-23
lines changed

3 files changed

+26
-23
lines changed

elf/icf.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ static Digest compute_digest(Context<E> &ctx, InputSection<E> &isec) {
249249
hash((u64)&sym);
250250
} else if (Subsection<E> *subsec = sym.get_subsec()) {
251251
hash('2');
252-
hash_string(subsec->data);
252+
hash((u64)subsec);
253253
} else if (!isec) {
254254
hash('3');
255255
} else if (isec->leader) {
@@ -298,7 +298,7 @@ static Digest compute_digest(Context<E> &ctx, InputSection<E> &isec) {
298298
SubsectionRef<E> &ref = isec.rel_subsections[subsec_idx++];
299299
hash('a');
300300
isec.get_addend(rel);
301-
hash_string(ref.subsec->data);
301+
hash((u64)ref.subsec);
302302
} else {
303303
hash_symbol(*isec.file.symbols[rel.r_sym]);
304304
}

elf/mold.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,15 @@ std::ostream &operator<<(std::ostream &out, const Symbol<E> &sym);
7373

7474
template <typename E>
7575
struct Subsection {
76-
Subsection(MergedSection<E> *sec, std::string_view data)
77-
: output_section(*sec), data(data) {}
76+
Subsection(MergedSection<E> *sec) : output_section(*sec) {}
7877

7978
Subsection(const Subsection &other)
80-
: output_section(other.output_section), data(other.data),
81-
offset(other.offset), alignment(other.alignment.load()),
82-
is_alive(other.is_alive.load()) {}
79+
: output_section(other.output_section), offset(other.offset),
80+
alignment(other.alignment.load()), is_alive(other.is_alive.load()) {}
8381

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

8684
MergedSection<E> &output_section;
87-
std::string_view data;
8885
u32 offset = -1;
8986
std::atomic_uint16_t alignment = 1;
9087
std::atomic_bool is_alive = false;

elf/output-chunks.cc

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,7 +1175,7 @@ MergedSection<E>::insert(std::string_view data, u64 hash, i64 alignment) {
11751175

11761176
Subsection<E> *subsec;
11771177
bool inserted;
1178-
std::tie(subsec, inserted) = map.insert(data, hash, Subsection(this, data));
1178+
std::tie(subsec, inserted) = map.insert(data, hash, Subsection(this));
11791179
assert(subsec);
11801180

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

11951195
tbb::parallel_for((i64)0, map.NUM_SHARDS, [&](i64 i) {
1196-
std::vector<Subsection<E> *> subsections;
1196+
struct KeyVal {
1197+
std::string_view key;
1198+
Subsection<E> *val;
1199+
};
1200+
1201+
std::vector<KeyVal> subsections;
11971202
subsections.reserve(shard_size);
11981203

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

12031208
// Sort subsections to make output deterministic.
12041209
tbb::parallel_sort(subsections.begin(), subsections.end(),
1205-
[](Subsection<E> *a, Subsection<E> *b) {
1206-
if (a->alignment != b->alignment)
1207-
return a->alignment < b->alignment;
1208-
if (a->data.size() != b->data.size())
1209-
return a->data.size() < b->data.size();
1210-
return a->data < b->data;
1210+
[](const KeyVal &a, const KeyVal &b) {
1211+
if (a.val->alignment != b.val->alignment)
1212+
return a.val->alignment < b.val->alignment;
1213+
if (a.key.size() != b.key.size())
1214+
return a.key.size() < b.key.size();
1215+
return a.key < b.key;
12111216
});
12121217

12131218
// Assign offsets.
12141219
i64 offset = 0;
12151220
i64 max_alignment = 0;
12161221

1217-
for (Subsection<E> *subsec : subsections) {
1218-
offset = align_to(offset, subsec->alignment);
1219-
subsec->offset = offset;
1220-
offset += subsec->data.size();
1221-
max_alignment = std::max<i64>(max_alignment, subsec->alignment);
1222+
for (KeyVal &kv : subsections) {
1223+
Subsection<E> &subsec = *kv.val;
1224+
offset = align_to(offset, subsec.alignment);
1225+
subsec.offset = offset;
1226+
offset += kv.key.size();
1227+
max_alignment = std::max<i64>(max_alignment, subsec.alignment);
12221228
}
12231229

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

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

0 commit comments

Comments
 (0)