英文:
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”操作码。但我没有确认这一点。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论