Skip to content

Add calldata located variables.#1499

Merged
jacqueswww merged 10 commits into
vyperlang:masterfrom
jacqueswww:1381_calldata_location
Jul 4, 2019
Merged

Add calldata located variables.#1499
jacqueswww merged 10 commits into
vyperlang:masterfrom
jacqueswww:1381_calldata_location

Conversation

@jacqueswww

@jacqueswww jacqueswww commented Jun 26, 2019

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Contributor Author

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

@charles-cooper

Copy link
Copy Markdown
Member

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

@jacqueswww

jacqueswww commented Jun 27, 2019

Copy link
Copy Markdown
Contributor Author

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

Comment thread vyper/parser/events.py
Comment thread vyper/parser/expr.py Outdated
Comment thread vyper/parser/function_definitions/parse_public_function.py Outdated
name=arg.name,
pos=default_arg_pos,
typ=arg.typ,
mutable=False,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for clarity, could set location explicitly here

Comment thread vyper/parser/self_call.py
Comment thread vyper/parser/stmt.py
Comment thread vyper/parser/parser_utils.py
Comment thread vyper/parser/function_definitions/parse_public_function.py
@charles-cooper

Copy link
Copy Markdown
Member

@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)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@jacqueswww jacqueswww merged commit 161acb0 into vyperlang:master Jul 4, 2019
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

Successfully merging this pull request may close these issues.

Frequent use of CALLDATACOPY

2 participants