]> git.lizzy.rs Git - rust.git/commitdiff
Cleanup alloc advice
authorAleksey Kladov <aleksey.kladov@gmail.com>
Thu, 15 Oct 2020 10:21:38 +0000 (12:21 +0200)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Thu, 15 Oct 2020 10:21:38 +0000 (12:21 +0200)
docs/dev/style.md

index 435de63e3bd4ec63871bd5e3bb8a69501d869b62..20f1b6253e7fba40d3d85a57718e4b884e97629c 100644 (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`.