Skip to content

High memory usage with a simple string vector example #1673

Closed

Description

@zhassan-aws

I tried this code:

const N: usize = 9;

#[cfg_attr(kani, kani::proof, kani::unwind(10))]
fn main() {
    let mut v: Vec<String> = Vec::new();
    for _i in 0..N {
        v.push(String::from("ABC"));
    }
    assert_eq!(v.len(), N);
    let index: usize = kani::any();
    kani::assume(index < v.len());
    let x = &v[index];
    assert_eq!(*x, "ABC");
}

using the following command line invocation:

kani push_string.rs

with Kani version: 0fa9ee3

This finished in ~8.5 minutes and used ~23 GB of memory:

image

Although at some point, memory usage went up to ~27 GB.

If I bump N to 10 (and unwind to 11), it runs out of memory (>32 GB).

Activity

  1. added
    [C] Bug
    [E] Performance
    on Sep 13, 2022
  2. zhassan-aws commented on Sep 13, 2022

    @zhassan-aws
    ContributorAuthor

    As with #1657, if I replace this line:

        let mut v: Vec<String> = Vec::new();

    with:

        let mut v: Vec<String> = Vec::with_capacity(N);

    Verification time goes down to 22 seconds, and memory usage down to 143 MB!

  3. zhassan-aws commented on Sep 13, 2022

    @zhassan-aws
    ContributorAuthor

    If I use a poor man's vector implementation such as the one below, I don't observe any blowups in memory:

    use std::alloc::{alloc, dealloc, Layout};
    
    const N: usize = 15;
    
    type T = String;
    
    struct MyVec {
        size: usize,
        capacity: usize,
        layout: Layout,
        data: *mut T,
    }
    
    impl MyVec {
        fn new(capacity: usize) -> Self {
            let layout = Layout::array::<T>(capacity).unwrap();
            let data = unsafe { alloc(layout) } as *mut T;
            Self { size: 0, capacity, layout, data }
        }
    
        fn push(&mut self, value: T) {
            unsafe {
                if self.size == self.capacity {
                    self.capacity *= 2;
                    let layout = Layout::array::<T>(self.capacity).unwrap();
                    let data = alloc(layout) as *mut T;
                    std::ptr::copy_nonoverlapping(self.data, data, self.size);
                    dealloc(self.data as *mut u8, self.layout);
                    self.data = data;
                    self.layout = layout;
                    //self.data = realloc(self.data, self.layout, self.capacity);
                }
                std::ptr::write(self.data.add(self.size), value);
                self.size += 1;
            }
        }
    
        fn get(&self, index: usize) -> &T {
            unsafe {
                &(*self.data.add(index))
            }
        }
    
        fn len(&self) -> usize {
            self.size
        }
    }
    
    #[cfg_attr(kani, kani::proof, kani::unwind(16))]
    fn main() {
        let mut v: MyVec = MyVec::new(1);
        for _i in 0..N {
            v.push(String::from("ABC"));
        }
        assert_eq!(v.len(), N);
        let index: usize = kani::any();
        kani::assume(index < v.len());
        let x = v.get(index);
        assert_eq!(*x, "ABC");
    }

    With N = 15, verification time is 75 seconds, and memory usage is under 200 MB.

  4. tautschnig commented on Oct 10, 2022

    @tautschnig
    Member

    Still WIP, but diffblue/cbmc#7230 might help here: the example at the top of this issue takes 18 seconds instead of 428 on my machine.

  5. tautschnig commented on Nov 9, 2022

    @tautschnig
    Member

    With diffblue/cbmc#7230 on top of diffblue/cbmc@70b7cf7baf735f I get "Verification Time: 3.781643s" when it takes 471.05692s using diffblue/cbmc@70b7cf7baf735f and Kani revision 057926b.

  6. zhassan-aws commented on Nov 9, 2022

    @zhassan-aws
    ContributorAuthor

    Awesome! Thanks @tautschnig! Did the memory usage go down as well?

  7. tautschnig commented on Nov 10, 2022

    @tautschnig
    Member

    Memory consumption appears to be below 250 MB :-)

  8. zhassan-aws commented on Nov 10, 2022

    @zhassan-aws
    ContributorAuthor

    Nice!

  9. added
    T-CBMC
    on Dec 6, 2022
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

    T-CBMC[C] Bug[E] Performance

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions