abonander / buf_redux

A drop-in replacement for Rust's std::io::BufReader, with extra features
Apache License 2.0
38 stars 16 forks source link

Use Vec::resize #1

Closed bluss closed 8 years ago

bluss commented 8 years ago

On line https://github.com/cybergeek94/buf_redux/blob/07bc5f90bb7ead27853f86349a6a999c7e931229/src/lib.rs#L129 you can use vec.resize instead, to grow & fill by zeros. It generates much better code.

bluss commented 8 years ago

Oh, since this is used in new/with_capacity, it should speed up initial creation of the buf reader pretty significantly.

abonander commented 8 years ago

I don't like .resize() because it uses .reserve() internally, which allocates in powers of two. That's great for frequent growth but it seems too greedy in this case. Calling .shrink_to_fit() afterwards is even more wasteful as it causes another reallocation.

If the iterator-based zeroing isn't generating good code, we can manually lower to a call to .set_len() and ptr::write_bytes() at the cost of adding more unsafe blocks.

It's too bad there's no way to directly call calloc without reinventing a lot of the wheel.

abonander commented 8 years ago

Or is getting rid of the powers-of-two allocation totally misguided?

bluss commented 8 years ago

There's just a small misunderstanding: reserve only allocates if you need more space -- since you reserve enough upfront you are fine. .extend() will use reserve as well, yet it does nothing in this case for the same reason.

abonander commented 8 years ago

See, that's the part I missed. Yeah, sounds fine to me. I'll patch it in right now.

abonander commented 8 years ago

FIxed in 0.1.3