紧凑的变量打包对于常量变量有影响吗?

huangapple go评论53阅读模式
英文:

Does Tight Variable Packing Matter for Constant Variables?

问题

uint8 public constant zero = 0;
uint8 public constant treeHeight = 32;
uint64 public constant treeWidth = uint64(2 ** treeHeight);
uint public leafCount; // the number of leaves currently in the tree

我正在尝试优化 Gas。一直在阅读有关紧凑变量打包的信息,想知道它是否也适用于常量,或者我是在浪费时间尝试优化这些常量的存储槽/访问。

我发现将变量大小缩小到 uint256 以下会使合同创建成本更高,只有在我利用所有变量现在都在一个槽中访问的事实时才会产生回报。

然而,我不知道这对于常量是否有影响,是否应该将所有常量都设置为 uint256。

任何帮助/建议都会受到欢迎 紧凑的变量打包对于常量变量有影响吗?

英文:
uint8 public constant zero = 0;
uint8 public constant treeHeight = 32;
uint64 public constant treeWidth = uint64(2 ** treeHeight);
uint public leafCount; // the number of leaves currently in the tree

I am trying to optimize for Gas. Keep reading about tight variable packing and was wondering if it applies to constants as well or if I am wasting my time trying to optimize the storage slots/access for these constants.

I am finding that making the vars smaller than uint256 makes contract creation much more expensive, which only pays off if I take advantage of the fact that all the vars are now in one slot when accessing.

Then again I don't know if any of this matters for constants and if I should just have all my constants be uint256.

Any help/advice appreciated 紧凑的变量打包对于常量变量有影响吗?

答案1

得分: 1

对于常量变量,打包不重要。

常量变量不存储在存储中,而是在EVM字节码中内联使用操作码。字节码打包始终是紧凑的,不受存储的256位填充规则的影响。

我假设对于大多数常量情况,会使用“PUSHXXX”操作码。但我没有确认这一点。

更多信息在这里

EVM操作码

英文:

Packing does not matter for constant variables.

Constant variables are not in storage but inlined in the EVM bytecode with opcodes. Bytecode packing is always tight-like and not subject to the same 256-bit padding rules as storage.

I assume for most of cases of constants, PUSHXXX opcodes are used. However I did not confirm this.

More here.

EVM opcodes.

huangapple
  • 本文由 发表于 2023年7月12日 23:57:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76672454.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定