Skip to content

Poor performance scaling for a simple program involving String push/pop #2015

Description

@zhassan-aws

Verification scales poorly on a simple program involving String push and pop even when the String is created using with_capacity:

#[kani::proof]
#[kani::unwind(2)]
fn main() {
    const N: usize = 1;
    let mut s = String::with_capacity(50);
    let chars: [char; N] = kani::any();
    for c in chars {
        s.push(c);
    }
    for c in chars.iter().rev() {
        assert_eq!(s.pop(), Some(*c));
    }
}

Verification time and memory usage is as follows:

# of pushes/pops Verification time (s) Memory usage (MB)
1 4 181
2 13 663
3 61 1872
4 225 4430
5 882 9145
6 2834 17746

In comparison, performance scaling is much better on a similar program with vector push/pop:

#[kani::proof]
#[kani::unwind(2)]
fn main() {
    const N: usize = 1;
    let mut v = Vec::with_capacity(10);
    let arr: [i32; N] = kani::any();
    for x in arr {
        v.push(x);
    }
    for x in arr.iter().rev() {
        assert_eq!(v.pop(), Some(*x));
    }
}
# of pushes/pops Verification time (s) Memory usage (MB)
1 0.9 54
2 1.1 64
3 1.3 73
4 1.5 83
5 1.7 94
6 2.0 104
7 2.2 114

So it seems that the UTF-8 encoding of strings significantly complicates the formulas.

Activity

  1. added
    [C] Feature / Enhancement
    [E] Performance
    on Dec 16, 2022
  2. self-assigned this
    on Mar 17, 2023
  3. tautschnig commented on Apr 20, 2023

    @tautschnig
    Member
  4. added
    T-CBMC
    on Aug 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

T-CBMC[C] Feature / Enhancement[E] Performance

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions