sonic-rs icon indicating copy to clipboard operation
sonic-rs copied to clipboard

Unnecessary allocation in `pointer!()` macro?

Open aldanor opened this issue 9 months ago • 1 comments

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)),+]
    );
}
``

aldanor avatar May 10 '24 16:05 aldanor

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.

liuq19 avatar May 14 '24 06:05 liuq19