solidity_mapping_implementation

简介: solidity 中 mapping 是如何存储的为了探测 solidity mapping 如何实现,我构造了一个简单的合约. 先说结论,实际上 mapping的访问成本并不比直接访问storage变量多花费更多的 gas.

solidity 中 mapping 是如何存储的

为了探测 solidity mapping 如何实现,我构造了一个简单的合约. 先说结论,实际上 mapping的访问成本并不比直接访问storage变量多花费更多的 gas.两者几乎差不多.

构造合约

pragma solidity ^0.4.23;

contract TestMap{
     mapping(uint256 => uint256) public channels;
     function TestSet() external{
        channels[0x39]=0x77;
     }
}

合约非常简单,就是写一个 mapping.

汇编指令

最主要是这些指令里面就有一条昂贵的就是 sstore, 至少需要5000gas.

    /* "testmapping.sol":151:155  0x77 */
      0x77
        /* "testmapping.sol":136:144  channels */
      0x0
        /* "testmapping.sol":136:150  channels[0x39] */
      dup1
        /* "testmapping.sol":145:149  0x39 */
      0x39
        /* "testmapping.sol":136:150  channels[0x39] */
      dup2
      mstore
      0x20
      add
      swap1
      dup2
      mstore
      0x20
      add
      0x0
      keccak256
        /* "testmapping.sol":136:155  channels[0x39]=0x77 */
      dup2
      swap1
      sstore
      pop

总结

由于指令的成本较低,写 storage 最少需要5000gas, 而 sha3只需要30+gas, 可以忽略不计,其他指令也很便宜.
当然如果是读的话,就稍微贵一点点,读 storage 是200gas, 那么 sha3加上这些指令,估计就有接近100了.
不过如果mapping 确实可以带来便利,那就用 mapping 吧.

目录
相关文章
|
Java API Android开发
Gradle 依赖关系中 compile和 implementation的区别
将在一个项目中展示implementation,api以及compile之间的差异。 假设我有一个包含三个Gradle模块的项目: • app(Android应用) • my-android-library(Android库) • my-java-library(Java库) app具有my-android-library与依赖。my-android-library具有my-java-library依赖。
576 0
|
4月前
|
算法 项目管理 开发者
【Conan 入门教程 】深入解析Conan中的依赖关系的定义方法(In-depth Analysis of Dependency Definition Methods in Conan)
【Conan 入门教程 】深入解析Conan中的依赖关系的定义方法(In-depth Analysis of Dependency Definition Methods in Conan)
216 0
|
10月前
|
编译器
[√]添加预处理的2个不同方式:target_compile_definitions / add_definitions
[√]添加预处理的2个不同方式:target_compile_definitions / add_definitions
169 0
Undefined symbols for architecture x86_64: "_OBJC_CLASS_$_ZMCertification", referenced from:解决方法
Undefined symbols for architecture x86_64: "_OBJC_CLASS_$_ZMCertification", referenced from:解决方法
192 0
filter operation implementation in Gateway framework
Created by Wang, Jerry, last modified on Dec 28, 2015 data type is singleton field type and actual value must be compatible
filter operation implementation in Gateway framework
MPC class get last modified - how to implement
Created by Wang, Jerry, last modified on Jan 06, 2015
MPC class get last modified - how to implement
implementation of CreateBindingContext
Created by Jerry Wang, last modified on Jul 29, 2015
131 0
implementation of CreateBindingContext
UI5 control inheritance implementation question
UI5 control inheritance implementation question
UI5 control inheritance implementation question
filter operation implementation in SAP Gateway framework
Created by Wang, Jerry, last modified on Dec 30, 2015
109 0
filter operation implementation in SAP Gateway framework
How to create ABAP implicit enhancement implementation
How to create ABAP implicit enhancement implementation
130 0
How to create ABAP implicit enhancement implementation