Skip to content

fix[codegen]: fix overcopying of bytes in make_setter#4419

Merged
charles-cooper merged 28 commits intovyperlang:masterfrom
charles-cooper:fix/copy-bytes-perf
Apr 12, 2025
Merged

fix[codegen]: fix overcopying of bytes in make_setter#4419
charles-cooper merged 28 commits intovyperlang:masterfrom
charles-cooper:fix/copy-bytes-perf

Conversation

@charles-cooper
Copy link
Copy Markdown
Member

@charles-cooper charles-cooper commented Dec 21, 2024

What I did

How I did it

How to verify it

check codesize improvements

note this PR originally had changes to the memmerge pass in venom, but they were superseded by the changes in #4422

Commit message

in `_complex_make_setter`, when a dynamic type (e.g. `Bytes` or
`DynArray`) is contained within a tuple, the current code generation
copies the entire buffer without checking how large it is.

note this represents a gas issue since more bytes might be copied than
is necessary, but not a correctness issue.

in comparison, the `make_setter` implementations for `Bytes`
and `DynArray` limit the copy length to the runtime sizes of the
`Bytes`/`DynArray`.

this commit disables the full buffer copy and adds a heuristic to
the `make_setter` implementations for `DynArray` and `Bytes` so that
in certain cases, they copy the full buffer instead of checking the
length.

Description for the changelog

Cute Animal Picture

Put a link to a cute animal picture inside the parenthesis-->

in `_complex_make_setter`, when a dynamic type (e.g. Bytes or DynArray)
is contained within a tuple, the current code generation copies the
entire buffer without checking how large it is.

(note this represents a gas issue since more bytes might be copied than
 is necessary, but not a correctness issue).

in comparison, Bytes and DynArray make_setters limit the copy length to
the runtime sizes of the Bytes/DynArray.

this commit disables the full buffer copy and adds a heuristic to the
make_setter implementations for DynArray and Bytes so that in certain
cases, they copy the full buffer instead of checking the length.
@charles-cooper charles-cooper marked this pull request as ready for review December 22, 2024 00:59
@charles-cooper charles-cooper changed the title fix[codegen]: fix overcopying of bytes in make_setter fix[codegen]: fix overcopying of bytes in make_setter Dec 22, 2024
@codecov
Copy link
Copy Markdown

codecov bot commented Dec 22, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.71%. Comparing base (2d515d3) to head (56f9142).
Report is 82 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #4419   +/-   ##
=======================================
  Coverage   92.70%   92.71%           
=======================================
  Files         123      123           
  Lines       17526    17546   +20     
  Branches     2971     2977    +6     
=======================================
+ Hits        16248    16268   +20     
  Misses        879      879           
  Partials      399      399           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Collaborator

@HodanPlodky HodanPlodky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Venom part seems good

@cyberthirst
Copy link
Copy Markdown
Collaborator

the heuristic doesn't seem precise (chatted about this offline). besides that the non-venom part looks sound and shouldn't break invariants. it should be equivalent to having the max_len provided at runtime (if are size calculations are correct) anyway

the coverage report suggests these new changes aren't covered by tests, let's please verify that

@charles-cooper
Copy link
Copy Markdown
Member Author

TODO: add heuristic when optimization level is set to codesize

@charles-cooper charles-cooper added this to the v0.4.1 milestone Feb 17, 2025
@charles-cooper
Copy link
Copy Markdown
Member Author

TODO: add heuristic when optimization level is set to codesize

added in 776fb48

@cyberthirst cyberthirst modified the milestones: v0.4.1, v0.4.2 Mar 10, 2025
@charles-cooper charles-cooper added release - tentative items still being considered for release inclusion release - must release blocker and removed release - tentative items still being considered for release inclusion labels Mar 17, 2025
length_calc_cost += 15

src_bound = src.typ.memory_bytes_required
if src.location in (CALLDATA, MEMORY) and src_bound <= (5 + length_calc_cost) * 32:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why don't we divide by 3 to account for the gas cost of copy op per word?

@charles-cooper charles-cooper enabled auto-merge (squash) April 12, 2025 09:11
@charles-cooper charles-cooper merged commit 9486a41 into vyperlang:master Apr 12, 2025
161 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release - must release blocker

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants