Make *-inl.h slim again
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
.ccfile - Functions over 4-5 lines that aren't template functions should live in a
.ccfile - Macros like
CHECK(..)expand to multiple lines of code and should be counted as such
Good (short, many callers):
| inline v8::Isolate* IsolateData::isolate() const { | |
| return isolate_; | |
| } |
Not good (big, only two infrequent callers):
| 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):
| 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