Solidity vs Assembly – Using delegatecall for Optimized Smart Contracts

assemblydelegatecallsolidity

Is there a real difference nowadays, performance-wise, between using assembly delegatecall and the high-level solidity version?

I know that in the past assembly was used so you could get the returned value of the call, but that's no longer case since the high-level delegatecall already provides that in the newer solidity versions.

Best Answer

If by "performance-wise" you mean gas optimization, then yes, I believe there are some that are actually used in the context of proxy contracts such as OpenZeppelin's implementation for example as after the delegatecall they either return or revert.

The memory gas cost computation is explained in this answer, "allocating" a new byte array at the free memory pointer location (which pure solidity would do) could lead to a non-zero memory expansion cost. While it might not matter that much (if at all) for a single contract, if your implementation is to be used by thousands of developers, you might as well take full memory control and try to avoid unnecessary potential extra cost by using the lowest possible offset (0) as in the implementation linked above both for sent and received data.

However, doing this will have unwanted effects on the solidity generated bytecode following your delegatecall as it overwrites the solidity reserved memory slots. But since in that use case, there should be no logic following the delegatecall, it's perfectly fine to do it.

For other use cases, I must say that I don't see any relevant advantages of assembly over plain solidity...

I hope that answers your question.

Related Topic