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.

Architect
Architect
Architect
Boost C++ Service Performance 3×: Class vs Protobuf, Cache‑Friendly Structures, jemalloc, and Lock‑Free Design

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.

Protobuf vs Class performance
Protobuf vs Class performance
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Architect
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.