aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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 bb7a351f3..bb99c4855 100644
--- a/docs/dev/style.md
+++ b/docs/dev/style.md
@@ -255,6 +255,41 @@ if words.len() != 2 {
255} 255}
256``` 256```
257 257
258# Avoid Monomorphization
259
260Rust 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*.
261This allows for exceptionally good performance, but leads to increased compile times.
262Runtime performance obeys 80%/20% rule -- only a small fraction of code is hot.
263Compile time **does not** obey this rule -- all code has to be compiled.
264For this reason, avoid making a lot of code type parametric, *especially* on the boundaries between crates.
265
266```rust
267// Good
268fn frbonicate(f: impl FnMut()) {
269 frobnicate_impl(&mut f)
270}
271fn frobnicate_impl(f: &mut dyn FnMut()) {
272 // lots of code
273}
274
275// Not as good
276fn frbonicate(f: impl FnMut()) {
277 // lots of code
278}
279```
280
281Avoid `AsRef` polymorphism, it pays back only for widely used libraries:
282
283```rust
284// Good
285fn frbonicate(f: &Path) {
286}
287
288// Not as good
289fn frbonicate(f: impl AsRef<Path>) {
290}
291```
292
258# Documentation 293# Documentation
259 294
260For `.md` and `.adoc` files, prefer a sentence-per-line format, don't wrap lines. 295For `.md` and `.adoc` files, prefer a sentence-per-line format, don't wrap lines.