Skip to content

Commit

Permalink
evaluator: Make std::mem::find_sequence a lot faster
Browse files Browse the repository at this point in the history
  • Loading branch information
WerWolv committed Jul 4, 2024
1 parent 6830e01 commit 544bb88
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 25 deletions.
4 changes: 2 additions & 2 deletions lib/include/pl/core/evaluator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ namespace pl::core {
}

void handleAbort() const {
if (this->m_aborted)
if (this->m_aborted) [[unlikely]]
err::E0007.throwError("Evaluation aborted by user.");
}

Expand Down Expand Up @@ -359,7 +359,7 @@ namespace pl::core {
this->m_mainSectionEditsAllowed = true;
}

[[nodiscard]] std::unique_ptr<Evaluator::UpdateHandler> updateRuntime(const ast::ASTNode *node);
[[nodiscard]] Evaluator::UpdateHandler updateRuntime(const ast::ASTNode *node);

void addBreakpoint(u64 line);
void removeBreakpoint(u64 line);
Expand Down
29 changes: 16 additions & 13 deletions lib/source/pl/core/evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@ namespace pl::core {
err::E0012.throwError(fmt::format("Tried accessing a non-existing section with id {}.", sectionId));
}

if (this->isDebugModeEnabled())
if (this->isDebugModeEnabled()) [[unlikely]]
this->m_console.log(LogConsole::Level::Debug, fmt::format("{} {} bytes from address 0x{:02X} in section {:02X}", write ? "Writing" : "Reading", size, address, sectionId));
}

Expand Down Expand Up @@ -1094,20 +1094,23 @@ namespace pl::core {
return;

evaluator->handleAbort();
auto temp = node->getLocation().line;
const auto line = temp + (temp == 0);
if (evaluator->m_shouldPauseNextLine && evaluator->m_lastPauseLine != line) {
evaluator->m_shouldPauseNextLine = false;
evaluator->m_lastPauseLine = line;
evaluator->m_breakpointHitCallback();
} else if (evaluator->m_breakpoints.contains(line)) {
if (evaluator->m_lastPauseLine != line) {

if (node != nullptr) {
auto rawLine = node->getLocation().line;
const auto line = rawLine + (rawLine == 0);
if (evaluator->m_shouldPauseNextLine && evaluator->m_lastPauseLine != line) {
evaluator->m_shouldPauseNextLine = false;
evaluator->m_lastPauseLine = line;
evaluator->m_breakpointHitCallback();
} else if (evaluator->m_breakpoints.contains(line)) {
if (evaluator->m_lastPauseLine != line) {
evaluator->m_lastPauseLine = line;
evaluator->m_breakpointHitCallback();
}
}
}

evaluator->m_callStack.push_back(node->clone());
evaluator->m_callStack.push_back(node->clone());
}
}

Evaluator::UpdateHandler::~UpdateHandler() {
Expand All @@ -1122,8 +1125,8 @@ namespace pl::core {
evaluator->m_callStack.pop_back();
}

std::unique_ptr<Evaluator::UpdateHandler> Evaluator::updateRuntime(const ast::ASTNode *node) {
return std::make_unique<UpdateHandler>(this, node);
Evaluator::UpdateHandler Evaluator::updateRuntime(const ast::ASTNode *node) {
return { this, node };
}

void Evaluator::addBreakpoint(u64 line) { this->m_breakpoints.insert(line); }
Expand Down
33 changes: 23 additions & 10 deletions lib/source/pl/lib/std/mem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,33 @@
namespace pl::lib::libstd::mem {

static std::optional<u128> findSequence(::pl::core::Evaluator *ctx, u64 occurrenceIndex, u64 offsetFrom, u64 offsetTo, const std::vector<u8> &sequence) {
std::vector<u8> bytes(sequence.size(), 0x00);
u32 occurrences = 0;
u32 occurrences = 0;
const u64 bufferSize = ctx->getDataSize();
const u64 endOffset = offsetTo <= offsetFrom ? bufferSize : std::min(bufferSize, u64(offsetTo));
for (u64 offset = offsetFrom; offset < endOffset - sequence.size(); offset++) {
ctx->readData(offset, bytes.data(), bytes.size(), ptrn::Pattern::MainSectionId);

if (bytes == sequence) {
if (occurrences < occurrenceIndex) {
occurrences++;
continue;
}
std::vector<u8> bytes(std::max(sequence.size(), size_t(4 * 1024)), 0x00);
for (u64 offset = offsetFrom; offset < endOffset; offset += bytes.size()) {
const auto bytesToRead = std::min(bytes.size(), endOffset - offset);
ctx->readData(offset, bytes.data(), bytesToRead, ptrn::Pattern::MainSectionId);
ctx->handleAbort();

for (u64 i = 0; i < bytes.size(); i += 1) {
if (bytes[i] == sequence[0]) [[unlikely]] {
bool found = true;
for (u64 j = 1; j < sequence.size(); j++) {
if (bytes[i + j] != sequence[j]) {
found = false;
break;
}
}

return u128(offset);
if (found) [[unlikely]] {
if (occurrences >= occurrenceIndex)
return offset + i;

occurrences++;
}
}
}
}

Expand Down

0 comments on commit 544bb88

Please sign in to comment.