Page MenuHomePhabricator

Optimize lld::elf::ScriptLexer::getLineNumber by avoiding repeated work
AcceptedPublic

Authored by ccross on Fri, Jun 11, 11:16 AM.

Details

Summary

getLineNumber() was counting the number of line feeds from the start
of the buffer to the current token. For large linker scripts this
became a performance bottleneck. For one 4MB linker script over 4
minutes was spent in getLineNumber's StringRef::count.

Store the line number from the last token, and only count the additional
line feeds since the last token.

Diff Detail

Event Timeline

ccross created this revision.Fri, Jun 11, 11:16 AM
ccross requested review of this revision.Fri, Jun 11, 11:16 AM
Herald added a project: Restricted Project. · View Herald TranscriptFri, Jun 11, 11:16 AM
MaskRay added inline comments.Sun, Jun 13, 10:48 AM
lld/ELF/ScriptLexer.cpp
65

the prevailing style is compact. You can move tokOffset immediately below StringRef tok and delete the extra empty lines

// For the first token, or when going backwards, start from the beginning of
// the buffer. If this token is after the previous token start from the previous token.
size_t line = 1;
size_t start = 0;
if (lastLineNumberOffset > 0 && tokOffset >= lastLineNumberOffset) {
MaskRay accepted this revision.Sun, Jun 13, 10:48 AM

Looks great!

This revision is now accepted and ready to land.Sun, Jun 13, 10:48 AM
ccross updated this revision to Diff 352009.Mon, Jun 14, 3:22 PM

Reorder variable definitions to reduce line count

MaskRay accepted this revision.Mon, Jun 14, 6:18 PM

I can commit this on your behalf:)

For one 4MB linker script over 4 minutes was spent in getLineNumber's StringRef::count.

Hmm... A 4MB linker script is weird.