diff options
| author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-08-24 10:50:04 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-08-24 10:50:04 +0000 |
| commit | 6eddcfd7a5e62c5e0b188fcdb084b8147cd7c62c (patch) | |
| tree | bf0d57ad3c6ba18bff21cfdc3a2eb97784a764d7 /docs/dev | |
| parent | e65d48d1fb3d4d91d9dc1148a7a836ff5c9a3c87 (diff) | |
| parent | fc3e591bdb28745ef29e90de2c1625e6d4e4a229 (diff) | |
| download | rust-6eddcfd7a5e62c5e0b188fcdb084b8147cd7c62c.tar.gz rust-6eddcfd7a5e62c5e0b188fcdb084b8147cd7c62c.zip | |
Merge #5853
5853: Avoid monomorphization r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
Diffstat (limited to 'docs/dev')
| -rw-r--r-- | docs/dev/style.md | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/docs/dev/style.md b/docs/dev/style.md index 44f0956c247..ae69cf1a743 100644 --- a/docs/dev/style.md +++ b/docs/dev/style.md @@ -231,6 +231,41 @@ if words.len() != 2 { } ``` +# Avoid Monomorphization + +Rust 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*. +This allows for exceptionally good performance, but leads to increased compile times. +Runtime performance obeys 80%/20% rule -- only a small fraction of code is hot. +Compile time **does not** obey this rule -- all code has to be compiled. +For this reason, avoid making a lot of code type parametric, *especially* on the boundaries between crates. + +```rust +// Good +fn frbonicate(f: impl FnMut()) { + frobnicate_impl(&mut f) +} +fn frobnicate_impl(f: &mut dyn FnMut()) { + // lots of code +} + +// Not as good +fn frbonicate(f: impl FnMut()) { + // lots of code +} +``` + +Avoid `AsRef` polymorphism, it pays back only for widely used libraries: + +```rust +// Good +fn frbonicate(f: &Path) { +} + +// Not as good +fn frbonicate(f: impl AsRef<Path>) { +} +``` + # Documentation For `.md` and `.adoc` files, prefer a sentence-per-line format, don't wrap lines. |
