>_ DevTrendsja

言語

ホーム

言語

セクション

フロントエンド バックエンド モバイル DevOps AI / ML ゲーム開発 ブロックチェーン 組み込み セキュリティ
C-plus-plus

How Google customizes the standard C++ library for its needs

Imagine writing code for systems that process billions of requests. The C++ Standard Library (STL) is good, but sometimes its universality becomes a bottleneck. Somewhere there's a lack of hash table performance, somewhere convenient date handling is missing, and some features from new C++20 or C++23 standards you want to use right now, without waiting for compiler updates on all servers.

That's exactly how Abseil came about. It's not a replacement for STL, but an extension that Google uses in its internal projects like Search, Ads, and Maps. Developers simply took code proven over years and released it as open source.

What's inside this Swiss Army knife

If you look into the abseil-cpp repository, you can find dozens of libraries. But I'll highlight those that really change everyday development.

Swiss Tables: when std::unordered_map is too slow

In Abseil there are container families absl::flat_hash_map and absl::flat_hash_set. Google calls them "Swiss tables". Unlike standard std::unordered_map, which uses linked lists for collision resolution, Abseil uses open addressing and SIMD instructions for lookup. In practice, this provides a noticeable speed boost and memory savings due to better cache locality.

Smart string handling

The strings library is a lifesaver for those tired of writing boilerplate for basic tasks. For example, absl::StrCat and absl::StrAppend work faster than the usual + or std::stringstream operators because they pre-calculate the required buffer size and avoid unnecessary allocations. And absl::StrSplit lets you split strings with just one line of code with flexible filters.

Error handling without exceptions

Many large projects disable exceptions in C++ due to overhead or security concerns. In Abseil there's absl::Status and absl::StatusOr<T> for this. This is an elegant way to return either a result or an error code with text description from a function. If you've ever worked with Go or Rust, this approach will seem very familiar and logical to you.

Synchronization and time

Instead of the standard std::mutex, they offer absl::Mutex. It's designed to be more efficient under high thread contention. And the time library makes working with time zones and intervals much simpler than the overloaded std::chrono.

Technical philosophy: Live at Head

Abseil has one characteristic that can either repel or attract. Google promotes the "Live at Head" concept. This means they recommend always building the project from the latest master version.

Developers strive not to break the API, but if it's necessary to improve the library, they will do it. To make life easier for users, Abseil ships with tools for automatic code refactoring to new API versions. For those not ready for such adventures, there are LTS releases, but the main focus is on moving forward.

The project supports building with Bazel and CMake. If you're already using Bazel, integrating Abseil will take just a couple of minutes.

Who this is for

I'd recommend taking a closer look at Abseil in two cases.

First, if you're writing a high-load backend. Replacing standard maps with absl::flat_hash_map sometimes gives a performance boost for free, without rewriting logic.

Second, if you're stuck on an old C++ standard (for example, C++17), but really want to use features from C++20. Abseil often backports new language capabilities, making them available on older compilers.

Abseil is not just a collection of utilities, but a reflection of how code is written at one of the most technologically advanced companies in the world. The library is huge, sometimes complex, but incredibly high-quality. If you need more than what the standard library offers or you're fighting for every percent of performance, this is definitely a tool worth having in your arsenal.

The best way to start learning is with the official guide, since the README in the repository is quite brief and serves more as a map of the project folders.

関連プロジェクト