-
Notifications
You must be signed in to change notification settings - Fork 6
Improvements to assembly sections in BytesLib
.
#81
base: main
Are you sure you want to change the base?
Conversation
738584a
to
6b0e267
Compare
This improves codegen in the compiler and reduces the chance of untraceable internal codegen errors when dealing with stack variables that can't be referenced with an instruction. Also, it removes unused functions in `BytesLib` to reduce code surface.
6b0e267
to
145a1ce
Compare
lgtm - all my issues / complaints with the code stem from the original BytesLib (as discussed on Slack, putting it here too for completeness sake):
also, again not sure how smart the optimizer is at this point, but both the if and the else branch in slice() start by loading the next free memory location and can hence be done once before the if (i.e. switch) instead of in each arm redundantly inconsistent specification of values in slice() - 31 is specified in dec, everything else in hex |
I think I'll address all of these here and make other improvements in a separate PR, if any. |
you can also combine the range checking and the offset calculation (right now the add is effectively done twice (once for the check, once for the offset) instead of just once) |
It calculates the starting offset for destination and source only once.
I think this is ready to be merged. @nonergodic could you take a peek at the changes? |
Thanks for catching my error in still looks lgtm, i.e. the nitpicky comments below are very much optional: Replacing >= with > could be done in all other I also think that except for 0x40 (which acts as an address of sorts), I'd in fact actually replace all hex values with decimal values (or better yet introduce constants if they are allowed in inline assembly by now) because specifying a number of bits/bytes in hex seems unnecessarily awkward. Not sure why the author(s) of BytesLib chose to originally default to hex in assembly in the first place (and then break it with the |
memory-safe
tag to assembly sections in BytesLib
.BytesLib
.
I'm marking this as draft since I'll be adding an improvement here and don't want to merge it separately. |
This improves codegen in the compiler and reduces the chance of untraceable internal codegen errors when dealing with stack variables that can't be referenced with an instruction.
I've been running into these codegen errors quite a bit while tweaking forge tests but this seems to eliminate them.
In other news, this shaves off 1KB from the contract bytecode so that's a good thing.
The bad news is that marking these sections as memory safe is unsafe to do if they don't actually respect Solidity's memory model. I was actually thinking of creating a reduced version of the
BytesLib
since we don't use all of it but I prefer to do that in another pull request.