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

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

charles-cooper
Copy link
Member

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

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.

fix an assertion failure in the memmerge pass.

What I did

How I did it

How to verify it

check codesize improvements

Commit message

Commit message for the final, squashed PR. (Optional, but reviewers will appreciate it! Please see our commit message style guide for what we would ideally like to see in a commit message.)

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
Copy link

codecov bot commented Dec 22, 2024

Codecov Report

Attention: Patch coverage is 58.33333% with 10 lines in your changes missing coverage. Please review.

Project coverage is 51.11%. Comparing base (9c98b3e) to head (65851c3).

Files with missing lines Patch % Lines
vyper/venom/passes/memmerging.py 0.00% 7 Missing ⚠️
vyper/codegen/core.py 82.35% 2 Missing and 1 partial ⚠️

❗ There is a different number of reports uploaded between BASE (9c98b3e) and HEAD (65851c3). Click for more details.

HEAD has 110 uploads less than BASE
Flag BASE (9c98b3e) HEAD (65851c3)
140 30
Additional details and impacted files
@@             Coverage Diff             @@
##           master    #4419       +/-   ##
===========================================
- Coverage   91.76%   51.11%   -40.66%     
===========================================
  Files         119      119               
  Lines       16615    16634       +19     
  Branches     2796     2803        +7     
===========================================
- Hits        15247     8502     -6745     
- Misses        936     7488     +6552     
- Partials      432      644      +212     

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

Copy link
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

@@ -276,6 +303,9 @@ def _dynarray_make_setter(dst, src, hi=None):
n_bytes = add_ofst(_mul(count, element_size), 32)
max_bytes = 32 + src.typ.count * element_size

if _prefer_copy_maxbound_heuristic(dst, src, element_size):
n_bytes = max_bytes
Copy link
Collaborator

Choose a reason for hiding this comment

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

codecov reports this isn't covered - can you please fix that?

Copy link
Member Author

@charles-cooper charles-cooper Jan 2, 2025

Choose a reason for hiding this comment

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

it's covered -- you can verify by running the test suite with the following patch:

diff --git a/vyper/codegen/core.py b/vyper/codegen/core.py
index 64c6a62a8..74cfebc79 100644
--- a/vyper/codegen/core.py
+++ b/vyper/codegen/core.py
@@ -304,6 +304,7 @@ def _dynarray_make_setter(dst, src, hi=None):
                 max_bytes = 32 + src.typ.count * element_size
 
                 if _prefer_copy_maxbound_heuristic(dst, src, element_size):
+                    raise Exception("covered")
                     n_bytes = max_bytes
 
                 # batch copy the entire dynarray, including length word

for me, this quickly raises an exception at "tests/functional/codegen/types/test_dynamic_array.py::test_dynarray_length_no_clobber[\n@external\ndef should_revert():\n c: DynArray[DynArray[uint256, 1], 2] = [[]]\n c[0] = c.pop()\n ]".
Screenshot from 2025-01-02 17-11-40

@cyberthirst
Copy link
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants