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

Add calldata located variables. #1499

Merged
merged 10 commits into from Jul 4, 2019

Conversation

@jacqueswww
Copy link
Collaborator

jacqueswww commented Jun 26, 2019

What I did

Fixes #1381.
This is an optimisation I am glad I finally got to make! Basically we were copying variables to memory unnecessarily, by using the calldata directly one not only gets better speed - but also ensures on the EVM level the variables are read-only (yes we had a test that actually was altering a parameter in a tuple assign).
This changes affects these these function parameter types.

  • Arrays
  • All Base Types
    Note: Turned out ByteArrays were too difficult optimise, especially when they tend to have to be packed, which one would have to just allocate them again. Also one looses out not being able to always re-use the identity / mem copy contract.

Taking some files from the examples directory:
Blind Auction -> 73 % less bytecode (!!!)
ERC20 -> 4.9 % less bytecode
ERC720 -> 6.3% less bytecode

Additionally we save runtime costs because we skip 'n full N of mstore (memory growth) on each call.
Also private calls tend to be much cheaper on average, because the in-memory context is smaller.

How I did it

Used a lot of coffee ☕️

How to verify it

Check commits, play with your own contracts.

Description for the changelog

Bytecode & Runtime optimisation for function parameters.

Cute Animal Picture

Put a link to a cute animal picture inside the parenthesis-->

jacqueswww added 8 commits Jun 26, 2019
@jacqueswww

This comment has been minimized.

Copy link
Collaborator Author

jacqueswww commented Jun 27, 2019

@charles-cooper could you give this a look through? :)

@charles-cooper

This comment has been minimized.

Copy link
Collaborator

charles-cooper commented Jun 27, 2019

Sure! I will have to get to it next week, though.

@jacqueswww

This comment has been minimized.

Copy link
Collaborator Author

jacqueswww commented Jun 27, 2019

Sure! I reckon it's mostly OK, I checked it over when it wasn't 4 AM ... and it still looked good hehe

vyper/parser/expr.py Outdated Show resolved Hide resolved
name=arg.name,
pos=default_arg_pos,
typ=arg.typ,
mutable=False,

This comment has been minimized.

Copy link
@charles-cooper

charles-cooper Jul 2, 2019

Collaborator

for clarity, could set location explicitly here

@charles-cooper

This comment has been minimized.

Copy link
Collaborator

charles-cooper commented Jul 2, 2019

@jacqueswww this looks pretty good, I didn't see the code that copies dynamic data to memory though? Could you help point it out to me?

@@ -138,12 +135,29 @@ def parse_public_function(code: ast.FunctionDef,
mem_pos, _ = context.memory_allocator.increase_memory(32 * get_size_of_type(arg.typ))
context.vars[arg.name] = VariableRecord(arg.name, mem_pos, arg.typ, False)

This comment has been minimized.

Copy link
@jacqueswww

jacqueswww Jul 4, 2019

Author Collaborator

@charles-cooper The byte-arrays are copied into memory here.

@jacqueswww jacqueswww merged commit 161acb0 into ethereum:master Jul 4, 2019
3 checks passed
3 checks passed
ci/circleci: lint Your tests passed on CircleCI!
Details
ci/circleci: py36-core Your tests passed on CircleCI!
Details
ci/circleci: py37-core Your tests passed on CircleCI!
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.