Skip to content

Commit ba6679d

Browse files
bors[bot]matklad
andauthored
Merge #6239
6239: Cleanup alloc advice r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 3a38554 + dedfaa3 commit ba6679d

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

docs/dev/style.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ fn frbonicate(f: impl AsRef<Path>) {
248248

249249
# Premature Pessimization
250250

251+
## Avoid Allocations
252+
251253
Avoid writing code which is slower than it needs to be.
252254
Don't allocate a `Vec` where an iterator would do, don't allocate strings needlessly.
253255

@@ -267,6 +269,8 @@ if words.len() != 2 {
267269
}
268270
```
269271

272+
## Push Allocations to the Call Site
273+
270274
If allocation is inevitable, let the caller allocate the resource:
271275

272276
```rust
@@ -282,6 +286,9 @@ fn frobnicate(s: &str) {
282286
}
283287
```
284288

289+
This is better because it reveals the costs.
290+
It is also more efficient when the caller already owns the allocation.
291+
285292
## Collection types
286293

287294
Prefer `rustc_hash::FxHashMap` and `rustc_hash::FxHashSet` instead of the ones in `std::collections`.

0 commit comments

Comments
 (0)