Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

calldataViewFunctions may encourage gas inefficiency #14

Open
CodeSandwich opened this issue Jan 26, 2023 · 7 comments
Open

calldataViewFunctions may encourage gas inefficiency #14

CodeSandwich opened this issue Jan 26, 2023 · 7 comments

Comments

@CodeSandwich
Copy link

The calldataViewFunctions flags all memory parameters as gas inefficient if they aren't modified. This isn't always the case, memory arguments are often cheaper than calldata. This is because calldata is not trusted by Solidity and each access does many sanity checks while memory arguments are sanity checked only once, when loading. I've seen a gas usage drop after switching from calldata to memory in the contracts I've been working on and it even has an issue in Solidity repo, which doesn't seem to be ever solved due to calldata arguments needing the sanity checks: ethereum/solidity#12103.

@IllIllI000
Copy link

IllIllI000 commented Jan 26, 2023

the issue referenced mentions that memory is cheaper if you access more than once, but if you're accessing more than once, you should be using a stack variable anyway. I'll try to come up with some test cases to tease out when it make sense to use one vs the other

@Picodes
Copy link
Owner

Picodes commented Feb 7, 2023

@IllIllI000 I am definitely interested in such test cases

@IllIllI000
Copy link

please remind me after March 25th (GMX contest end)

@InfectedIsm
Copy link

@IllIllI000

⏰ it's after March 25th (sorry was reading the issues and maybe you still want to be reminded lol)

@IllIllI000
Copy link

Ah, thanks I did indeed forget. Still busy with other work, but won't submit this finding again until I have it worked out

@IllIllI000
Copy link

@CodeSandwich do you have an example where memory is cheaper than calldata when the array isn't an array where each array value is smaller than a word? That's the only type of scenario for which I was able to reproduce it: https://gist.github.com/IllIllI000/2ac9a647be917f58ffe5baa2cecbbc42

@CodeSandwich
Copy link
Author

@IllIllI000 I think that your results are spot on, memory only makes sense for types smaller than the word.

I've tried doing some benchmarks myself and for arrays of uint256 calldata was always cheaper no matter how many times the array was accessed, and for uint248 or smaller memory was always cheaper. It's also true for bytes, they are marginally cheaper as calldata. Out of curiosity I've tried uint256[][], and still calldata is cheaper.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants