Skip to content

Use #[inline] everywhere.#145

Merged
phil-opp merged 1 commit into
rust-osdev:masterfrom
m-ou-se:inline
Apr 11, 2020
Merged

Use #[inline] everywhere.#145
phil-opp merged 1 commit into
rust-osdev:masterfrom
m-ou-se:inline

Conversation

@m-ou-se

@m-ou-se m-ou-se commented Apr 11, 2020

Copy link
Copy Markdown
Contributor

I tried to follow the following rules for determining if something
should be #[inline]:

  • Functions that are trivial. (E.g. implementations of Add, or a
    trivial new function.)
  • Functions that do not appear trivial, but would generate trivial
    assembly. (E.g. functions with mostly Rust code that don't result much
    generated code: use statements, wrapping values, bit twiddling,
    #[cfg], PhantomData, etc.)
  • Functions that only call another function.
  • Functions that could benefit a lot from inline optimization:
    • Functions that have asserts/matches/ifs on the parameters, which are
      likely constant or known to be in a certain range:
      If a function starts with assert!(param < 32), inlining enables
      the compiler to completely optimize the check away if it already
      knows the value to always be less than 32.
      If a function matches on a parameter, inlining allows throwing all
      but one match arm away.
  • Functions where knowing the return value could benefit optimization on
    later calls/code.

I tried to follow the following rules for determining if something
should be #[inline]:

- Functions that are trivial. (E.g. implementations of `Add`, or a
  trivial `new` function.)
- Functions that do not appear trivial, but would generate trivial
  assembly. (E.g. functions with mostly Rust code that don't result much
  generated code: `use` statements, wrapping values, bit twiddling,
  `#[cfg]`, `PhantomData`, etc.)
- Functions that only call another function.
- Functions that could benefit a lot from inline optimization:
  - Functions that have asserts/matches/ifs on the parameters, which are
    likely constant or known to be in a certain range:
    If a function starts with `assert!(param < 32)`, inlining enables
    the compiler to completely optimize the check away if it already
    knows the value to always be less than 32.
    If a function matches on a parameter, inlining allows throwing all
   but one match arm away.
- Functions where knowing the return value could benefit optimization on
  later calls/code.

@phil-opp phil-opp left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for this! I have one small question about a removed inline, otherwise this looks good to me!

Comment thread src/structures/paging/frame.rs
@phil-opp phil-opp merged commit 855760e into rust-osdev:master Apr 11, 2020
phil-opp added a commit that referenced this pull request Apr 11, 2020
@m-ou-se m-ou-se deleted the inline branch April 11, 2020 12:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants