Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BlobVec::push is linear #10797

Closed
stepancheg opened this issue Nov 29, 2023 · 0 comments · Fixed by #11167
Closed

BlobVec::push is linear #10797

stepancheg opened this issue Nov 29, 2023 · 0 comments · Fixed by #11167
Labels
A-ECS Entities, components, systems, and events C-Bug An unexpected or incorrect behavior C-Performance A change motivated by improving speed, memory usage or compile times

Comments

@stepancheg
Copy link
Contributor

    #[test]
    fn test_quadratic() {
        unsafe {
            let mut vec = BlobVec::new(Layout::new::<u64>(), None, 0);
            for i in 0..100_000_000 {
                if i % 1_000_000 == 0 {
                    println!("{i}");
                }
                OwningPtr::make(i, |ptr| {
                    vec.push(ptr);
                });
                hint::black_box(&mut vec);
            }
        }
    }

    #[test]
    fn test_quadratic_vec() {
        let mut vec = Vec::new();
        for i in 0..100_000_000 {
            if i % 1_000_000 == 0 {
                println!("{i}");
            }
            vec.push(i);
            hint::black_box(&mut vec);
        }
    }

Second test finishes in a second.

First test never finishes, and shows it gets slower with each iteration.

It is not noticable in typical application because:

  • reserve_exact before push (which is how BlobVec is used) makes effect less visible (yet keeping insertion kind of quadratic)
  • on small BlobVec realloc often reallocates in place or reallocates to double size, making push effectively constant even with unnecessary call to realloc. But on larger allocations malloc no longer doubles the size making push very expensive.
@stepancheg stepancheg added C-Bug An unexpected or incorrect behavior S-Needs-Triage This issue needs to be labelled labels Nov 29, 2023
@ItsDoot ItsDoot added A-ECS Entities, components, systems, and events C-Performance A change motivated by improving speed, memory usage or compile times and removed S-Needs-Triage This issue needs to be labelled labels Nov 30, 2023
github-merge-queue bot pushed a commit that referenced this issue Jan 22, 2024
# Objective

- Fixes #10797

## Solution

- Double the capacity of a full `BlobVec` before pushing a new element.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events C-Bug An unexpected or incorrect behavior C-Performance A change motivated by improving speed, memory usage or compile times
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants