sonic-rs
sonic-rs copied to clipboard
Unnecessary allocation in `pointer!()` macro?
As it currently stands, pointer!()
macro allocates a Vec
(or a boxed slice, rather). Why is it necessary where a simply array would do? An array also implements IntoIterator
, so you could just have [pointer_node1, pointer_node_2, pointer_node3]
without extra allocations.
It's understandable that it's nice to be able to .push()
into it, but having an allocation by default would really hurt the most common case where you just use pointer!()
as is, and is a bit un-rust-like. Perhaps the vec-like pointer!
could be a separate macro? Or, you can always do an pointer!(...).into_vec()
if you need to mutate it.
Suggestion:
#[macro_export]
macro_rules! pointer {
() => (
([] as [$crate::PointerNode; 0])
);
($($x:expr),+ $(,)?) => (
[$($crate::PointerNode::from($x)),+]
);
}
``
Yes, an array can be used here to reduce an allocation.
This is a break change and will be published in sonic-rs 0.4
.