From 538072bc3b11a7c212e1fa4b3ec38b9b87a8d951 Mon Sep 17 00:00:00 2001 From: trocher <43437004+trocher@users.noreply.github.com> Date: Thu, 24 Apr 2025 15:01:12 +0200 Subject: [PATCH] chore: add explainations for the maxbound heuristic --- vyper/codegen/core.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/vyper/codegen/core.py b/vyper/codegen/core.py index ea51eda832..e9ddd9cbfa 100644 --- a/vyper/codegen/core.py +++ b/vyper/codegen/core.py @@ -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 MLOAD PUSH1 ITEMSIZE MUL PUSH1 32 ADD (3 * 4 + 8 = 20 gas | 8 bytes) + # DUP 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 @@ -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