Skip to content

How to create a TinyVec of a specific size (not capacity!), initialized to zero (or user-defined value) #210

Closed

Description

@dEajL3kA

Hello.

What is the most efficient way to create a new TinyVec of size length, which is initialized zero (or some user-defined value)?

Is there a better way than this?

#[inline(always)]
pub fn calloc_tinyvec<const N: usize>(length: usize) -> TinyVec<[u8; N]> {
    let mut vec = TinyVec::with_capacity(length);
    vec.resize(length, 0u8);
    vec
}

Is this version preferible?

#[inline(always)]
pub fn calloc_tinyvec<const N: usize>(length: usize) -> TinyVec<[u8; N]> {
    if length > N {
        let mut vec = TinyVec::with_capacity(length);
        vec.resize(length, 0u8);
        vec
    } else {
        TinyVec::from_array_len([0u8; N], length)
    }
}

Maybe it could be useful to have a method like:

TinyVec::with_size<A: Array>(length: usize, inital_value: T) -> TinyVec<A>

🤔

Best regards!

Activity

  1. changed the title [-]Create a TinyVec of a specific size (not capacity!), initialized to zero (or user-defined value)[/-] [+]How to create a TinyVec of a specific size (not capacity!), initialized to zero (or user-defined value)[/+] on Dec 5, 2025
  2. lordmulder commented on Dec 17, 2025

    @lordmulder
    Contributor

    I did some performance tests:

    1. #[inline(always)]
      pub fn mk_tinyvec_1<const N: usize>(length: usize) -> TinyVec<[u8; N]> {
          TinyVec::from_iter(iter::repeat_n(0u8, length))
      }
    2. #[inline(always)]
      pub fn mk_tinyvec_2<const N: usize>(length: usize) -> TinyVec<[u8; N]> {
          let mut vec = TinyVec::with_capacity(length);
          vec.resize(length, 0u8);
          vec
      }
    3. #[inline(always)]
      pub fn mk_tinyvec_3<const N: usize>(length: usize) -> TinyVec<[u8; N]> {
          if length > N {
              let mut vec = TinyVec::with_capacity(length);
              vec.resize(length, 0u8);
              vec
          } else {
              TinyVec::from_array_len([0u8; N], length)
          }
      }
    4. #[inline(always)]
      pub fn mk_tinyvec_4<const N: usize, T: Copy + Default>(length: usize) -> TinyVec<[T; N]> {
          if length <= N {
              TinyVec::Inline(ArrayVec::from_array_len([T::default(); N], length))
          } else {
              TinyVec::Heap(vec![T::default(); length])
          }
      }

    Test bench:

    const LOOPS: usize = 999999999usize;
    type TinyVecType = TinyVec<[u8; 256usize]>;
    
    macro_rules! do_test_helper {
        ($func:ident, $n:literal) => {{
            {
                let start_time = Instant::now();
                for _ in 0usize..LOOPS {
                    let instance: TinyVecType = $func($n);
                    assert_eq!(instance.len(), $n);
                    assert_eq!(instance[0usize], 0u8);
                    assert_eq!(instance[$n - 1usize], 0u8);
                }
                let elapsed = start_time.elapsed();
                println!("{} - {} - {:.3}", stringify!($func), $n, elapsed.as_secs_f64());
            }
        }};
    }
    
    macro_rules! do_test {
        ($func:ident) => {{
            do_test_helper!($func, 128usize);
            do_test_helper!($func, 256usize);
            do_test_helper!($func, 512usize);
            println!();
        }};
    }
    
    fn main() {
        for i in 0..2 {
            if i > 0 {
                println!("----");
                println!();
            }
            do_test!(mk_tinyvec_1);
            do_test!(mk_tinyvec_2);
            do_test!(mk_tinyvec_3);
            do_test!(mk_tinyvec_4);
        }
    }

    Results:

    mk_tinyvec_1 - 128 - 25.646
    mk_tinyvec_1 - 256 - 36.296
    mk_tinyvec_1 - 512 - 59.758
    
    mk_tinyvec_2 - 128 - 36.814
    mk_tinyvec_2 - 256 - 26.966
    mk_tinyvec_2 - 512 - 39.912
    
    mk_tinyvec_3 - 128 - 0.000
    mk_tinyvec_3 - 256 - 0.000
    mk_tinyvec_3 - 512 - 40.039
    
    mk_tinyvec_4 - 128 - 0.000
    mk_tinyvec_4 - 256 - 0.000
    mk_tinyvec_4 - 512 - 5.359
    

    Version #4 wins easily 🥳

  3. Lokathor commented on Aug 19, 2026

    @Lokathor
    Owner

    Based on the benchmarks, looks like a solid answer and we can close this.

  4. lordmulder commented on Aug 19, 2026

    @lordmulder
    Contributor

    Too bad nobody will find this information.

    Wouldn't it make sense to have a built-in function that creates a TinyVec of specific size? (pre-filled with the given value)

  5. Lokathor commented on Aug 19, 2026

    @Lokathor
    Owner

    Yeah make a PR. You'll probably have to feature gate it behind latest_stable_rust to pass CI.

  6. lordmulder commented on Aug 20, 2026

    @lordmulder
    Contributor

    Here is a first suggestion:
    #224

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions