Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eliminate inline keywords #803

Closed
hollasch opened this issue Nov 13, 2020 · 2 comments
Closed

Eliminate inline keywords #803

hollasch opened this issue Nov 13, 2020 · 2 comments
Assignees
Milestone

Comments

@hollasch
Copy link
Collaborator

Our codebase has a bunch of uses of the inline keyword. This used to be useful with early compilers, but now is just extra fluff to read. (I just ran a perf suite on all renderings and there's no change with or without inline hints). We should eliminate these.

@hollasch hollasch added this to the v4.0.0 milestone Nov 13, 2020
@hollasch hollasch self-assigned this Nov 13, 2020
@hollasch
Copy link
Collaborator Author

Sigh. I fell for it. In C++ inline does two completely different things:

  1. It poorly hints to the compiler to perform the inline optimization (eliminate the function call overhead, inject the function code directly as appropriate, and/or maybe perform broader optimizations of the code in the context of the larger function. I say "poorly" because at this point compilers can determine if this is a Good Thing better than you can, and on the universe of compilers and platforms and compilation switches et cetera.

  2. You know how in C++ static means like six different things that are only vaguely related to the actual word static? Well, inline is like that too. It tells the linker that it may encounter a function definition in multiple translation units. I believe that it's undefined which function definition wins, but there's anecdotal evidence that the first definition wins. In other words, if the linker sees inline int foo { return 1; } and inline int foo { return 2; }, it may always use the first and discard the second.

For our code as implemented it doesn't matter, since each program has a single translation unit, starting at main.cc. However, if a reader chose to do their implementation with headers and source files separated, then without the inline keyword, they'd start seeing multiple function definitions, and the associated errors from that.

For this reason, common functions outside of classes should get the inline keyword. As an alternate implementation, we might choose to define a common utility function as a static function inside a related class. In this scenario, classes operate as de facto namespaces, and the method definition inside a class definition functions in the same way that the inline keyword does for functions outside of classes.

@hollasch
Copy link
Collaborator Author

Oops, forgot to include some very helpful links.

https://stackoverflow.com/a/1759575/566185
https://stackoverflow.com/a/45710235/566185

@hollasch hollasch mentioned this issue Nov 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant