Skip to content

Commit 09f6d07

Browse files
alx32Alex B
authored and
Alex B
committed
Address Feedback nr.1
1 parent 686d003 commit 09f6d07

File tree

1 file changed

+15
-36
lines changed

1 file changed

+15
-36
lines changed

lld/MachO/ObjC.cpp

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -354,13 +354,10 @@ class ObjcCategoryMerger {
354354
ConcatInputSection *catListIsec;
355355
ConcatInputSection *catBodyIsec;
356356
uint32_t offCatListIsec = 0;
357-
uint32_t inputIndex = 0;
358357

359358
bool wasMerged = false;
360359
};
361360

362-
typedef std::vector<InfoInputCategory> CategoryGroup;
363-
364361
// To write new (merged) categories or classes, we will try make limited
365362
// assumptions about the alignment and the sections the various class/category
366363
// info are stored in and . So we'll just reuse the same sections and
@@ -432,14 +429,15 @@ class ObjcCategoryMerger {
432429

433430
private:
434431
void collectAndValidateCategoriesData();
435-
void mergeCategoriesIntoSingleCategory(CategoryGroup &categories);
432+
void
433+
mergeCategoriesIntoSingleCategory(std::vector<InfoInputCategory> &categories);
436434

437435
void eraseISec(ConcatInputSection *isec);
438436
void removeRefsToErasedIsecs();
439437
void eraseMergedCategories();
440438

441439
void generateCatListForNonErasedCategories(
442-
std::map<ConcatInputSection *, std::set<uint64_t>>
440+
MapVector<ConcatInputSection *, std::set<uint64_t>>
443441
catListToErasedOffsets);
444442
void collectSectionWriteInfoFromIsec(const InputSection *isec,
445443
InfoWriteSection &catWriteInfo);
@@ -492,8 +490,8 @@ class ObjcCategoryMerger {
492490

493491
InfoCategoryWriter infoCategoryWriter;
494492
std::vector<ConcatInputSection *> &allInputSections;
495-
// Info for all input categories, grouped by base class
496-
std::vector<CategoryGroup> categoryGroups;
493+
// Map of base class Symbol to list of InfoInputCategory's for it
494+
MapVector<const Symbol *, std::vector<InfoInputCategory>> categoryMap;
497495
// Set for tracking InputSection erased via eraseISec
498496
DenseSet<InputSection *> erasedIsecs;
499497

@@ -1063,12 +1061,6 @@ void ObjcCategoryMerger::createSymbolReference(Defined *refFrom,
10631061
}
10641062

10651063
void ObjcCategoryMerger::collectAndValidateCategoriesData() {
1066-
// Make category merging deterministic by using a counter for found categories
1067-
uint32_t inputCategoryIndex = 0;
1068-
// Map of base class Symbol to list of InfoInputCategory's for it. We use this
1069-
// for fast lookup of categories to their base class. Later this info will be
1070-
// moved into 'categoryGroups' member.
1071-
DenseMap<const Symbol *, std::vector<InfoInputCategory>> categoryMap;
10721064
for (InputSection *sec : allInputSections) {
10731065
if (sec->getName() != section_names::objcCatList)
10741066
continue;
@@ -1098,25 +1090,12 @@ void ObjcCategoryMerger::collectAndValidateCategoriesData() {
10981090
tryGetSymbolAtIsecOffset(catBodyIsec, catLayout.klassOffset);
10991091
assert(classSym && "Category does not have a valid base class");
11001092

1101-
InfoInputCategory catInputInfo{catListCisec, catBodyIsec, off,
1102-
inputCategoryIndex++};
1093+
InfoInputCategory catInputInfo{catListCisec, catBodyIsec, off};
11031094
categoryMap[classSym].push_back(catInputInfo);
11041095

11051096
collectCategoryWriterInfoFromCategory(catInputInfo);
11061097
}
11071098
}
1108-
1109-
// Move categoryMap into categoryGroups and sort by the first category's
1110-
// inputIndex. This way we can be sure that category merging will be
1111-
// deterministic across linker runs.
1112-
categoryGroups.reserve(categoryMap.size());
1113-
for (auto &mapEntry : categoryMap)
1114-
categoryGroups.push_back(mapEntry.second);
1115-
1116-
std::sort(categoryGroups.begin(), categoryGroups.end(),
1117-
[](const CategoryGroup &a, const CategoryGroup &b) {
1118-
return a[0].inputIndex < b[0].inputIndex;
1119-
});
11201099
}
11211100

11221101
// In the input we have multiple __objc_catlist InputSection, each of which may
@@ -1125,7 +1104,7 @@ void ObjcCategoryMerger::collectAndValidateCategoriesData() {
11251104
// (not erased). For these not erased categories, we generate new __objc_catlist
11261105
// entries since the parent __objc_catlist entry will be erased
11271106
void ObjcCategoryMerger::generateCatListForNonErasedCategories(
1128-
const std::map<ConcatInputSection *, std::set<uint64_t>>
1107+
const MapVector<ConcatInputSection *, std::set<uint64_t>>
11291108
catListToErasedOffsets) {
11301109

11311110
// Go through all offsets of all __objc_catlist's that we process and if there
@@ -1192,10 +1171,10 @@ void ObjcCategoryMerger::eraseISec(ConcatInputSection *isec) {
11921171
// them.
11931172
void ObjcCategoryMerger::eraseMergedCategories() {
11941173
// Map of InputSection to a set of offsets of the categories that were merged
1195-
std::map<ConcatInputSection *, std::set<uint64_t>> catListToErasedOffsets;
1174+
MapVector<ConcatInputSection *, std::set<uint64_t>> catListToErasedOffsets;
11961175

1197-
for (auto &catGroup : categoryGroups) {
1198-
for (InfoInputCategory &catInfo : catGroup) {
1176+
for (auto &mapEntry : categoryMap) {
1177+
for (InfoInputCategory &catInfo : mapEntry.second) {
11991178
if (catInfo.wasMerged) {
12001179
eraseISec(catInfo.catListIsec);
12011180
catListToErasedOffsets[catInfo.catListIsec].insert(
@@ -1210,8 +1189,8 @@ void ObjcCategoryMerger::eraseMergedCategories() {
12101189
generateCatListForNonErasedCategories(catListToErasedOffsets);
12111190

12121191
// Erase the old method lists & names of the categories that were merged
1213-
for (auto &catgroup : categoryGroups) {
1214-
for (InfoInputCategory &catInfo : catgroup) {
1192+
for (auto &mapEntry : categoryMap) {
1193+
for (InfoInputCategory &catInfo : mapEntry.second) {
12151194
if (!catInfo.wasMerged)
12161195
continue;
12171196

@@ -1262,10 +1241,10 @@ void ObjcCategoryMerger::removeRefsToErasedIsecs() {
12621241
void ObjcCategoryMerger::doMerge() {
12631242
collectAndValidateCategoriesData();
12641243

1265-
for (auto &catGroup : categoryGroups)
1266-
if (catGroup.size() > 1)
1244+
for (auto &entry : categoryMap)
1245+
if (entry.second.size() > 1)
12671246
// Merge all categories into a new, single category
1268-
mergeCategoriesIntoSingleCategory(catGroup);
1247+
mergeCategoriesIntoSingleCategory(entry.second);
12691248

12701249
// Erase all categories that were merged
12711250
eraseMergedCategories();

0 commit comments

Comments
 (0)