[PATCH] D104137: Optimize lld::elf::ScriptLexer::getLineNumber by avoiding repeated work
Colin Cross via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 14 15:22:04 PDT 2021
ccross updated this revision to Diff 352009.
ccross added a comment.
Reorder variable definitions to reduce line count
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D104137/new/
https://reviews.llvm.org/D104137
Files:
lld/ELF/ScriptLexer.cpp
lld/ELF/ScriptLexer.h
Index: lld/ELF/ScriptLexer.h
===================================================================
--- lld/ELF/ScriptLexer.h
+++ lld/ELF/ScriptLexer.h
@@ -40,6 +40,9 @@
bool inExpr = false;
size_t pos = 0;
+ size_t lastLineNumber = 0;
+ size_t lastLineNumberOffset = 0;
+
protected:
MemoryBufferRef getCurrentMB();
Index: lld/ELF/ScriptLexer.cpp
===================================================================
--- lld/ELF/ScriptLexer.cpp
+++ lld/ELF/ScriptLexer.cpp
@@ -56,7 +56,27 @@
return 1;
StringRef s = getCurrentMB().getBuffer();
StringRef tok = tokens[pos - 1];
- return s.substr(0, tok.data() - s.data()).count('\n') + 1;
+ const size_t tokOffset = tok.data() - s.data();
+
+ // For the first token, or when going backwards, start from the beginning of
+ // the buffer.
+ size_t line = 1;
+ size_t start = 0;
+
+ // If this token is after the previous token start from the previous token.
+ if (lastLineNumberOffset > 0 && tokOffset >= lastLineNumberOffset) {
+ start = lastLineNumberOffset;
+ line = lastLineNumber;
+ }
+
+ // Add the number of linefeeds since the start of the region of interest.
+ line += s.substr(start, tokOffset - start).count('\n');
+
+ // Store the line number of this token for reuse.
+ lastLineNumberOffset = tokOffset;
+ lastLineNumber = line;
+
+ return line;
}
// Returns 0-based column number of the current token.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D104137.352009.patch
Type: text/x-patch
Size: 1409 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210614/de26895d/attachment.bin>
More information about the llvm-commits
mailing list