Skip to content

feat: add clear api - #29

Merged
aapoalas merged 1 commit into
mainfrom
add-clear-api
Sep 23, 2025
Merged

feat: add clear api#29
aapoalas merged 1 commit into
mainfrom
add-clear-api

Conversation

@omfj

@omfj omfj commented Sep 22, 2025

Copy link
Copy Markdown
Collaborator

Adds a SoAVec::clear() API.

This deallocated all the items inside of the SoAVec and sets the length to 0.

Moved the contents of the Drop-impl to a function called deallocate_items so that the same logic is used for clearing/dropping the items and dropping the SoAVec.

Closes #20.

@omfj omfj added the enhancement New feature or request label Sep 22, 2025
@omfj omfj self-assigned this Sep 22, 2025
@omfj omfj added the enhancement New feature or request label Sep 22, 2025
@omfj
omfj requested a review from aapoalas September 22, 2025 10:52
Comment thread soavec/src/lib.rs Outdated
return;
}

self.deallocate_items();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

issue: There's a subtle memory-safety problem hiding in here.

What happens if some drop of T or call to T::TupleRerp::drop_in_place panics inside the clear call?

  1. Let's assume we've already dropped some T's in the SoAVec (otherwise the problem will not show itself).
  2. A panic occurs inside of deallocate_items, meaning that we leave this function without performing set_len(0).
  3. Regardless of if the panic is caught above or goes on to crash the program, the Drop impl of our SoAVec will get called: it starts dropping the items anew from the beginning.
  4. Since we already dropped some items in the SoAVec in step 1, step 3 now performs double-free and we are in trouble.

Take a look at how Vec::clear is implemented; we'll need to follow its internal logic to make this safe.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I tried to look at how Vec::clear worked, but I didn't really understand what would be the equivalent of ptr::drop_in_place(elems).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Right so; right now we do not have a full equivalent to ptr::drop_in_place. We do have the T::TupleRepr::drop_in_place which works if !T::MUST_DROP_AS_SELF, but for structs that require dropping as Self we simply need to read the data out of the SoAVec, turn it into a T using T::from_tuple, and drop that.

So, effectively it's the full equivalent is deallocate_items except that deallocate_items currently uses self.len(). If we simply take len as a parameter (or perhaps all the ptr, cap, and len values) then we can reuse the function both here and in Drop; in Drop we don't need to call set_len as we're already dropping the SoAVec: if a panic happens inside Drop then it will not be retried.

In clear we'll have to read len, then set_len(0), and then call deallocate_items with the stored len value. That way even if deallocate_items panics, dropping the SoAVec will see a 0 length and will not try to re-drop the items.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Ok, hopefully I understood. I renamed the function to better match the one used in the actual Vec::clear too.

@omfj
omfj force-pushed the add-clear-api branch 4 times, most recently from a604479 to 55ea75f Compare September 22, 2025 20:12

@aapoalas aapoalas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM!

@aapoalas
aapoalas merged commit 8a4c8a0 into main Sep 23, 2025
3 checks passed
@aapoalas
aapoalas deleted the add-clear-api branch September 23, 2025 05:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

clear API

3 participants