Skip to content

Make *-inl.h slim again #43712

Open
Open
@bnoordhuis

Description

@bnoordhuis

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:

  1. Compile times
  2. Binary size (duplication)
  3. Run-time performance (instruction cache pressure)

Rules of thumb:

  1. Functions with few or infrequent callers should (judgment call) live in a .cc file
  2. Functions over 4-5 lines that aren't template functions should live in a .cc file
  3. Macros like CHECK(..) expand to multiple lines of code and should be counted as such

Good (short, many callers):

node/src/env-inl.h

Lines 56 to 58 in 7d13f5e

inline v8::Isolate* IsolateData::isolate() const {
return isolate_;
}

Not good (big, only two infrequent callers):

node/src/env-inl.h

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):

node/src/env-inl.h

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    c++Issues and PRs that require attention from people who are familiar with C++.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions