-
Notifications
You must be signed in to change notification settings - Fork 11.8k
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
Consistently name multiple returned values #5177
base: master
Are you sure you want to change the base?
Conversation
|
JPoint[16] memory points, | ||
uint256 u1, | ||
uint256 u2 | ||
) private view returns (uint256 ax, uint256 ay) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why ax
, ay
? I confused by the a
part
- Could be
rx
,adry
for "result" - Could be
vx
andvy
(because v comes after u)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Named them after "affine x" and "affine y". I'm fine with rx
and ry
I'm concerned with the performance impact of this change, because yes, there is an impact. Lets compare function double_v1(uint256 X) public returns (uint256) {
return 2 * x;
}
function double_v2(uint256 X) public returns (uint256 result) {
return 2 * x;
}
function double_v3(uint256 X) public returns (uint256 result) {
result = 2 * x;
}
I'm not exactly sure what the compiler optimizes, but option 2 is clearly the worst in terms of stack space usage. Naming the returns creates a stack object in between the arguments of the function and the internal variable of the function. This makes stack too deep happen "faster". Here is looks like the impact will be negligeable. Probably less then 10/20 gas per function. Possibly 0 with optimisations ? |
The second case is more expensive, but I'd be fine with the gas increase. Generally, we prefer to follow guidelines over gas savings and here the order of the savings is low. We're not always naming variables, only when there are multiple of them, which doesn't happen often. Alternative, we can use the 3rd option, but I remember we never liked it for readability. We might agree to do it whenever returned values are named for whatever reason. |
I'd be ok with doing option 2 when that doesn't cause stack too deep errors. In cases where options 2 causes stack to deep error, then I would go for either option 3, or with function double_v2_inline_commented(uint256 X) public returns (uint256 /*result*/) {
return 2 * x;
} |
In cases where a stack too deep error is caused I'd just go for option 3. We've done that in the past in AccessManager#schedule |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should the return values of getFull
and _getFullAt
functions in Time
be named as well?
Fixes N-05
Took the time to look for every case of multiple return values that weren't named. We can reduce the scope to those of the audit if needed.
PR Checklist
npx changeset add
)