solidity smart contract bytes maximum and reduced the number of bytes

The maximum number of bytes smart contract

In the Solidity, EIP 170 to contract the maximum size is limited to 24 KB. Therefore, if the smart contract terms too much will lead to not publish operation.

The method of reducing the number of compressed bytes

Method and variable naming

To a certain extent, method or variable length will affect how much the number of bytes, and therefore can be compressed by reducing the contents of name or variable name, but the effect is generally small, after all method and variable names themselves footprint limited.

Reduce variable definitions members

If a value is used only once or twice, then minimize and then create a separate variable, the more variable, compiled more Number of Bytes, this can reduce the size of the lot compiled.

Variable initialization

Variables explicitly initialized only consumes Gas, will increase the compiled byte code, it is possible to use the default value that is the default value.

Avoid duplication check

Some values ​​in the business around and do the appropriate checks, and there is no need to repeat the examination, such code to reduce the number of bytes compiled effectively reduced.

Define constants

1e18 such values ​​such as, multiple use, must define constants, write directly cause the compiled byte code becomes long, this way can effectively reduce the number of byte codes.

Reduce public property

Calling methods provides the corresponding default attributes after compiling public therefore unnecessary to set the interior of the public property, the method can be reduced corresponding to bytes.

Use short string of reasons

Revert or other reason require simplified description string.

Single-line exchange

Solidity variable value can be exchanged in a single statement. Thereby avoiding temporary definition of intermediate variables.

(result[i], result[j]) = (result[j], result[i])

Typical applications of the above scenario is written bubble sort, the position needs to be exchanged when two values ​​in the array, can be obtained by writing the above, the definition of intermediate variables reduced.

Using the event with mention storage

Some data do not need to be stored on the block chain, waste of resources, information can be logged by the event.

Using the library to save some of the bytecode

Public library function will not be part of a smart contract, so you can calculate the number of write operations to a public library inside, and then make the appropriate access by authorized.

Original link: https://www.choupangxia.com/2019/08/06/solidity%e6%99%ba%e8%83%bd%e5%90%88%e7%ba%a6%e5%ad%97 % e8% 8a% 82% e6 % 95% b0% e6% 9c% 80% e5% a4% a7% e5% 80% bc% e5% 8f% 8a% e7% bc% a9% e5% 87% 8f% e5 % ad% 97% e8% 8a % 82% e6% 95% b0 /

Guess you like

Origin www.cnblogs.com/secbro/p/11311521.html