Skip to content

chore[docs]: document the copy maxbound heuristic reasoning #4593

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion vyper/codegen/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,12 @@ def _prefer_copy_maxbound_heuristic(dst, src, item_size):
# a heuristic - it's cheaper to just copy the extra buffer bytes
# than calculate the number of bytes
# copy(dst, src, 32 + itemsize*load(src))
# DUP<src> MLOAD PUSH1 ITEMSIZE MUL PUSH1 32 ADD (3 * 4 + 8 = 20 gas | 8 bytes)
# DUP<src> MLOAD PUSH1 32 ADD (3 * 4 = 12 gas | 5 bytes) if ITEM_SIZE == 1
# =>
# copy(dst, src, bound)
# (32 + itemsize*(load(src))) costs 4*3 + 8 - 3 gas over just `bound`
# PUSH1 BOUND (3 gas | 2 bytes)
# (32 + itemsize*(load(src))) costs 3 * 4 [+ 8] - 3 gas over just `bound`
length_calc_cost = 4 * 3 - 3
length_calc_cost += 8 * (item_size != 1) # PUSH MUL

Expand Down Expand Up @@ -237,6 +240,8 @@ def _prefer_copy_maxbound_heuristic(dst, src, item_size):
# or not.
# (dload(src) expands to `codecopy(0, add(CODE_END, src), 32); mload(0)`,
# and we have already accounted for an `mload(ptr)`).
# PUSH1 32 DUP2 PUSH CODE_END ADD PUSH0 CODECOPY (3 * 4 + 2 + 6 = 20 gas)
# PUSH1 32 PUSH OFFSET PUSH0 CODECOPY (3 * 2 + 2 + 6 = 14 gas) if src is a literal
# for simplicity, skip the 14 case.
if src.location == DATA and copy_cost <= (20 + length_calc_cost):
return True
Expand Down