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.
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) {