Reducing EOS contract deployment RAM footprint
Doing smart contract work on EOS is currently a bit expensive due to RAM prices. The state of a contract, along with the code and ABI are stored in a memory based database. That memory you have to buy, and it is as I am writing this at about $0.7 / kbyte.
Even if you write a contract that does all data storage on user’s expense you still need to pay for the deployment of the contract.
So let’s say that you have a basic contract that you use eosio.cdt to build like this:
eosio-cpp -o player/player.wasm player/player.cpp
In this example case, this results in a .wasm file of about 42kbytes. So to deploy this you might expect to have to pay 42*$0.7=$29,4
Well, it turns out that the EOS software in order to accommodate for the VM overhead, etc it needs to allocate ten times that. So you should expect to need about 420kbytes of RAM in order to deploy that 42kbytes contract! You now need almost $300 to deploy this.
So with a multiplier of ten on the size some size optimizations might come handy. We can achieve that by specifying some extra parameters on the command line:
eosio-cpp -Os -finline-hint-functions -fno-elide-constructor -o player/player.wasm player/player.cpp
This way we instruct the compiler to optimize for executable size, to inline functions only when the programmer specifically hinted so, and to disable a specific copy constructor optimization.
The resulting wasm file is now 37kbytes so the cost will be $259. That is $41 saved that you can spend on coffee to fuel your development.
The benefits of about 15% that we saw here will vary of course. The type of code you write, the amount of libraries you use, etc can make this go up significantly or down. We should also note that these kind of optimizations will result in a slower contract, but unless you do something extremely computationally intensive this will not be noticable.