Skip to content

Commit d517764

Browse files
committed
[llvm-objdump][NFC] Make the PrettyPrinter::printInst() output buffered
Summary: Every time PrettyPrinter::printInst is called, stdout is flushed and it makes llvm-objdump slow. This patches adds a string buffer to prevent stdout from being flushed. Benchmark results (./llvm-objdump-master: without this patch, ./bin/llvm-objcopy: with this patch): $ hyperfine --warmup 10 './llvm-objdump-master -d ./bin/llvm-objcopy' './bin/llvm-objdump -d ./bin/llvm-objcopy' Benchmark llvm#1: ./llvm-objdump-master -d ./bin/llvm-objcopy Time (mean ± σ): 2.230 s ± 0.050 s [User: 1.533 s, System: 0.682 s] Range (min … max): 2.115 s … 2.278 s 10 runs Benchmark #2: ./bin/llvm-objdump -d ./bin/llvm-objcopy Time (mean ± σ): 386.4 ms ± 13.0 ms [User: 376.6 ms, System: 6.1 ms] Range (min … max): 366.1 ms … 407.0 ms 10 runs Summary './bin/llvm-objdump -d ./bin/llvm-objcopy' ran 5.77 ± 0.23 times faster than './llvm-objdump-master -d ./bin/llvm-objcopy' Reviewers: alexshap, Bigcheese, jhenderson, rupprecht, grimar, MaskRay Reviewed By: jhenderson, MaskRay Subscribers: dexonsmith, jhenderson, javed.absar, kristof.beyls, rupprecht, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D64969 llvm-svn: 366984
1 parent 5b406d7 commit d517764

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

llvm/tools/llvm-objdump/llvm-objdump.cpp

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -668,24 +668,20 @@ class PrettyPrinter {
668668
if (SP && (PrintSource || PrintLines))
669669
SP->printSourceLine(OS, Address);
670670

671-
{
672-
formatted_raw_ostream FOS(OS);
673-
if (!NoLeadingAddr)
674-
FOS << format("%8" PRIx64 ":", Address.Address);
675-
if (!NoShowRawInsn) {
676-
FOS << ' ';
677-
dumpBytes(Bytes, FOS);
678-
}
679-
FOS.flush();
680-
// The output of printInst starts with a tab. Print some spaces so that
681-
// the tab has 1 column and advances to the target tab stop.
682-
unsigned TabStop = NoShowRawInsn ? 16 : 40;
683-
unsigned Column = FOS.getColumn();
684-
FOS.indent(Column < TabStop - 1 ? TabStop - 1 - Column : 7 - Column % 8);
685-
686-
// The dtor calls flush() to ensure the indent comes before printInst().
671+
size_t Start = OS.tell();
672+
if (!NoLeadingAddr)
673+
OS << format("%8" PRIx64 ":", Address.Address);
674+
if (!NoShowRawInsn) {
675+
OS << ' ';
676+
dumpBytes(Bytes, OS);
687677
}
688678

679+
// The output of printInst starts with a tab. Print some spaces so that
680+
// the tab has 1 column and advances to the target tab stop.
681+
unsigned TabStop = NoShowRawInsn ? 16 : 40;
682+
unsigned Column = OS.tell() - Start;
683+
OS.indent(Column < TabStop - 1 ? TabStop - 1 - Column : 7 - Column % 8);
684+
689685
if (MI)
690686
IP.printInst(MI, OS, "", STI);
691687
else

0 commit comments

Comments
 (0)