# Inlining non-generic call chains cross crates

**URL:** <https://users.rust-lang.org/t/inlining-non-generic-call-chains-cross-crates/55858>\
**Category:** uncategorized\
**Created:** [February 19, 2021, 7:15pm UTC](https://users.rust-lang.org/t/inlining-non-generic-call-chains-cross-crates/55858 "2021-02-19T19:15:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![matklad](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/matklad/32/41370_2.png) [@matklad](https://users.rust-lang.org/u/matklad)\
**Post date:** [February 19, 2021, 7:15pm UTC](https://users.rust-lang.org/t/inlining-non-generic-call-chains-cross-crates/55858/1 "2021-02-19T19:15:12Z")

</div>

Lets' say I have the following code:

```rust
pub fn small_foo() {
  small_bar()
}

fn small_bar() {
   // ...
}

```

The way `rustc` works today, if I want to enable inlining of `small_foo` across the crates, I need to at least add `#[inline] pub fn small_foo()`. Question: do I need `#[inline] pub fn small_bar()` as well?

From my fuzzy recollections of various profiling session, I believe that the answer is generally "yes". Is the definitive answer documented somewhere? Are there any ready benchmarks I can run myself?

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/cad97/32/5076_2.png) [@CAD97](https://users.rust-lang.org/u/CAD97)\
**Post date:** [February 19, 2021, 7:36pm UTC](https://users.rust-lang.org/t/inlining-non-generic-call-chains-cross-crates/55858/2 "2021-02-19T19:36:31Z")

</div>

A quick test shows that for truly trivial cases (like `a + b` trivial), you get a full inline.

I was getting some strange output from `cargo asm`, though, so I don't know how much I trust it.

My guess from observations: one of two cases are true; in addition to the LLVM `inlinehint`, either

- `#[inline]` gives you access to the MIR of the annotated function, but no more, meaning calls to unannotated functions are LLVM inlining barriers; you get MIR optimization but not LLVM; MIR inlining is working for some truly trivial cases; or
- `#[inline]` gets you the full locally optimized version of the function, after LLVM inlining; this only considers locally known information in the subcrate; knowledge about the arguments cannot be used to drive further inlining through non-`#[inline]` calls, even if the knowledge would change inlining decisions.

I, of course, have no actual knowledge of what's going on, and this is just an educated guess that I had fun constructing.

---

<div class="post-metadata">

**Author:** ![nologik](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/nologik/32/23342_2.png) [@nologik](https://users.rust-lang.org/u/nologik)\
**Post date:** [February 19, 2021, 7:50pm UTC](https://users.rust-lang.org/t/inlining-non-generic-call-chains-cross-crates/55858/3 "2021-02-19T19:50:12Z")

</div>

This makes sense because nested inlining by default could lead to explosions in binary size, which would ultimately be an undesirable feature unless for the trivial cases

---

<div class="post-metadata">

**Author:** ![matklad](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/matklad/32/41370_2.png) [@matklad](https://users.rust-lang.org/u/matklad)\
**Post date:** [February 25, 2021, 10:41am UTC](https://users.rust-lang.org/t/inlining-non-generic-call-chains-cross-crates/55858/4 "2021-02-25T10:41:34Z")

</div>

Experimented myself:

[https://github.com/matklad/rust-inline](https://github.com/matklad/rust-inline)

The answer appears to be "you need both `#[inline]`s" ☹

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [May 26, 2021, 10:42am UTC](https://users.rust-lang.org/t/inlining-non-generic-call-chains-cross-crates/55858/5 "2021-05-26T10:42:10Z")

</div>

Closed on May 26, 2021, 10:42am UTC

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
