0

I'm currently learning about how the function selector is loaded from calldata. The algorithm is like so:

PUSH4 ffffffff
PUSH29 0100000000000000000000000000000000000000000000000000000000000000
PUSH1 00
CALLDATALOAD
DIV
AND

Which results in the selector being loaded from calldata shifted to the right on the stack. This comes to a total of 20 gas. What I don't understand is why SHR isn't just used directly? ie.

PUSH0
CALLDATALOAD
PUSH1 E0 // 224 in hex. 28 bytes to the right
SHR

Which comes to 11 gas, with the same single-value on the stack.

1 Answer 1

1

That is probably due to using non-updated documentation or tutorials, which refers to an old Solidity compiler.

The procedure you mention using SHR has been in place as default for the compiler, with or without optimization enabled, for more than four years.

I did some tests, and I can say [email protected], released on March 5th, 2019, starts to use that procedure, while [email protected] and below use the old version.

Side note. Nothing stops you from using the old selector procedure, but as you mentioned, you don't gain anything from that.

2
  • Thanks, it was indeed that the tutorial I was following was using compiler version 0.4.24. Still, interesting that this was ever the procedure at all.
    – riordant
    Commented Dec 3, 2023 at 23:22
  • @riordant mark it as an answer if this solved your query. thanks:) Commented Dec 4, 2023 at 4:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.