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

add a small vector to the standard library #4991

Closed
thestinger opened this issue Feb 17, 2013 · 13 comments
Closed

add a small vector to the standard library #4991

thestinger opened this issue Feb 17, 2013 · 13 comments
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. I-slow Issue: Problems and improvements with respect to performance of generated code. P-low Low priority

Comments

@thestinger
Copy link
Contributor

It's often useful to have a vector optimized for storing small sequences. LLVM includes a SmallVector templated type which takes the size to store inside the struct as a template parameter, which isn't possible at the moment in Rust. However, it would be fine to just copy what libc++ does for std::string which is to store 3 words in the struct (size, capacity, and the pointer) and reuse that space for the small string optimization.

@catamorphism
Copy link
Contributor

Added the An-interesting-project label.

@abhijeetgaiha
Copy link

@catamorphism @thestinger
Just trying to get a feel for what is required here...
This works much like a ~[], except we will allocate space for N elements during initialisation.
How is this different from creating a ~[] and calling reserve()?

@huonw
Copy link
Member

huonw commented Jul 18, 2013

@abhijeetgaiha it's more subtle than that, it's effectively:

enum SmallVector<T> {
   Small(uint, [T, .. 8]), // size, data (the 8 shouldn't be hardcoded like this)
   Big(uint, uint, *T) // capacity, size, pointer
}

(But not exactly, a Rust implementation should try to leverage LLVM's support.)

I.e. it is a value with size 3 words, and if there are a small number of elements they are stored directly in the value, not behind a pointer-indirection.

@thestinger
Copy link
Contributor Author

@huonw: LLVM's type is just a templated C++ container that they use heavily throughout their codebase, it's not really useful to us.

@abhijeetgaiha
Copy link

@huonw @thestinger
The bug body makes a little more sense now.

Looks like you are suggesting we have the following struct
SmallVector {
big:bool,
size:uint,
capacity:uint,
ptr: *T
}

For small values, we set big to false and re-use the space reserved for size, capacity and ptr for storage.
If we exceed that, we set big to true and allocate some space to ptr and move the elements there.

Am I following you correctly?

@huonw
Copy link
Member

huonw commented Jul 19, 2013

@abhijeetgaiha I think the best-case scenario would be SmallVector { size: uint, capacity: uint, ptr: *T }, and (for example) use the high bit of size to indicate whether the data is stored in the two other words, or if it is behind ptr.

(Note that you still need to indicate the size of the data when on the stack, so size cannot be (entirely) used to store data in that case.)

@abhijeetgaiha
Copy link

@huonw Thanks for the input. I'm going to make a initial attempt at this.

@thestinger
Copy link
Contributor Author

This is still unimplemented and would be very useful, but the lack of integers in the type system makes the concept much less powerful.

@erickt
Copy link
Contributor

erickt commented Dec 8, 2013

I just saw that there is a syntax::util::small_vector that optimizes for 0, 1, or many items cases. It'd be a nice stopgap between now and if/when we add integers to the type system.

@thestinger
Copy link
Contributor Author

@erickt: It should be doing one branch for a small case (3+ words) and a large case and ideally avoiding the overhead of using an enum. A sane vector type is already not allocating for the 0 element case.

@pnkfelix
Copy link
Member

This need not block 1.0. @pcwalton has one in Servo that we could copy over if need be. Assigning P-low.

@Gankra
Copy link
Contributor

Gankra commented Sep 16, 2014

CC #16998 as a sister effort

@thestinger
Copy link
Contributor Author

clearly a feature request

bors added a commit to rust-lang-ci/rust that referenced this issue May 2, 2020
…1995

Add suggestions for `if_let_some_result`

Fixes rust-lang#4991

This approach may be fragile though...

changelog: Add suggestions for `if_let_some_result`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. I-slow Issue: Problems and improvements with respect to performance of generated code. P-low Low priority
Projects
None yet
Development

No branches or pull requests

7 participants