The task is to optimise a smart contract by applying various gas optimisation techniques.
-
Fixed Size Array
: Instead of using a dynamic array (uint[]
), a fixed-size array (uint[10]
) was used. Fixed-size arrays are more gas-efficient because Solidity does not need to perform dynamic memory allocation. -
Caching State Variable
: The length of thenumbers
array was cached in a local variable (arrayLength
). This avoids redundant/repeated storage reads inside the loop, reducing gas consumption. -
Unchecked Block
: Thefor loop
was wrapped in anunchecked
block. This allows skipping certain checks like integer overflow. Since we know the loop range is safe, we can use theunchecked
block to optimize gas consumption. -
For Loop Increment Syntax
: The for loop increment syntax is changed to++i
to reduce gas consumption.