aboutsummaryrefslogtreecommitdiff
path: root/docs/dev/style.md
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-08-24 11:50:04 +0100
committerGitHub <[email protected]>2020-08-24 11:50:04 +0100
commit6eddcfd7a5e62c5e0b188fcdb084b8147cd7c62c (patch)
treebf0d57ad3c6ba18bff21cfdc3a2eb97784a764d7 /docs/dev/style.md
parente65d48d1fb3d4d91d9dc1148a7a836ff5c9a3c87 (diff)
parentfc3e591bdb28745ef29e90de2c1625e6d4e4a229 (diff)
Merge #5853
5853: Avoid monomorphization 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.md35
1 files changed, 35 insertions, 0 deletions
diff --git a/docs/dev/style.md b/docs/dev/style.md
index 44f0956c2..ae69cf1a7 100644
--- a/docs/dev/style.md
+++ b/docs/dev/style.md
@@ -231,6 +231,41 @@ if words.len() != 2 {
231} 231}
232``` 232```
233 233
234# Avoid Monomorphization
235
236Rust uses monomorphization to compile generic code, meaning that for each instantiation of a generic functions with concrete types, the function is compiled afresh, *per crate*.
237This allows for exceptionally good performance, but leads to increased compile times.
238Runtime performance obeys 80%/20% rule -- only a small fraction of code is hot.
239Compile time **does not** obey this rule -- all code has to be compiled.
240For this reason, avoid making a lot of code type parametric, *especially* on the boundaries between crates.
241
242```rust
243// Good
244fn frbonicate(f: impl FnMut()) {
245 frobnicate_impl(&mut f)
246}
247fn frobnicate_impl(f: &mut dyn FnMut()) {
248 // lots of code
249}
250
251// Not as good
252fn frbonicate(f: impl FnMut()) {
253 // lots of code
254}
255```
256
257Avoid `AsRef` polymorphism, it pays back only for widely used libraries:
258
259```rust
260// Good
261fn frbonicate(f: &Path) {
262}
263
264// Not as good
265fn frbonicate(f: impl AsRef<Path>) {
266}
267```
268
234# Documentation 269# Documentation
235 270
236For `.md` and `.adoc` files, prefer a sentence-per-line format, don't wrap lines. 271For `.md` and `.adoc` files, prefer a sentence-per-line format, don't wrap lines.