File tree Expand file tree Collapse file tree 1 file changed +7
-0
lines changed Expand file tree Collapse file tree 1 file changed +7
-0
lines changed Original file line number Diff line number Diff line change @@ -248,6 +248,8 @@ fn frbonicate(f: impl AsRef<Path>) {
248
248
249
249
# Premature Pessimization
250
250
251
+ ## Avoid Allocations
252
+
251
253
Avoid writing code which is slower than it needs to be.
252
254
Don'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
+
270
274
If allocation is inevitable, let the caller allocate the resource:
271
275
272
276
``` rust
@@ -282,6 +286,9 @@ fn frobnicate(s: &str) {
282
286
}
283
287
```
284
288
289
+ This is better because it reveals the costs.
290
+ It is also more efficient when the caller already owns the allocation.
291
+
285
292
## Collection types
286
293
287
294
Prefer ` rustc_hash::FxHashMap ` and ` rustc_hash::FxHashSet ` instead of the ones in ` std::collections ` .
You can’t perform that action at this time.
0 commit comments