For循环略微性能提升

This commit is contained in:
2026-02-08 09:18:31 +08:00
parent 52b75f17da
commit e1c765aade
3 changed files with 20 additions and 6 deletions

View File

@@ -6,6 +6,6 @@ def fib(x:int) -> int:
if __name__ == '__main__': if __name__ == '__main__':
t0 = tt() t0 = tt()
result = fib(25) result = fib(30)
t1 = tt() t1 = tt()
print('cost: ',t1-t0, 'result:', result) print('cost: ',t1-t0, 'result:', result)

View File

@@ -80,6 +80,13 @@ namespace Fig
// c.structTypeNames.end()); // c.structTypeNames.end());
} }
void clear()
{
variables.clear();
implRegistry.clear();
opRegistry.clear();
}
std::unordered_map<size_t, Function> getFunctions() const std::unordered_map<size_t, Function> getFunctions() const
{ {
std::unordered_map<size_t, Function> result; std::unordered_map<size_t, Function> result;

View File

@@ -327,7 +327,7 @@ namespace Fig
FString(std::format("<Function {}>", opFnName.toBasicString())), ctx); FString(std::format("<Function {}>", opFnName.toBasicString())), ctx);
const auto &fillOpFnParas = [this, structType, implMethod, opFnName, fnCtx, ctx, paraCnt]( const auto &fillOpFnParas = [this, structType, implMethod, opFnName, fnCtx, ctx, paraCnt](
const std::vector<ObjectPtr> &args) { const std::vector<ObjectPtr> &args) -> StatementResult {
const Ast::FunctionParameters &paras = implMethod.paras; const Ast::FunctionParameters &paras = implMethod.paras;
for (size_t i = 0; i < paraCnt; ++i) for (size_t i = 0; i < paraCnt; ++i)
{ {
@@ -345,6 +345,7 @@ namespace Fig
} }
fnCtx->def(paras.posParas[i].first, paraType, AccessModifier::Normal, args[i]); fnCtx->def(paras.posParas[i].first, paraType, AccessModifier::Normal, args[i]);
} }
return StatementResult::normal();
}; };
if (paraCnt == 1) if (paraCnt == 1)
@@ -536,6 +537,11 @@ namespace Fig
loopContext); // ignore init statement result loopContext); // ignore init statement result
size_t iteration = 0; size_t iteration = 0;
ContextPtr iterationContext = std::make_shared<Context>(
FString(std::format(
"<For {}:{}, Iteration {}>", forSt->getAAI().line, forSt->getAAI().column, iteration)),
loopContext); // every loop has its own context
while (true) // use while loop to simulate for loop, cause we while (true) // use while loop to simulate for loop, cause we
// need to check condition type every iteration // need to check condition type every iteration
{ {
@@ -549,11 +555,12 @@ namespace Fig
} }
if (!condVal->as<ValueType::BoolClass>()) { break; } if (!condVal->as<ValueType::BoolClass>()) { break; }
iteration++; iteration++;
ContextPtr iterationContext = std::make_shared<Context>(
FString(std::format(
"<For {}:{}, Iteration {}>", forSt->getAAI().line, forSt->getAAI().column, iteration)),
loopContext); // every loop has its own context
StatementResult sr = evalBlockStatement(forSt->body, iterationContext); StatementResult sr = evalBlockStatement(forSt->body, iterationContext);
iterationContext->clear();
iterationContext->setScopeName(FString(std::format(
"<For {}:{}, Iteration {}>", forSt->getAAI().line, forSt->getAAI().column, iteration)));
if (sr.shouldReturn()) { return sr; } if (sr.shouldReturn()) { return sr; }
if (sr.shouldBreak()) { break; } if (sr.shouldBreak()) { break; }
if (sr.shouldContinue()) if (sr.shouldContinue())