3
0
Fork 0
mirror of https://github.com/YosysHQ/yosys synced 2025-06-07 06:33:24 +00:00

read_liberty: Faster std::string construction in the liberty lexer

This extends the `LibertyInputStream` added in the previous commit to
allow arbitrary lookahead. Then this uses the lookahead to find the
total length of the token within the input buffer, instead of consuming
the token byte by byte while appending to a std::string. Constructing
the std::string with the total length is known avoids any reallocations
from growing std::string's buffer.
This commit is contained in:
Jannis Harder 2025-04-01 13:50:29 +02:00
parent 119e998f12
commit bc01468c75
2 changed files with 46 additions and 20 deletions

View file

@ -101,6 +101,7 @@ namespace Yosys
bool extend_buffer_at_least(size_t size = 1);
YS_COLD int get_cold();
YS_COLD int peek_cold(size_t offset);
public:
LibertyInputStream(std::istream &f) : f(f) {}
@ -116,6 +117,16 @@ namespace Yosys
return c;
}
int peek(size_t offset = 0) {
if (buf_pos + offset >= buf_end)
return peek_cold(offset);
return buffer[buf_pos + offset];
}
void consume(size_t n = 1) {
buf_pos += n;
}
void unget() {
buf_pos -= 1;
}