{"content":{"title":"Gas优化学习笔记2","body":"接上篇 [gas优化1](https://learnblockchain.cn/article/6844)\r\n\r\n\r\n\r\ndefi上有一类很火的项目，就是借贷，如Aave，Compound。\r\n\r\n用户任何一个简单的操作，合约都要进行各种if判断, 比如：\r\n\r\n1. 是否已经计算过利息\r\n2. 上次更新的区块是否就是当前区块\r\n3. 你的总额度是否透支\r\n\r\n\r\n\r\n很多逻辑的执行，都需要多个条件，这里的代码需要考虑gas吗？\r\n\r\n\r\n\r\n上篇的lowGas和highCas，我做一下简单的修改。\r\n\r\n返回类型：由uint改为bool\r\n\r\n返回：之前返回x，改为 x是否等于value的布尔值\r\n\r\n```\r\n    function lowGas(uint value) costGas('lowGas') public view returns(bool) {\r\n        uint x=10;\r\n        x = x + x;\r\n        \r\n        return x == value;\r\n    }\r\n\r\n    function highGas(uint value)  costGas('highGas') public view returns(bool)  {\r\n        uint x=10;\r\n        x = x * 2;\r\n        return x == value;\r\n    }\r\n```\r\n\r\n增加一个方法\r\n\r\n```\r\n    function fn_or(uint value) costGas('fn_or') public view returns(bool){\r\n        return lowGas(value) || highGas(value);\r\n    }\r\n```\r\n\r\nfn_or: 对两个函数进行or运算\r\n\r\n\r\n\r\n\r\n\r\n代码很简单，看下运行效果。\r\n\r\n注意，代码进行了编译优化，run=200\r\n\r\n\r\n\r\nfn_or,  入参=10\r\n\r\n```\r\nCALL\r\n[call]from: 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4to: Gas2.fn_or(uint256)data: 0x33b...0000a\r\nconsole.log:\r\nlowGas costGas: 100\r\nhighGas costGas: 116\r\nfn_or costGas: 7416\r\n```\r\n\r\nfn_or,  入参=20\r\n\r\n```\r\nCALL\r\n[call]from: 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4to: Gas2.fn_or(uint256)data: 0x33b...00014\r\nconsole.log:\r\nlowGas costGas: 100\r\nfn_or costGas: 4964\r\n```\r\n\r\n\r\n\r\n对比下两次gas。\r\n\r\n输入10， \r\n\r\nlowGas返回false，highGas返回false\r\n\r\nlowGas执行了，gas fee：100\r\n\r\nhighGas执行了，gas fee：116\r\n\r\n总gas fee：7416\r\n\r\n\r\n\r\n输入20\r\n\r\nlowGas返回true，\r\n\r\nlowGas执行了，gas fee：100\r\n\r\nhighGas根本没执行\r\n\r\n总 gas fee：4964\r\n\r\n\r\n\r\nlowGas返回false，没啥影响。\r\n\r\nlowGas返回true，会把highGas都节省掉。\r\n\r\n\r\n\r\n结论：如果两个函数作为条件判断有一个成立即可，那么gas少的那个放在前面！！！\r\n\r\n\r\n\r\n\r\n\r\n再新增一个函数\r\n\r\n```\r\n    function fn_add(uint value) costGas('fn_and') public view returns(bool){\r\n        return lowGas(value) && highGas(value);\r\n    }\r\n```\r\n\r\nfn_and:对两个函数进行and运算\r\n\r\n\r\n\r\nfn_and,  入参=20\r\n\r\n```\r\nCALL\r\n[call]from: 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4to: Gas2.fn_add(uint256)data: 0x81e...00014\r\nconsole.log:\r\nlowGas costGas: 100\r\nhighGas costGas: 116\r\nfn_and costGas: 7419\r\n```\r\n\r\nfn_and,  入参=10\r\n\r\n```\r\nCALL\r\n[call]from: 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4to: Gas2.fn_add(uint256)data: 0x81e...0000a\r\nconsole.log:\r\nlowGas costGas: 100\r\nfn_and costGas: 4967\r\n```\r\n\r\n\r\n\r\n对比下两次gas。\r\n\r\n输入20， \r\n\r\nlowGas返回true，highGas返回true\r\n\r\nlowGas执行了，gas fee：100\r\n\r\nhighGas执行了，gas fee：116\r\n\r\n总gas fee：7419\r\n\r\n\r\n\r\n输入10\r\n\r\nlowGas返回false，\r\n\r\nlowGas执行了，gas fee：100\r\n\r\nhighGas根本没执行\r\n\r\n总 gas fee：4967\r\n\r\n\r\n\r\nlowGas返回true，没啥影响。\r\n\r\nlowGas返回false，会把highGas都节省掉。\r\n\r\n\r\n\r\n结论：如果两个函数作为条件判断必须两个都成立即可，那么gas少的那个放在前面！！！"},"author":{"user":"https://learnblockchain.cn/people/3908","address":null},"history":null,"timestamp":1701327236,"version":1}