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

Copy only after concat args traversal #620

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Copy only after concat args traversal #620

wants to merge 3 commits into from

Conversation

ureeves
Copy link
Collaborator

@ureeves ureeves commented Feb 21, 2025

Code generated for the concat word is changed to ensure both its arguments are traversed before copying their contents onto the resulting sequence.

This avoids a partially copied to return buffer should the code fail during resolution of the second argument. It will also allow for a cleaner implementation of cost tracking since we can then also avoid copying on a cost overrun.

See-also: #616

Code generated for the `concat` word is changed to ensure both its
arguments are traversed *before* copying their contents onto the
resulting sequence.

This avoids a partially copied to return buffer should the code fail
during resolution of the second argument. It will also allow for a
cleaner implementation of cost tracking since we can then also avoid
copying on a cost overrun.

See-also: #616
Copy link

codecov bot commented Feb 21, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 85.66%. Comparing base (d046c16) to head (4f31c8d).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #620   +/-   ##
=======================================
  Coverage   85.66%   85.66%           
=======================================
  Files          46       46           
  Lines       24597    24592    -5     
  Branches    24597    24592    -5     
=======================================
- Hits        21070    21067    -3     
  Misses       1675     1675           
+ Partials     1852     1850    -2     

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

Comment on lines 485 to 486
generator.set_expr_type(lhs_expr, ret_ty.clone())?;
generator.set_expr_type(rhs_expr, ret_ty)?;
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is something that has always bothered me here, so maybe it's a good time to change this: the type of the lists arguments shouldn't be exactly the same as ret_ty. When we do this, we stupidly set the arguments max len to the same size as ret_ty. So if the traversal of an argument has to allocate memory, it will allocate the size of ret_ty instead of the real max size of the argument.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Should I just remove those two lines then? It seems to me like these shouldn't even be here given what you're saying.

Copy link
Collaborator Author

@ureeves ureeves Feb 24, 2025

Choose a reason for hiding this comment

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

Tests pass with the lines removed

Copy link
Collaborator

Choose a reason for hiding this comment

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

Normally, you were lucky if the tests pass. if I'm not wrong, we should still have an issue if we try to concatenate two lists with types which would be for example (list 1 (optional Notype)) and (list 1 (optional int)).

Comment on lines 491 to 511
/* [] */
builder.local_get(ret_off);
/* [ret_off] */
generator.traverse_expr(builder, lhs_expr)?;
builder.local_tee(lhs_len);
/* [ret_off, lhs_off, lhs_len] */
builder.local_get(ret_off);
/* [ret_off, lhs_off, lhs_len, ret_off] */
builder.local_get(lhs_len);
/* [ret_off, lhs_off, lhs_len, ret_off, lhs_len] */
builder.binop(BinaryOp::I32Add);
/* [ret_off, lhs_off, lhs_len, ret_off + lhs_len] */
generator.traverse_expr(builder, rhs_expr)?;
builder.local_tee(rhs_len);
/* [ret_off, lhs_off, lhs_len, ret_off + lhs_len, rhs_off, rhs_len] */

// Copy arguments to the return buffer. RHS is copied 1st, and LHS 2nd

/* [ret_off, lhs_off, lhs_len, ret_off + lhs_len, rhs_off, rhs_len] */
builder.memory_copy(memory, memory);
/* [ret_off, lhs_off, lhs_len] */
Copy link
Collaborator

Choose a reason for hiding this comment

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

You won't believe this, but there are so many comments that my brain can't parse the code :D
Might be just me though.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Okay, I took a sheet of paper and rewrote this in Wat, it makes sense :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I actually do believe you, I used them as aid while rewriting. Would you prefer I remove them? Or perhaps I could leave them, but just on top?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I tend to never put any, and I don't think I am a good example of documentation 😅
But yeah, maybe removing all the intermediary comments, regrouping all the builder.x into a single expression, and explaining on top what you are calculating could be more readable.
Not sure though, we should try.

@ureeves ureeves requested a review from Acaccia February 24, 2025 17:01
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.

2 participants