Skip to content

Comments

Optimize From<Vec<T>> for EcoVec<T>#50

Merged
laurmaedje merged 5 commits intotypst:mainfrom
bergkvist:tobias/from-vec
Aug 4, 2025
Merged

Optimize From<Vec<T>> for EcoVec<T>#50
laurmaedje merged 5 commits intotypst:mainfrom
bergkvist:tobias/from-vec

Conversation

@bergkvist
Copy link
Contributor

Inspired by the implementation of From<Vec<T>> for Arc<[T]>:
https://github.com/rust-lang/rust/blob/175e04331be56c5b4bdf77478434b1a5e0556770/library/alloc/src/sync.rs#L3823-L3851

  • Vec::into_raw_parts(self) APIs are experimental, so we can't use them, even though the stdlib Arc implementation uses it internally.

When we copy/move the elements from the Vec into the EcoVec, we need to ensure that the items don't get dropped twice.

  • For this we use Vec::set_len(&mut self, len: usize) to set the length to 0, which ensures that when the Vec is dropped later, and its buffer is freed - it doesn't run drop on the individual items that were moved into the EcoVec.

@bergkvist bergkvist requested a review from laurmaedje August 3, 2025 16:04
@laurmaedje laurmaedje changed the title Optimize From<Vec<T>> for EcoVec<T> Optimize From<Vec<T>> for EcoVec<T> Aug 4, 2025
@laurmaedje laurmaedje merged commit 54bea9d into typst:main Aug 4, 2025
4 checks passed
@laurmaedje
Copy link
Member

Thanks!

@laurmaedje
Copy link
Member

Published ecow 0.2.6 with this change.

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