Skip to content

Commit 7351449

Browse files
authored
evaluator: Optimize call stack recording (WerWolv#213)
Moved call stack entry recording to stack unwinding phase when exception is active. Eliminates overhead in normal execution.
1 parent 6efa38c commit 7351449

3 files changed

Lines changed: 8 additions & 8 deletions

File tree

lib/include/pl/core/evaluator.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ namespace pl::core {
100100
~UpdateHandler();
101101

102102
Evaluator *evaluator;
103+
const ast::ASTNode *node = nullptr;
104+
u64 offset = 0;
103105
};
104106

105107
struct StackTrace {

lib/source/pl/core/evaluator.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,20 +1155,18 @@ namespace pl::core {
11551155
evaluator->m_lastPauseLine = std::nullopt;
11561156
}
11571157
}
1158-
evaluator->m_callStack.emplace_back(node->clone(), evaluator->getReadOffset());
1158+
this->node = node;
1159+
this->offset = evaluator->getReadOffset();
11591160
}
11601161
}
11611162

11621163
Evaluator::UpdateHandler::~UpdateHandler() {
11631164
if (evaluator->m_evaluated)
11641165
return;
11651166

1166-
// Don't pop scopes if an exception is currently being thrown so we can generate
1167-
// a stack trace
1168-
if (std::uncaught_exceptions() > 0)
1169-
return;
1170-
1171-
evaluator->m_callStack.pop_back();
1167+
if (std::uncaught_exceptions() > 0) [[unlikely]]
1168+
if (node != nullptr)
1169+
evaluator->m_callStack.emplace_back(node->clone(), offset);
11721170
}
11731171

11741172
Evaluator::UpdateHandler Evaluator::updateRuntime(const ast::ASTNode *node) {

lib/source/pl/pattern_language.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ namespace pl {
267267

268268
const auto &callStack = evaluator->getCallStack();
269269
u32 lastLine = 0;
270-
for (const auto &entry : callStack | std::views::reverse) {
270+
for (const auto &entry : callStack) {
271271
const auto &[node, address] = entry;
272272
if (node == nullptr)
273273
continue;

0 commit comments

Comments
 (0)