Avoid unnecessary memory allocations, copies, and inefficient memory usage patterns that can impact performance. This includes several key practices:
Avoid unnecessary allocations:
static NullIoStream globalNullStream;
instead of static auto globalNullStream = kj::heap<NullIoStream>();
kj::str(...)
when the result is immediately wrappedOptimize parameter passing:
const tracing::TraceId& traceId
instead of tracing::TraceId traceId
kj::mv()
can interfere with NRVO (Named Return Value Optimization)Pre-allocate when size is known:
// Instead of letting vector grow dynamically
kj::Vector<kj::Promise<void>> promises;
// Reserve space when size is known
promises.reserve(filenames.size());
Choose efficient memory usage patterns:
readAllBytes()
that buffers unnecessarilykj::Path({"tmp"})
instead of kj::Path::parse("tmp")
These optimizations are particularly important in hot paths or when processing large amounts of data, as small inefficiencies can compound significantly.
Enter the URL of a public GitHub repository