<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Zhichao Li on Medium]]></title>
        <description><![CDATA[Stories by Zhichao Li on Medium]]></description>
        <link>https://medium.com/@Zhichao_Li?source=rss-6fc96326de98------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Stories by Zhichao Li on Medium</title>
            <link>https://medium.com/@Zhichao_Li?source=rss-6fc96326de98------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 21:13:38 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@Zhichao_Li/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Techniques to Cut Gas Costs for Your Dapps]]></title>
            <link>https://medium.com/coinmonks/techniques-to-cut-gas-costs-for-your-dapps-7e8628c56fc9?source=rss-6fc96326de98------2</link>
            <guid isPermaLink="false">https://medium.com/p/7e8628c56fc9</guid>
            <category><![CDATA[smart-contracts]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[ethereum]]></category>
            <dc:creator><![CDATA[Zhichao Li]]></dc:creator>
            <pubDate>Fri, 15 Jun 2018 00:08:12 GMT</pubDate>
            <atom:updated>2023-12-24T18:08:13.214Z</atom:updated>
            <content:encoded><![CDATA[<p>Gas costs play a significant role in the long run for each Ethereum-based Dapp. We share several hands-on techniques, by example, to help your Dapps cut the associated gas costs. We would love to hear your feedback if any.</p><p><strong>Terms</strong> we use in the article:</p><ul><li>SC: Smart Contract</li><li>GC: Gas Cost</li><li>SOLC: Solidity Compiler</li><li>EVM: Ethereum Virtual Machine</li></ul><p><strong>Overall Approach</strong>: <em>Feedback-based solution.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*P0pOaDEtSv9LdeQjcoGaaA.png" /></figure><p>We tried to optimize the design and implementation of our SC the way it is expected from SOLC and EVM. We verified the ideas by performing GC regression tests and apply the feedback back to the design and implementation.</p><p><strong>Techniques</strong> we used to cut gas costs (<em>note that we use SOLC version 0.4.21 for discussion purpose)</em>:</p><ul><li><strong><em>Bit-compaction</em></strong></li></ul><p>Bit-compaction format for external function parameters helps cut gas costs because it minimizes the amount of input data sent to the Ethereum blockchain. Please note that it introduces certain small extra gas costs to unpack the data. However, the savings usually weigh out the extra costs.</p><p>Take the following code snippet for example:</p><pre><em>pragma solidity ^0.4.21;</em></pre><pre><em>contract bitCompaction {<br>  function oldExam(uint64 a, uint64 b, uint64 c, uint64 d) public {<br>  }<br>  function newExam(uint256 packed) public {<br>  }<br>}</em></pre><p>Its corresponding assembly code shows that the oldExam function incurs 4 `<em>CALLDATALOAD`</em> operations once it is called, and each `CALLDATALOAD` operation triggers one memory allocation operation in Ethereum, while the newExam function incurs only 1 instead. Note that the assembly code is too large for us to present here, but one can easily generate it by performing (for example) `solc — asm — optimize — optimize-runs 200 bitCompaction.sol`.</p><p>In the experiment, we have the following call data for each function:</p><pre>oldExam call data: ([]uint8) (len=132 cap=132) {<br>00000000  3e f2 62 fd 00 00 00 00  00 00 00 00 00 00 00 00<br>00000010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00<br>00000020  00 00 00 01 00 00 00 00  00 00 00 00 00 00 00 00<br>00000030  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00<br>00000040  00 00 00 01 00 00 00 00  00 00 00 00 00 00 00 00<br>00000050  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00<br>00000060  00 00 00 01 00 00 00 00  00 00 00 00 00 00 00 00<br>00000070  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00<br>00000080  00 00 00 01<br>}</pre><pre>newExam call data: ([]uint8) (len=36 cap=36) {<br>00000000  83 ba 6e 5a 00 00 00 00  00 00 00 01 00 00 00 00<br>00000010  00 00 00 01 00 00 00 00  00 00 00 01 00 00 00 00<br>00000020  00 00 00 01<br>}</pre><p>As we can see, those uint64 parameters in the oldExam function are first converted to uint256 ones once being called. Gas costs for the above two are <strong>22235</strong> and <strong>21816</strong> respectively, meaning the newExam function saves <strong>419</strong> gas costs. The more scattered the parameters are, the more savings you cut by adopting the bit compaction method.</p><ul><li><strong><em>Batching</em></strong></li></ul><p>Batching cuts gas cost because it reduces common data processing. Take the following code snippet for example:</p><pre><em>Old: func once(uint256 header, uint256 val...) x N<br>New: func batch(uint256 header, uint256[] val... x N)</em></pre><p>Executing the old function N times processes the common `header` field N times, as well as calling the function N times. However, by batching, the function is called only one time and the common `header` field is processed only one time. Therefore, it saves gas costs by reducing `CALLDATALOAD`, memory allocation, and function call operations. The larger the N, the larger the savings by adopting the batching mechanism.</p><ul><li><strong><em>Separating writes to storage struct</em></strong></li></ul><p>Separating writes to struct object that is defined in storage, cuts gas cost in many cases. Take the following code snippet for example:</p><pre><em>pragma solidity ^0.4.21;</em></pre><pre><em>contract structWrite {<br>  struct Object {<br>    uint64 v1;<br>    uint64 v2;<br>    uint64 v3;<br>    uint64 v4;<br>  }</em></pre><pre><em>  Object obj;</em></pre><pre><em>  function StructWrite() public {<br>    obj.v1 = 1;<br>    obj.v2 = 1;<br>    obj.v3 = 1;<br>    obj.v4 = 1;<br>  }</em></pre><pre><em>  function oldExam(uint64 a, uint64 b) public {<br>    uint a0; uint a1; uint a2; uint a3; uint a4; uint a5; uint a6; <br>    uint b0; uint b1; uint b2; uint b3; uint b4; uint b5;</em></pre><pre><em>    obj.v1 = a + b;<br>    obj.v2 = a - b;<br>    obj.v3 = a * b;<br>    obj.v4 = a / (b + 1);<br>  }</em></pre><pre><em>  function setObject(uint64 v1, uint64 v2, uint64 v3, uint64 v4) private {<br>    obj.v1 = v1;<br>    obj.v2 = v2;<br>    obj.v3 = v3;<br>    obj.v4 = v4;<br>  }</em></pre><pre><em>  function newExam(uint64 a, uint64 b) public {<br>    uint a0; uint a1; uint a2; uint a3; uint a4;<br>    uint b0; uint b1; uint b2; uint b3;<br>    setObject(a + b, a - b, a * b, a / (b + 1));<br>  }<br>}</em></pre><p>Once the optimize option is turned on, for the above example, SOLC compiles storage struct writes in such a way that the oldExam function would incur 1 EVM `SSTORE` operation (`SSTORE` is the most gas costly operation) for each of the struct field in Object since the current implementation of SOLC could not optimize the `SSTORE` operations when there is not enough stack space (there is stack space for only 16 local variables). However, with the new approach, it has ample stack space such that the current compiler can optimize the struct accesses by performing, in total, only 1 `SSTORE` operation after merging writes to the struct. This can be easily verified against by looking at the corresponding assembly code. This is restricted to the current implementation of SOLC, and is subject to change in the future. For the above example, the gas costs are <strong>58140</strong> and <strong>27318</strong> respectively, indicating a gas cost saving of <strong>30822</strong> for the newExam function. When the stack space is not enough, the more fields the struct has, the more gas cut you achieve if the new approach is leveraged.</p><ul><li><strong><em>uint256 and direct memory access</em></strong></li></ul><p>Calculation unit in SOLC is uint256. Hence other types (e.g., uint8) require type conversions before calculations are applied. This incurs extra gas costs. Moreover, direct memory access is more GC-efficient than direct storage access, and more GC-efficient than struct pointer based access. These smaller tricks can be represented as:</p><pre><em>uint8 data;                  </em><strong><em>=&gt;  </em></strong><em> uint256 data;</em></pre><pre><em>uint256 val = storageData;   =&gt;   uint256 memoryData = storageData;<br>(N Times)                         uint256 val = memoryData;</em></pre><pre><em>uint64 val = obj.v1;         </em><strong><em>=&gt;  </em></strong><em> uint64 val = val1;</em></pre><ul><li><strong><em>Assembly optimization</em></strong></li></ul><p>When compiling your SOLC code, make sure to perform GC experiments with the SOLC `optimize — runs` option to figure out the best GC-efficient assembly code to run in EVM.</p><h3><strong>Citation</strong></h3><pre>@article{li2018gassaving,<br>  title   = &quot;Techniques to Cut Gas Costs for Your Dapps&quot;&quot;,<br>  author  = &quot;Li, Zhichao&quot;,<br>  journal = &quot;medium.com&quot;,<br>  year    = &quot;2018&quot;,<br>  month   = &quot;June&quot;,<br>  url     = &quot;https://medium.com/coinmonks/techniques-to-cut-gas-costs-for-your-dapps-7e8628c56fc9&quot;<br>}<br><br>Or:<br><br>Li, Zhichao. (Jun 2018). &quot;Techniques to Cut Gas Costs for Your Dapps&quot;. Medium.com.<br>https://medium.com/coinmonks/techniques-to-cut-gas-costs-for-your-dapps-7e8628c56fc9.</pre><h3>Reference</h3><ul><li>Gas Costs from Yellow Paper. <a href="https://docs.google.com/spreadsheets/d/1n6mRqkBz3iWcOlRem_mO09GtSKEKrAsfO7Frgx18pNU/edit#gid=0"><strong><em>Link</em></strong></a>.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7e8628c56fc9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/coinmonks/techniques-to-cut-gas-costs-for-your-dapps-7e8628c56fc9">Techniques to Cut Gas Costs for Your Dapps</a> was originally published in <a href="https://medium.com/coinmonks">Coinmonks</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>