[PATCH] D104137: Optimize lld::elf::ScriptLexer::getLineNumber by avoiding repeated work
Fangrui Song via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Tue Jun 22 15:35:43 PDT 2021
This revision was automatically updated to reflect the committed changes.
Closed by commit rGe387778722f9: [ELF] Optimize ScriptLexer::getLineNumber by caching the previous line number… (authored by ccross, committed by MaskRay).
Changed prior to commit:
https://reviews.llvm.org/D104137?vs=352009&id=353808#toc
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,25 @@
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. 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) {
+ start = lastLineNumberOffset;
+ line = lastLineNumber;
+ }
+
+ 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.353808.patch
Type: text/x-patch
Size: 1331 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210622/b26d17c4/attachment.bin>
More information about the llvm-commits
mailing list