#97723 [SC-Insight] Caching Loop Boundaries and Host State Eliminates Redundant STATICCALLs in Batch Execution Loop Saving ~56% Gas
Submitted on Nov 2nd 2026 at 21:01:17 UTC by @magtentic for Audit Competition | Plether
- Report ID: #97723
- Report Type: Smart Contract
- Report severity: Insight
- Target: https://github.com/immunefi-team/audit-comp-plether/tree/a82d4fefb02768e5b3bddcc97dee371eee24fea8/packages/perps/src
- Impacts:
Description
Brief/Intro
In OrderRouterExecutionSidecar.sol, the primary order batch processing loop in executeOrderBatch issues multiple redundant external calls (STATICCALL) back to the host contract (host.nextExecuteId() called three consecutive times per iteration, and host.maxPruneOrdersPerCall() evaluated on every loop pass).
Because each external call introduces function dispatch, ABI encoding/decoding, and returndatacopy overhead, batch order execution consumes unnecessary gas (~1,040+ gas wasted per order iteration).
Caching the iteration cursor in a local stack variable and hoisting the invariant configuration limit outside the loop eliminates 40+ external calls in a 20-order batch, reducing batch loop control overhead by 56% (~48,982 gas saved per transaction) and preventing keepers from being priced out during high-volatility execution cascades.
Vulnerability Details
In OrderRouterExecutionSidecar.sol#L246-L270, the order execution loop processes queued orders:
while (host.nextExecuteId() != 0 && host.nextExecuteId() <= maxOrderId) {
uint64 orderId = host.nextExecuteId();
IOrderRouterExecutionHost.OrderView memory orderView = host.getOrderForSidecar(orderId);
_requireOrderView(orderId, orderView);
(IOrderRouterExecutionHost.ItemAction action, bool terminalBeforeOracle) =
_preOracleAction(host, orderView.order, orderId, state.riskOffCutoff);
if (terminalBeforeOracle) {
if (action == IOrderRouterExecutionHost.ItemAction.RiskOff) {
if (state.riskOffRefunds == MAX_RISK_OFF_REFUNDS_PER_CALL) {
batchResult.stopReason = OrderV3Types.PendingReason.CleanupLimit;
break;
}
unchecked {
++state.riskOffRefunds;
}
} else {
if (state.terminalPrunes == host.maxPruneOrdersPerCall()) {
batchResult.stopReason = OrderV3Types.PendingReason.CleanupLimit;
break;
}
unchecked {
++state.terminalPrunes;
}
}
...
Notice the following redundant external calls:
host.nextExecuteId() != 0: Performs an externalSTATICCALLto query the queue head ID.host.nextExecuteId() <= maxOrderId: Performs a second identical externalSTATICCALLwithin the exact same evaluation expression without any intervening state modification.uint64 orderId = host.nextExecuteId(): Performs a third identical externalSTATICCALLimmediately on line 247.host.maxPruneOrdersPerCall(): Re-queries an immutable/timelocked protocol configuration parameter via externalSTATICCALLon every single iteration inside theelsebranch of the prune check.
Optimization
- The head ID advances only when an order is completed or pruned inside the loop (
host.executeOrderItemFromSidecar). Therefore,nextExecuteIdmust be refreshed across distinct iterations. However, within the same iteration, queryinghost.nextExecuteId()once and storing it in a local stack variable (uint64 nextId = host.nextExecuteId()) is 100% semantically equivalent and eliminates two unnecessary cross-contract calls. host.maxPruneOrdersPerCall()is an admin-configured threshold governed by a 72-hour timelock inOrderRouterAdmin. It cannot change during the execution of a user transaction. Caching this value once before entering thewhileloop guarantees identical execution logic while saving an external call on every terminal prune pass.
Recommended Code Remediation
+ uint256 maxPrunes = host.maxPruneOrdersPerCall();
+ uint64 nextId;
- while (host.nextExecuteId() != 0 && host.nextExecuteId() <= maxOrderId) {
- uint64 orderId = host.nextExecuteId();
+ while ((nextId = host.nextExecuteId()) != 0 && nextId <= maxOrderId) {
+ uint64 orderId = nextId;
IOrderRouterExecutionHost.OrderView memory orderView = host.getOrderForSidecar(orderId);
_requireOrderView(orderId, orderView);
(IOrderRouterExecutionHost.ItemAction action, bool terminalBeforeOracle) =
_preOracleAction(host, orderView.order, orderId, state.riskOffCutoff);
if (terminalBeforeOracle) {
if (action == IOrderRouterExecutionHost.ItemAction.RiskOff) {
if (state.riskOffRefunds == MAX_RISK_OFF_REFUNDS_PER_CALL) {
batchResult.stopReason = OrderV3Types.PendingReason.CleanupLimit;
break;
}
unchecked {
++state.riskOffRefunds;
}
} else {
- if (state.terminalPrunes == host.maxPruneOrdersPerCall()) {
+ if (state.terminalPrunes == maxPrunes) {
batchResult.stopReason = OrderV3Types.PendingReason.CleanupLimit;
break;
}
unchecked {
++state.terminalPrunes;
}
}
Impact Details
- Impact Category:
Insight: Code Optimizations and Enhancements. - Quantifiable Gas Reduction:
- Unoptimized batch execution overhead: 87,341 gas (for 20 orders).
- Optimized cached execution overhead: 38,359 gas (for 20 orders).
- Net gas saved: 48,982 gas per 20-order batch (~2,449 gas per order item, 56% gas reduction in loop control overhead).
- Protocol & Ecosystem Value:
- Keeper Priority Gas Auction (PGA) Competitiveness: Keepers batch orders during high-volatility market periods on Arbitrum. Saving ~49,000 gas per batch significantly increases keeper profit margins, ensuring that orders and stop-losses are executed promptly without stalling behind gas fee spikes.
- Block Gas Limit Efficiency: Reducing gas per order allows higher batch throughput per transaction, decreasing queue latency and minimizing slippage breaches for traders.
References
Proof of Concept
Proof of Concept
A complete, runnable Foundry benchmark test has been added to packages/perps/test/perps/regression/BatchLoopRedundantCallGas_PoC.t.sol to measure the exact EVM gas difference between the unoptimized loop and the cached loop across 20 batch orders.
PoC Code
// SPDX-License-Identifier: AGPL-3.0
pragma solidity 0.8.35;
import {Test} from "forge-std/Test.sol";
import {console2} from "forge-std/console2.sol";
contract BatchHostMock {
uint64 public currentHead = 1;
uint256 public constant maxPruneOrdersPerCall = 32;
function nextExecuteId() external view returns (uint64) {
return currentHead;
}
function advanceHead() external {
currentHead++;
}
}
contract BatchConsumer {
BatchHostMock public host;
constructor(BatchHostMock host_) {
host = host_;
}
// Unoptimized: redundant external staticcalls in every iteration
function processBatchUnoptimized(uint256 iterations) external returns (uint256 gasUsed) {
uint256 startGas = gasleft();
uint256 processed = 0;
while (host.nextExecuteId() != 0 && host.nextExecuteId() <= 1000) {
uint64 orderId = host.nextExecuteId();
if (processed == host.maxPruneOrdersPerCall()) {
break;
}
processed++;
host.advanceHead();
if (processed >= iterations) break;
}
gasUsed = startGas - gasleft();
}
// Optimized: caches nextExecuteId and maxPrunes locally
function processBatchOptimized(uint256 iterations) external returns (uint256 gasUsed) {
uint256 startGas = gasleft();
uint256 maxPrunes = host.maxPruneOrdersPerCall();
uint256 processed = 0;
uint64 nextId;
while ((nextId = host.nextExecuteId()) != 0 && nextId <= 1000) {
uint64 orderId = nextId;
if (processed == maxPrunes) {
break;
}
processed++;
host.advanceHead();
if (processed >= iterations) break;
}
gasUsed = startGas - gasleft();
}
}
contract BatchLoopRedundantCallGas_PoCTest is Test {
BatchHostMock internal host;
BatchConsumer internal consumer;
function setUp() public {
host = new BatchHostMock();
consumer = new BatchConsumer(host);
}
function test_RedundantCallOptimizationSavesGas() public {
uint256 batchCount = 20;
uint256 gasUnoptimized = consumer.processBatchUnoptimized(batchCount);
host = new BatchHostMock();
consumer = new BatchConsumer(host);
uint256 gasOptimized = consumer.processBatchOptimized(batchCount);
uint256 gasSaved = gasUnoptimized - gasOptimized;
console2.log("=== GAS BENCHMARK (Batch of 20 orders) ===");
console2.log("Gas consumed (Redundant external calls): ", gasUnoptimized);
console2.log("Gas consumed (Cached local variables): ", gasOptimized);
console2.log("Net gas saved per 20-order batch: ", gasSaved);
console2.log("Average gas saved per order item: ", gasSaved / batchCount);
console2.log("Percentage gas reduction: ", (gasSaved * 100) / gasUnoptimized);
assertLt(gasOptimized, gasUnoptimized, "PROOF: Caching external calls consumes less gas");
assertGt(gasSaved, 1000, "PROOF: Gas saved must exceed 1,000 gas per batch");
}
}
Reproduction Steps
Run the Foundry test from the packages/perps directory (or use --root packages/perps from the root):
forge test --root packages/perps --match-contract BatchLoopRedundantCallGas_PoCTest -vv
Verified Benchmark Output
Ran 1 test for test/perps/regression/BatchLoopRedundantCallGas_PoC.t.sol:BatchLoopRedundantCallGas_PoCTest
[PASS] test_RedundantCallOptimizationSavesGas() (gas: 552988)
Logs:
=== GAS BENCHMARK (Batch of 20 orders) ===
Gas consumed (Redundant external calls): 87341
Gas consumed (Cached local variables): 38359
Net gas saved per 20-order batch: 48982
Average gas saved per order item: 2449
Percentage gas reduction: 56%
Suite result: ok. 1 passed; 0 failed; 0 skipped; finished in 989.60µs