@@ -388,3 +388,74 @@ macho::PriorityBuilder::buildInputSectionPriorities() {
388388
389389 return sectionPriorities;
390390}
391+
392+ void macho::PriorityBuilder::parseOrderFileCString (StringRef path) {
393+ std::optional<MemoryBufferRef> buffer = readFile (path);
394+ if (!buffer) {
395+ error (" Could not read cstring order file at " + path);
396+ return ;
397+ }
398+ MemoryBufferRef mbref = *buffer;
399+ int priority = std::numeric_limits<int >::min ();
400+ for (StringRef line : args::getLines (mbref)) {
401+ if (line.empty ())
402+ continue ;
403+ uint32_t hash = 0 ;
404+ if (!to_integer (line, hash))
405+ continue ;
406+ auto it = cStringPriorities.find (hash);
407+ if (it == cStringPriorities.end ())
408+ cStringPriorities[hash] = ++priority;
409+ else
410+ assert (it->second <= priority);
411+ }
412+ }
413+
414+ std::vector<StringPiecePair> macho::PriorityBuilder::buildCStringPriorities (
415+ ArrayRef<CStringInputSection *> inputs) {
416+ std::vector<StringPiecePair> orderedStringPieces;
417+ if (config->cStringOrderFilePath .empty ()) {
418+ for (CStringInputSection *isec : inputs) {
419+ for (const auto &[stringPieceIdx, piece] :
420+ llvm::enumerate (isec->pieces )) {
421+ if (!piece.live )
422+ continue ;
423+ orderedStringPieces.emplace_back (isec, stringPieceIdx);
424+ }
425+ }
426+ return orderedStringPieces;
427+ }
428+
429+ // Split the input strings into hold and cold sets.
430+ // Order hot set based on -order_file_cstring for performance improvement;
431+ // TODO: Order cold set of cstrings for compression via BP.
432+ std::vector<std::pair<int , StringPiecePair>>
433+ hotStringPrioritiesAndStringPieces;
434+ std::vector<StringPiecePair> coldStringPieces;
435+
436+ for (CStringInputSection *isec : inputs) {
437+ for (const auto &[stringPieceIdx, piece] : llvm::enumerate (isec->pieces )) {
438+ if (!piece.live )
439+ continue ;
440+
441+ auto it = cStringPriorities.find (piece.hash );
442+ if (it != cStringPriorities.end ())
443+ hotStringPrioritiesAndStringPieces.emplace_back (
444+ it->second , std::make_pair (isec, stringPieceIdx));
445+ else
446+ coldStringPieces.emplace_back (isec, stringPieceIdx);
447+ }
448+ }
449+
450+ // Order hot set for perf
451+ llvm::stable_sort (hotStringPrioritiesAndStringPieces);
452+ for (auto &[priority, stringPiecePair] : hotStringPrioritiesAndStringPieces)
453+ orderedStringPieces.push_back (stringPiecePair);
454+
455+ // TODO: Order cold set for compression
456+
457+ orderedStringPieces.insert (orderedStringPieces.end (),
458+ coldStringPieces.begin (), coldStringPieces.end ());
459+
460+ return orderedStringPieces;
461+ }
0 commit comments