From 6f74e4fb9cee70d5b89c64aa6c9f329e57819f11 Mon Sep 17 00:00:00 2001 From: Rangi42 Date: Thu, 28 Mar 2024 14:49:36 -0400 Subject: [PATCH] Remove the suboptimal `.canPeek()` and `.peek()` methods --- include/asm/lexer.hpp | 7 ------- src/asm/lexer.cpp | 16 ++++++++-------- 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/include/asm/lexer.hpp b/include/asm/lexer.hpp index 38e6e168..8d743aa2 100644 --- a/include/asm/lexer.hpp +++ b/include/asm/lexer.hpp @@ -34,8 +34,6 @@ struct Expansion { size_t offset; // Cursor into `contents` size_t size() const { return contents->size(); } - bool canPeek(uint8_t distance) const { return offset + distance < contents->size(); } - uint8_t peek(uint8_t distance) const { return (*contents)[offset + distance]; } bool advance(); // Increment `offset`; return whether it then exceeds `contents` }; @@ -51,9 +49,6 @@ struct ViewedContent { ViewedContent(ContentSpan const &span_) : span(span_) {} ViewedContent(std::shared_ptr ptr, size_t size) : span({.ptr = ptr, .size = size}) {} - bool canPeek(uint8_t distance) const { return offset + distance < span.size; } - uint8_t peek(uint8_t distance) const { return span.ptr[offset + distance]; } - std::shared_ptr makeSharedContentPtr() const { return std::shared_ptr(span.ptr, &span.ptr[offset]); } @@ -68,8 +63,6 @@ struct BufferedContent { BufferedContent(int fd_) : fd(fd_) {} ~BufferedContent(); - bool canPeek(uint8_t distance) const { return size > distance; } - uint8_t peek(uint8_t distance) const { return buf[(offset + distance) % LEXER_BUF_SIZE]; } void advance(); // Increment `offset` circularly, decrement `size` void refill(); // Read from `fd` to fill `buf` diff --git a/src/asm/lexer.cpp b/src/asm/lexer.cpp index 8e853716..7397b51b 100644 --- a/src/asm/lexer.cpp +++ b/src/asm/lexer.cpp @@ -692,25 +692,25 @@ int LexerState::peek(uint8_t distance) { assert(distance == 0 || distance == 1); for (Expansion &exp : expansions) { - // An expansion that has reached its end will have `exp->offset` == `exp->size()`, + // An expansion that has reached its end will have `exp.offset` == `exp.size()`, // and `.peek()` will continue with its parent assert(exp.offset <= exp.size()); - if (exp.canPeek(distance)) - return exp.peek(distance); + if (exp.offset + distance < exp.size()) + return (uint8_t)(*exp.contents)[exp.offset + distance]; distance -= exp.size() - exp.offset; } if (auto *view = std::get_if(&content); view) { - if (view->canPeek(distance)) - return view->peek(distance); + if (view->offset + distance < view->span.size) + return (uint8_t)view->span.ptr[view->offset + distance]; } else { assert(std::holds_alternative(content)); auto &cbuf = std::get(content); assert(distance < LEXER_BUF_SIZE); - if (!cbuf.canPeek(distance)) + if (cbuf.size <= distance) cbuf.refill(); // Buffer isn't full enough, read some chars in - if (cbuf.canPeek(distance)) - return cbuf.peek(distance); + if (cbuf.size > distance) + return (uint8_t)cbuf.buf[(cbuf.offset + distance) % LEXER_BUF_SIZE]; } // If there aren't enough chars, give up return EOF;