Omniscia Gnosis Guild Audit
Consumptions Code Style Findings
Consumptions Code Style Findings
CSN-01C: Inefficient Lookup Operation of Consumptions
Type | Severity | Location |
---|---|---|
Gas Optimization | ![]() | Consumptions.sol:L64 |
Description:
The Consumptions::find
function that is invoked within Consumptions::merge
is invoked so with a result
array that contains multiple zeroed out entries that are iterated redundantly.
Example:
packages/evm/contracts/Consumptions.sol
46function merge(47 Consumption[] memory c1,48 Consumption[] memory c249) internal pure returns (Consumption[] memory result) {50 if (c1.length == 0) return c2;51 if (c2.length == 0) return c1;52
53 result = new Consumption[](c1.length + c2.length);54
55 uint256 length = c1.length;56 unchecked {57 for (uint256 i; i < length; ++i) {58 result[i].allowanceKey = c1[i].allowanceKey;59 result[i].balance = c1[i].balance;60 result[i].consumed = c1[i].consumed;61 }62
63 for (uint256 i; i < c2.length; ++i) {64 (uint256 index, bool found) = find(result, c2[i].allowanceKey);65 if (found) {66 result[index].consumed += c2[i].consumed;67 } else {68 result[length].allowanceKey = c2[i].allowanceKey;69 result[length].balance = c2[i].balance;70 result[length].consumed = c2[i].consumed;71 length++;72 }73 }74 }
Recommendation:
As the result
array contains all c1
elements at the same indexes, the Consumptions::find
function could accept the c1
array as an input and use the same index to operate on the result
array.
To note, the contract itself assumes that consumptions
do not contain duplicate allowanceKey
entries by themselves rendering this approach possible.
Alleviation:
The input of the referenced Consumptions::find
function invocation statement has been adjusted to the c1
array as advised, optimizing the lookup operation significantly and thus alleviating this exhibit.