Replacng vec with tinyvec #347
stevefan1999-personal
started this conversation in
Ideas
Replies: 2 comments
-
POC usage: use tinyvec::{array_vec, tiny_vec};
fn main() {
const MIN: usize = 2;
let mut unbounded_arr = tiny_vec!([_; MIN]);
for _ in 1..=4 {
unbounded_arr.push("test");
}
// ["test", "test", "test", "test"]
println!("{:?}", unbounded_arr);
let mut bounded_arr = array_vec!([_; MIN]);
for _ in 1..=MIN {
bounded_arr.push("test");
}
// ["test", "test"]
println!("{:?}", bounded_arr);
// Panic!
bounded_arr.push("test");
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
Oh I realized why this is not made in the first place. It is not necessary for the production value to have |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
This library is able to make both array/stack based vector and heap-allocated vector based on the need (https://docs.rs/tinyvec/latest/tinyvec/). It also claimed to have near correspondence to
alloc::vec::Vec
which means we can just use it out of the box given the low amount of API usage.This is extremely useful on situations like repetition, especially when we have finite count bounds, since we can simply just make an array limited by max bound. And we can just use a heap vector if the upper count is unbounded.
And we can also replace these into
try_append
(https://docs.rs/tinyvec/latest/tinyvec/struct.ArrayVec.html#method.try_append) somewhere elserust-peg/peg-macros/translate.rs
Lines 672 to 722 in f62301b
Not only this would eliminate the hidden allocation once and for all, making the overall parsing more deterministic, but this would also greatly help optimization, or even benefit from SIMD. The only thing left might be about lifetime bound issue, that use-after-free of the
ArrayVec
could be possible during parser production action. Maybe we should relax it from Vec into &[T], and let the user collect the value to heap if they want to. But this would be an API breaking change that needs special care so I suggest discussing first.We just need to tell user that if you want to have unbounded repetition then you need to use the
alloc
feature.Beta Was this translation helpful? Give feedback.
All reactions