6239: Cleanup alloc advice r=matklad a=matklad

bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2020-10-15 10:22:01 +00:00 committed by GitHub
commit ba6679dc6c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -248,6 +248,8 @@ fn frbonicate(f: impl AsRef<Path>) {
# Premature Pessimization
## Avoid Allocations
Avoid writing code which is slower than it needs to be.
Don't allocate a `Vec` where an iterator would do, don't allocate strings needlessly.
@ -267,6 +269,8 @@ if words.len() != 2 {
}
```
## Push Allocations to the Call Site
If allocation is inevitable, let the caller allocate the resource:
```rust
@ -282,6 +286,9 @@ fn frobnicate(s: &str) {
}
```
This is better because it reveals the costs.
It is also more efficient when the caller already owns the allocation.
## Collection types
Prefer `rustc_hash::FxHashMap` and `rustc_hash::FxHashSet` instead of the ones in `std::collections`.