aboutsummaryrefslogtreecommitdiff
path: root/docs/dev/style.md
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-10-15 11:22:01 +0100
committerGitHub <[email protected]>2020-10-15 11:22:01 +0100
commitba6679dc6ce7065d349ac4d93ab86826b13e15a5 (patch)
tree626a8f27624110b9de1d75bc3e14c3d9216b5416 /docs/dev/style.md
parent3a38554f86e5e1b41b111ed8ccc688e84a9d5ae4 (diff)
parentdedfaa384408b266c461fea45fee936989bb151b (diff)
Merge #6239
6239: Cleanup alloc advice r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'docs/dev/style.md')
-rw-r--r--docs/dev/style.md7
1 files changed, 7 insertions, 0 deletions
diff --git a/docs/dev/style.md b/docs/dev/style.md
index 435de63e3..20f1b6253 100644
--- a/docs/dev/style.md
+++ b/docs/dev/style.md
@@ -248,6 +248,8 @@ fn frbonicate(f: impl AsRef<Path>) {
248 248
249# Premature Pessimization 249# Premature Pessimization
250 250
251## Avoid Allocations
252
251Avoid writing code which is slower than it needs to be. 253Avoid writing code which is slower than it needs to be.
252Don't allocate a `Vec` where an iterator would do, don't allocate strings needlessly. 254Don't allocate a `Vec` where an iterator would do, don't allocate strings needlessly.
253 255
@@ -267,6 +269,8 @@ if words.len() != 2 {
267} 269}
268``` 270```
269 271
272## Push Allocations to the Call Site
273
270If allocation is inevitable, let the caller allocate the resource: 274If allocation is inevitable, let the caller allocate the resource:
271 275
272```rust 276```rust
@@ -282,6 +286,9 @@ fn frobnicate(s: &str) {
282} 286}
283``` 287```
284 288
289This is better because it reveals the costs.
290It is also more efficient when the caller already owns the allocation.
291
285## Collection types 292## Collection types
286 293
287Prefer `rustc_hash::FxHashMap` and `rustc_hash::FxHashSet` instead of the ones in `std::collections`. 294Prefer `rustc_hash::FxHashMap` and `rustc_hash::FxHashSet` instead of the ones in `std::collections`.