Skip to content

Commit 6d91720

Browse files
fix: don't alloc slots for nonreentrant keys..
which appear multiple times. this is not a semantic bug but an optimization bug since we allocate more slots than we actually need, leading to "holes" in the slot allocator -- slots which are allocated but unused.
1 parent 8f95dad commit 6d91720

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

tests/cli/outputs/test_storage_layout.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ def public_bar():
2727

2828
assert out["layout"] == {
2929
"nonreentrant.foo": {"type": "nonreentrant lock", "location": "storage", "slot": 0},
30-
"nonreentrant.bar": {"type": "nonreentrant lock", "location": "storage", "slot": 2},
30+
"nonreentrant.bar": {"type": "nonreentrant lock", "location": "storage", "slot": 1},
3131
"foo": {
3232
"type": "HashMap[address, uint256][address, uint256]",
3333
"location": "storage",
34-
"slot": 3,
34+
"slot": 2,
3535
},
36-
"baz": {"type": "Bytes[65]", "location": "storage", "slot": 4},
37-
"bar": {"type": "uint256", "location": "storage", "slot": 9},
36+
"baz": {"type": "Bytes[65]", "location": "storage", "slot": 3},
37+
"bar": {"type": "uint256", "location": "storage", "slot": 8},
3838
}

vyper/semantics/validation/data_positions.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,30 @@ def set_storage_slots(vyper_module: vy_ast.Module) -> StorageLayout:
3232

3333
for node in vyper_module.get_children(vy_ast.FunctionDef):
3434
type_ = node._metadata["type"]
35-
if type_.nonreentrant is not None:
36-
type_.set_reentrancy_key_position(StorageSlot(storage_slot))
37-
38-
# TODO this could have better typing but leave it untyped until
39-
# we nail down the format better
40-
variable_name = f"nonreentrant.{type_.nonreentrant}"
41-
ret[variable_name] = {
42-
"type": "nonreentrant lock",
43-
"location": "storage",
44-
"slot": storage_slot,
45-
}
46-
47-
# TODO use one byte - or bit - per reentrancy key
48-
# requires either an extra SLOAD or caching the value of the
49-
# location in memory at entrance
50-
storage_slot += 1
35+
if type_.nonreentrant is None:
36+
continue
37+
38+
variable_name = f"nonreentrant.{type_.nonreentrant}"
39+
40+
# a nonreentrant key can appear many times in a module but it
41+
# only takes one slot. ignore it after the first time we see it.
42+
if variable_name in ret:
43+
continue
44+
45+
type_.set_reentrancy_key_position(StorageSlot(storage_slot))
46+
47+
# TODO this could have better typing but leave it untyped until
48+
# we nail down the format better
49+
ret[variable_name] = {
50+
"type": "nonreentrant lock",
51+
"location": "storage",
52+
"slot": storage_slot,
53+
}
54+
55+
# TODO use one byte - or bit - per reentrancy key
56+
# requires either an extra SLOAD or caching the value of the
57+
# location in memory at entrance
58+
storage_slot += 1
5159

5260
for node in vyper_module.get_children(vy_ast.AnnAssign):
5361
type_ = node.target._metadata["type"]

0 commit comments

Comments
 (0)