typst / ecow

Compact, clone-on-write vector and string.
Apache License 2.0
205 stars 16 forks source link

Optimize `ecovec![]` macro #29

Closed Kmeakin closed 1 year ago

Kmeakin commented 1 year ago

LLVM is not smart enough to eliminate calls to EcoVec::reserve() even when they are unecessary thanks to earlier calls to EcoVec::reserve() or EcoVec::with_capacity(). This patch elimates them when the ecovec![] macro is used. There may be other unnecessary calls which can be elimated in future patches.

ecovec![1, 2, 3] results in 3 unecessary calls to EcoVec::reserve(). Fixed by forwarding to the From<[T; N]> impl.

ecovec![1; 42] results in 42 unecessary calls to EcoVec::reserve(). Fixed by adding EcoVec::push_unchecked() function for when we know resizing is unnecessary.

laurmaedje commented 1 year ago

Originally, I planned to make a release today so that EcoVec::new constness is available for use through crates.io. If you plan to submit further PRs, I would wait with this.

laurmaedje commented 1 year ago

Thank you!