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

improve loop performance #4099

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion vyper/codegen/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ def parse_Name(self):
ret._referenced_variables = {varinfo}
return ret

if varname in self.context.forvars:
return self.context.forvars[varname]

if varinfo.is_constant:
return Expr.parse_value_expr(varinfo.decl_node.value, self.context)

Expand Down Expand Up @@ -689,7 +692,7 @@ def parse_Call(self):
(arg0,) = self.expr.args
arg_ir = Expr(arg0, self.context).ir_node

assert arg_ir.typ == AddressT()
assert isinstance(arg_ir.typ, (AddressT, InterfaceT)), arg_ir.typ
arg_ir.typ = self.expr._metadata["type"]

return arg_ir
Expand Down
16 changes: 6 additions & 10 deletions vyper/codegen/stmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,11 @@ def _parse_For_range(self):

varname = self.stmt.target.target.id
i = IRnode.from_list(self.context.fresh_varname("range_ix"), typ=target_type)
iptr = self.context.new_variable(varname, target_type)

self.context.forvars[varname] = True
assert varname not in forvars
self.context.forvars[varname] = i

loop_body = ["seq"]
# store the current value of i so it is accessible to userland
loop_body.append(["mstore", iptr, i])
loop_body.append(parse_body(self.stmt.body, self.context))

del self.context.forvars[varname]
Expand All @@ -247,14 +245,9 @@ def _parse_For_list(self):

# user-supplied name for loop variable
varname = self.stmt.target.target.id
loop_var = IRnode.from_list(
self.context.new_variable(varname, target_type), typ=target_type, location=MEMORY
)

i = IRnode.from_list(self.context.fresh_varname("for_list_ix"), typ=UINT256_T)

self.context.forvars[varname] = True

ret = ["seq"]

# list literal, force it to memory first
Expand All @@ -269,7 +262,10 @@ def _parse_For_list(self):

# set up the loop variable
e = get_element_ptr(iter_list, i, array_bounds_check=False)
body = ["seq", make_setter(loop_var, e), parse_body(self.stmt.body, self.context)]
assert varname not in self.context.forvars
self.context.forvars[varname] = e

body = parse_body(self.stmt.body, self.context)

repeat_bound = iter_list.typ.count
if isinstance(iter_list.typ, DArrayT):
Expand Down
Loading