diff options
| author | Aleksey Kladov <aleksey.kladov@gmail.com> | 2020-10-15 12:21:38 +0200 |
|---|---|---|
| committer | Aleksey Kladov <aleksey.kladov@gmail.com> | 2020-10-15 12:21:38 +0200 |
| commit | dedfaa384408b266c461fea45fee936989bb151b (patch) | |
| tree | 626a8f27624110b9de1d75bc3e14c3d9216b5416 /docs/dev/style.md | |
| parent | 3a38554f86e5e1b41b111ed8ccc688e84a9d5ae4 (diff) | |
| download | rust-dedfaa384408b266c461fea45fee936989bb151b.tar.gz rust-dedfaa384408b266c461fea45fee936989bb151b.zip | |
Cleanup alloc advice
Diffstat (limited to 'docs/dev/style.md')
| -rw-r--r-- | docs/dev/style.md | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/docs/dev/style.md b/docs/dev/style.md index 435de63e3bd..20f1b6253e7 100644 --- a/docs/dev/style.md +++ b/docs/dev/style.md @@ -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`. |
