Boost C++ Service Performance 3×: Class vs Protobuf, Cache‑Friendly Structures, jemalloc, and Lock‑Free Design
This article analyzes practical C++ performance optimizations—replacing Protobuf with native classes, adopting cache‑friendly structures, switching to jemalloc/tcmalloc, employing lock‑free double buffers, and tailoring data formats—backed by concrete code samples, benchmark results (up to 3× speedup), and tool recommendations.
1. Replace Protobuf with native C++ classes
Protobuf uses an arena allocator that creates many small heap objects, leading to memory fragmentation and slower destructor calls, especially for string fields. Re‑implementing the same message as a plain C++ class with explicit constructors, setters, and a Clear method removes the arena overhead.
class ParamHitInfo {
public:
class Param {
public:
Param() = default;
~Param() = default;
const std::string& name() const { return name_; }
void set_name(const std::string& name) { name_ = name; }
const std::string& value() const { return value_; }
void set_value(const std::string& value) { value_ = value; }
void clear_name() { name_.clear(); }
void clear_value() { value_.clear(); }
void Clear() { clear_name(); clear_value(); }
private:
std::string name_, value_;
};
// other fields omitted for brevity
};A GoogleTest benchmark creates 1,000 ParamHit protobuf objects and 1,000 ParamHitInfo class objects, copies each 1,000 times, and measures execution time. The class version is roughly three times faster, confirming the claim.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Architect
Professional architect sharing high‑quality architecture insights. Topics include high‑availability, high‑performance, high‑stability architectures, big data, machine learning, Java, system and distributed architecture, AI, and practical large‑scale architecture case studies. Open to ideas‑driven architects who enjoy sharing and learning.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
