28 votes

How does the solidity optimizer work?

The optimizer seems like magic even to me. chriseth (He is the main DEV developer of solidity) would probably be in a better position to answer your question than myself. Here is an interesting high ...
user avatar
  • 5,848
12 votes
Accepted

Any reason not to use Browser Solidity's "Enable Optimization"?

The downside is that optimization adds extra complexity, and more complexity means more bugs. See this post for a previous bug in the optimizer: https://blog.ethereum.org/2017/05/03/solidity-optimizer-...
user avatar
  • 16.4k
8 votes
Accepted

Do intermediate memory variables cost gas?

Here is more information about storing data in solidity https://solidity.readthedocs.io/en/latest/introduction-to-smart-contracts.html#storage-memory-and-the-stack Those variables, which you defined,...
user avatar
  • 1,712
8 votes
Accepted

Enabling truffle compile optimizer does not change the deployment gas cost

Obviously it is really simple: The optimizer settings are in the wrong place. I used the example provided here but according to the documentation it needs to be under settings:{...} in the solc config....
user avatar
  • 507
7 votes
Accepted

Are there any best practices for optimizing computations?

Given the high cost of blockchain-based computation, it is definitely worthwhile to performance optimise the computations performed in the chain itself. One basic technique used by almost all ...
user avatar
6 votes

Are there any best practices for optimizing computations?

There are a few techniques which can be used depending on what you're optimizing for: Deploy libraries Enable compiler optimizations Network-specific tips & tricks Some contracts are pre-...
user avatar
5 votes
Accepted

Binary search in Solidity arrays

No, all the collection data types are very basic. This may be considered a design decision to keep the execution very cheap and will therefore not change in the future. To my knowledge, no library for ...
user avatar
5 votes

Do intermediate memory variables cost gas?

It depends on the settings used when compiling the Solidity. https://solidity.readthedocs.io/en/v0.4.25/using-the-compiler.html As tested by @Aquila, compiling with the optimizer leads to the same ...
user avatar
  • 78.8k
5 votes
Accepted

SLOAD costs 800 gas - how is it possible?

EIP-1884 changed the cost of an SLOAD from 200 to 800, which explains why you are seeing exactly 600 more than expected. It looks like the yellow paper has not been updated to reflect this change. ...
user avatar
4 votes
Accepted

Why is the Eternal Storage approach still used if it hugely increases gas usage compared to storing data in structs?

They're not mutually exclusive concepts. That code snippet originates here, to the best of my knowledge: https://blog.colony.io/writing-upgradeable-contracts-in-solidity-6743f0eecc88/ The gist of ...
user avatar
  • 51.9k
4 votes
Accepted

Loop Optimisation for gas usage

You could start by using a uint type for the loop variable rather than uint8. This is not obvious, but the compiler introduces extra code to do type-coercion when you use types smaller than 256-bits. ...
user avatar
  • 8,732
4 votes
Accepted

Is it reasonable to use uint128 or less to save space?

In storage, variables are packed tightly, so your struct will fit in one word. See this page of the docs: http://solidity.readthedocs.io/en/latest/miscellaneous.html For parameters, packing's less ...
user avatar
4 votes

Binary search in Solidity arrays

There's an implementation of binary search for sorted integer arrays, here. Note that this is look-up only, and you'll have to change the handled data type/structure from int to whatever your ...
user avatar
4 votes
Accepted

Does Solidity pre-compute the powers of two defined in the body of functions?

Yup, it does cache the numbers, even without the optimizer enabled! When passing 65535 (max uint16) as the parameter x, it costs 1003 gas to run either foo or bar. I ran this test with Solidity v0.8.3 ...
user avatar
3 votes

Binary search in Solidity arrays

OpenZeppelin wrote a binary search algorithm in Solidity v0.8. Here's their implementation (code taken from here): function findUpperBound(uint256[] storage array, uint256 element) internal view ...
user avatar
3 votes

Why do reputable ERC721 contracts use uint256 so often?

