The onlyBeneficiaryWithIsInherited
modifier inefficiently reads the beneficiaries.length
from storage in each loop iteration, resulting in unnecessary gas consumption
In the current implementation, beneficiaries.length
is read from storage in every iteration of the while loop:
Reading from storage costs 100 gas per read. Since this length check happens in every loop iteration, the gas cost increases linearly with the number of beneficiaries that need to be checked before finding a match.
Increased gas costs for functions using this modifier
The gas cost grows linearly with the position of the caller in the beneficiaries array
Unnecessary storage reads that could be optimized
Before any improvements:
After loop is optimised, gas cost is smaller in every test scenario:
Foundry test and manual code review
Cache the array length in memory at the beginning of the modifier to avoid repeated storage reads:
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.