Solidity: How could I decrease gas usage of complex structs?

by alper   Last Updated November 13, 2018 08:28 AM

Struct is very expensive as labelled here: It seems extremely expensive when storing struct type.

https://www.cryptocompare.com/coins/guides/what-is-the-gas-in-ethereum/:

sstore 100 Paid for a normal SSTORE operation (doubled or waived sometimes).

[Q] What decised to double it?

For example I have following struct defined inside a library: Following operation inside insert() nearly cost 150,000 gas. Storing string, uint and byte under the struct.

Lib.sol

library Lib {
  struct job {
    string      hash;  // string hash_index_folderType;
    uint       index;  //
    byte  folderType;  //
  }
}

Example.sol

import "./Lib.sol"
contract Example{
   using Lib for Lib.data;
   function insert( string key, byte folderType ){
     uint currentIndex = cluster.jobStatus[jobKey].length;
     cluster.submittedJobs.push( Lib.job({hash: key, index: 
   currentIndex, folderType: folderType }) );
  }
}

I know that storing data is the most expensive operation in an smart contract.

But as we know ether keep increasing(jumped from 50$ to 240$ which means memory cost increased 5 times out of air) and storage price keep increases rapidly for clients as well, since mining price does not decrease. So I am in search for most compact and efficient way to use memory.

=> While ether was increasing why won't gas price does not decrease for memory?

[Q] My question is related to instead of storing struct inside library and if I store it under the same file with the insert() exists, does it decrease the gas usage? If not: is it good idea to combine all variables by appending them into a string and store them as single string variable instead of a struct, might it help to decrease the gas usage?

As an example:

import "github.com/Arachnid/solidity-stringutils/strings.sol";
string hash_index_folderType = key.toSlice().concat(currentIndex.toSlice().concat(folderType.toSlice());


Answers 1


  1. Gas cost of operation may depend on its parameters. For example writing in a storage some nonzero data costs different amount of gas than writing zero data, moreover if you empty nonzero data you would be even refunded with gas.
    Up-to-date gas prices.
    By the way, storing variable length string in a struct is not the best idea.
  2. It seems the reason is that everyone using default gas price suggested by ethereum clients like mist or parity, so the "market" of transactions is overpopulated with high gas price transactions.
    The sole purpose of gas price is to measure miner's cost to process your transaction, so it's completely determined by miners. Today the biggest part of miner's bounty comes from block reward (5 ETH) and miners don't really care about transaction fees. Due to reward, mining would be economically profitable even if gas price is set to 0. And there is no real connection between ETH/USD price and transaction operation costs. Therefore it's highly unlikely that the price will lower until adoption of proof of stake consensus algorithm or change in how clients' suggested price is calculated.
randomlogin
randomlogin
June 01, 2017 15:16 PM

Related Questions


Hidden information for game contract

Updated September 29, 2017 04:28 AM



Issue in insertion to an array inside a struct

Updated September 28, 2017 06:28 AM