Skip to content

Commit

Permalink
feat: optimize bytestrings
Browse files Browse the repository at this point in the history
optimize bytestrings in both codesize and gas

store them in code, and CODECOPY to memory when needed instead of
inlining
  • Loading branch information
charles-cooper committed Jul 16, 2023
1 parent a2fbe2b commit b81a909
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions vyper/codegen/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from vyper import ast as vy_ast
from vyper.codegen import external_call, self_call
from vyper.codegen.core import (
_freshname,
clamp,
ensure_in_memory,
get_dyn_array_count,
Expand Down Expand Up @@ -137,18 +138,16 @@ def parse_Bytes(self):

def _make_bytelike(self, btype, bytez, bytez_length):
placeholder = self.context.new_internal_variable(btype)
seq = []
seq.append(["mstore", placeholder, bytez_length])
for i in range(0, len(bytez), 32):
seq.append(
[
"mstore",
["add", placeholder, i + 32],
bytes_to_int((bytez + b"\x00" * 31)[i : i + 32]),
]
)
ret = ["seq"]
assert isinstance(bytez, bytes)
label = _freshname("bytesdata")
len_ = len(bytez)
ret.append(["data", label, bytez])
ret.append(["codecopy", placeholder + 32, ["symbol", label], len_])
ret.append(["mstore", placeholder, len_])
ret.append(placeholder)
return IRnode.from_list(
["seq"] + seq + [placeholder],
ret,
typ=btype,
location=MEMORY,
annotation=f"Create {btype}: {bytez}",
Expand Down

0 comments on commit b81a909

Please sign in to comment.