mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-27 05:23:24 +00:00
Cleanup alloc advice
This commit is contained in:
parent
3a38554f86
commit
dedfaa3844
1 changed files with 7 additions and 0 deletions
|
@ -248,6 +248,8 @@ fn frbonicate(f: impl AsRef<Path>) {
|
||||||
|
|
||||||
# Premature Pessimization
|
# Premature Pessimization
|
||||||
|
|
||||||
|
## Avoid Allocations
|
||||||
|
|
||||||
Avoid writing code which is slower than it needs to be.
|
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.
|
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:
|
If allocation is inevitable, let the caller allocate the resource:
|
||||||
|
|
||||||
```rust
|
```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
|
## Collection types
|
||||||
|
|
||||||
Prefer `rustc_hash::FxHashMap` and `rustc_hash::FxHashSet` instead of the ones in `std::collections`.
|
Prefer `rustc_hash::FxHashMap` and `rustc_hash::FxHashSet` instead of the ones in `std::collections`.
|
||||||
|
|
Loading…
Reference in a new issue