-
-
Notifications
You must be signed in to change notification settings - Fork 33.1k
Open
Labels
c++Issues and PRs that require attention from people who are familiar with C++.Issues and PRs that require attention from people who are familiar with C++.
Description
Inspired by me setting a breakpoint on a function and gdb telling me there are 12 locations...
The idea behind the *-inl.h
files is to provide inline definitions of short functions. Large gobs of code don't belong in them because they negatively impact:
- Compile times
- Binary size (duplication)
- Run-time performance (instruction cache pressure)
Rules of thumb:
- Functions with few or infrequent callers should (judgment call) live in a
.cc
file - Functions over 4-5 lines that aren't template functions should live in a
.cc
file - Macros like
CHECK(..)
expand to multiple lines of code and should be counted as such
Good (short, many callers):
Lines 56 to 58 in 7d13f5e
inline v8::Isolate* IsolateData::isolate() const { | |
return isolate_; | |
} |
Not good (big, only two infrequent callers):
Lines 349 to 365 in 7d13f5e
inline void Environment::AssignToContext(v8::Local<v8::Context> context, | |
const ContextInfo& info) { | |
context->SetAlignedPointerInEmbedderData( | |
ContextEmbedderIndex::kEnvironment, this); | |
// Used by Environment::GetCurrent to know that we are on a node context. | |
context->SetAlignedPointerInEmbedderData( | |
ContextEmbedderIndex::kContextTag, Environment::kNodeContextTagPtr); | |
// Used to retrieve bindings | |
context->SetAlignedPointerInEmbedderData( | |
ContextEmbedderIndex::kBindingListIndex, &(this->bindings_)); | |
#if HAVE_INSPECTOR | |
inspector_agent()->ContextCreated(context, info); | |
#endif // HAVE_INSPECTOR | |
this->async_hooks()->AddContext(context); | |
} |
Questionable (single infrequent caller):
Lines 510 to 518 in 7d13f5e
inline void Environment::TryLoadAddon( | |
const char* filename, | |
int flags, | |
const std::function<bool(binding::DLib*)>& was_loaded) { | |
loaded_addons_.emplace_back(filename, flags); | |
if (!was_loaded(&loaded_addons_.back())) { | |
loaded_addons_.pop_back(); | |
} | |
} |
The most commonly used *-inl.h
files are:
$ find src -type f | xargs perl -ne 'print "$1\n" if /#\s*include "([^"]+-inl.h)"/' | sort | uniq -c | sort -r | head -9
91 env-inl.h
74 util-inl.h
52 memory_tracker-inl.h
42 base_object-inl.h
35 async_wrap-inl.h
29 debug_utils-inl.h
19 threadpoolwork-inl.h
18 node_process-inl.h
15 stream_base-inl.h
legendecas, benjamingr and F3n67u
Metadata
Metadata
Assignees
Labels
c++Issues and PRs that require attention from people who are familiar with C++.Issues and PRs that require attention from people who are familiar with C++.