Edited Answer The reason people like using uint256 for most calculation is that OpenZeppelin has a SafeMath library for uint256 that prevent number overflow for sol version < 0.8. Additional ...
user avatar
3 votes

Why does a constant function that reads a struct memory cost the same as accessing the struct member directly?

Because during compilation these 2 options will be represented as equal bytecode, because makes same logic. Compiler see that function getOwner get ID and extract owner from mapping, same as '...
user avatar
  • 156
2 votes

Binary search in Solidity arrays

I've created an Order Statistics Tree that resolves a number of problems such as finding the median or rank of a value in a sorted list while also providing a method to cap gas cost to an absolute ...
user avatar
  • 51.9k
2 votes
Accepted

What optimizations does Geth's --jitvm do?

In the time since this question was asked the following post has been made on the official Ethereum blog: Go Ethereum’s JIT-EVM This helps to answer: What optimizations does Geth's --jitvm do? ...
user avatar
2 votes
Accepted

Does an Ethereum client need to download the entire state of a contract in order to use it?

The same "light client" techniques that work for getting ether balances works fine for getting data from a smart contract's storage. See https://github.com/ethereum/wiki/wiki/Light-client-protocol for ...
user avatar
  • 26.7k
2 votes
Accepted

Difference between unoptimized and optimized solidity code

Does the unoptimized compiler, when compiling above code, call SSTORE twice or once? Yes, the unoptimised code will call SSTORE twice. However, the second SSTORE to the same storage slot will only ...
user avatar
  • 7,194
2 votes
Accepted

Loop optimization checking coordinates in a grid

You can pack all bools in a row into a single uint and save on storage updates: pragma solidity 0.4.19; contract Test { mapping (uint => uint) public grid; function check(uint x, uint y,...
user avatar
  • 10.4k
2 votes

Why is the Eternal Storage approach still used if it hugely increases gas usage compared to storing data in structs?

When storing data into a Solidity mapping, there is no cost for storing the key. The key is not actually stored anywhere. The sha3 hash of the key is used as the storage address for the value. For ...
user avatar
  • 9,167
2 votes

Why are timestamps stored in a uint (256 bit) when they would fit in a uint64?

A storage slot is 256 bits, as are hashes, ETH amounts etc, so it's simpler to standardize on returning a 256-bit number. However, if you need a timestamp in your contract you are under no ...
user avatar
  • 16.4k
2 votes

Solidity optimizer vs LLVM optimizer

Regardless of how a contract is executed, it's the EVM's accounting that matters. The protocol doesn't know if you're executing a contract on x86, a ARM7, a 6502, or pen and paper. What it does know ...
user avatar
2 votes

Deleting index of an array in terms of optimization for looping

Seconding Edmond's concerns about the loops. In my opinion, the need to enumerate the list is questionable. The contract could work on an append-only basis with a simple flag to indicate "sold" and ...
user avatar
  • 51.9k
2 votes
Accepted

Where can I learn to read a flow of opcodes and learn how to debug transactions?

The code in Ethereum is executed on the Ethereum Virtual Machine which is a stack-machine which executes instructions. A call to any contract starts with the instruction pointer at 0 meaning that the ...
user avatar
2 votes

How does USDC manage to only cost 25k gas instead of 35k++ for transfer?

It totally depends on how the transfer function is implemented and what it does. I ran some tests with a bare-bones ERC20 contract. Or, actually it doesn't anymore implement all needed functions but ...
user avatar
2 votes
Accepted

Using assembly to store more values per word

You don't need assembly, you can do it with Solidity. Be careful, sometimes it is better to use uint256 anyway because of conversion costs and more complexity, which generate more costs than the ...
user avatar
2 votes
Accepted

Is type casting address vars to contract types more expensive than defining vars as contract types? And why?

From both options above, what would be the more gas-efficient option for accessing and interacting with the token contract? And why? They are exactly the same. There is no difference between address ...
user avatar
  • 5,919

Only top scored, non community-wiki answers of a minimum length are eligible