From 7f534602c986213abdf80506d8a57da66d5a55ff Mon Sep 17 00:00:00 2001 From: DevDacian <114125154+devdacian@users.noreply.github.com> Date: Wed, 12 Feb 2025 14:24:21 +0900 Subject: [PATCH] adding modify input example --- README.md | 19 +++++++++ src/14-modify-input/ModifyInputFixed.sol | 24 +++++++++++ src/14-modify-input/ModifyInputUnop.sol | 30 +++++++++++++ test/14-modify-input/ModifyInputTest.sol | 54 ++++++++++++++++++++++++ 4 files changed, 127 insertions(+) create mode 100644 src/14-modify-input/ModifyInputFixed.sol create mode 100644 src/14-modify-input/ModifyInputUnop.sol create mode 100644 test/14-modify-input/ModifyInputTest.sol diff --git a/README.md b/README.md index 8908aeb..bb2b27e 100644 --- a/README.md +++ b/README.md @@ -217,3 +217,22 @@ Again using only the 1 storage slot, this produces the following call stack: ``` In our simplied example using only 1 storage slot the gas cost was 9.45% cheaper with optimizer enabled and 11.86% cheaper without the optimizer. In real-world protocols where multiple storage slots are not changed but frequently read the gas savings are likely to be even greater. + + +### #14 Modify Input Instead Of Temp Variable: EFFECTIVE 0.19% CHEAPER ### +When an input variable's value doesn't need to be preserved, modifying that input variable is more efficient than using a temporary variable inside the function: +```diff +function returnLowest(uint256 input) external view returns(uint256 lowest) { + lowest = input; + + uint256 compsLen = comps.length; + for(uint256 i; i