Skip to content

fix(treewide): clean up move semantics#11843

Merged
Mic92 merged 6 commits intoNixOS:masterfrom
xokdvium:dev/move-fixes
Nov 9, 2024
Merged

fix(treewide): clean up move semantics#11843
Mic92 merged 6 commits intoNixOS:masterfrom
xokdvium:dev/move-fixes

Conversation

@xokdvium
Copy link
Copy Markdown
Contributor

@xokdvium xokdvium commented Nov 8, 2024

Motivation

Running clang-tidy with the following config has revealed some incorrect/inefficient usage of move semantics.

Checks:
  - -*
  - performance-no-automatic-move
  - performance-move-constructor-init
  - performance-move-const-arg

These fixes are for the more egregious problems I was able to find. These pessimizations can pretty negatively affect the compiler codegen and the final performance.

Context

One more reason to enable regular clang-tidy runs: #11839

Priorities and Process

Add 👍 to pull requests you find important.

The Nix maintainer team uses a GitHub project board to schedule and track reviews.

`auto &&` and `T &&` are forwarding references and can be
either lvalue or rvalue references. Moving from universal references
is incorrect and should not be done.

Moving from integral or floating-point values is pointless and just
worsens debug performance.
Moving from arguments where it should be done.
BasicDerivation(BasicDerivation &&) = default;
BasicDerivation(const BasicDerivation &) = default;
BasicDerivation& operator=(BasicDerivation &&) = default;
BasicDerivation& operator=(const BasicDerivation &) = default;
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.

If all the constructors are default, can't we just omit them?

Copy link
Copy Markdown
Contributor Author

@xokdvium xokdvium Nov 8, 2024

Choose a reason for hiding this comment

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

Not really, thats the problem. Move ctors are deleted if the virtual dtor is defined

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Same with user-defined copy ctors for ref. A defined copy ctor deletes the move ctor/assignment operator. That's why it's better to not define anything by-hand if possible. In that case the compiler will do everything that the member types support.

@xokdvium
Copy link
Copy Markdown
Contributor Author

xokdvium commented Nov 8, 2024

I might have gotten a bit overzealous with the MAKE_WRAPPER_CONSTRUCTOR and it's fine as-is. Sorry for the confusion

@xokdvium
Copy link
Copy Markdown
Contributor Author

xokdvium commented Nov 8, 2024

Also pushed a fix for ValidPathInfo move ctor/assignment pessimization. I think this should be enough for now.

@Mic92 Mic92 merged commit aa9c0bc into NixOS:master Nov 9, 2024
@xokdvium xokdvium deleted the dev/move-fixes branch November 10, 2024 19:49
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.

3 participants