You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If a function has a `bool` or an `Option` parameter, and it is always called with `true`, `false`, `Some` and `None` literals, split the function in two.
455
+
456
+
```rust
457
+
// GOOD
458
+
fncaller_a() {
459
+
foo()
460
+
}
461
+
462
+
fncaller_b() {
463
+
foo_with_bar(Bar::new())
464
+
}
465
+
466
+
fnfoo() { ... }
467
+
fnfoo_with_bar(bar:Bar) { ... }
468
+
469
+
// BAD
470
+
fncaller_a() {
471
+
foo(None)
472
+
}
473
+
474
+
fncaller_b() {
475
+
foo(Some(Bar::new()))
476
+
}
477
+
478
+
fnfoo(bar:Option<Bar>) { ... }
479
+
```
480
+
481
+
**Rationale:** more often than not, such functions display "`false sharing`" -- they have additional `if` branching inside for two different cases.
482
+
Splitting the two different control flows into two functions simplifies each path, and remove cross-dependencies between the two paths.
483
+
If there's common code between `foo` and `foo_with_bar`, extract *that* into a common helper.
484
+
452
485
## Avoid Monomorphization
453
486
454
487
Avoid making a lot of code type parametric, *especially* on the boundaries between crates.
0 commit comments