Build `folly::coro` with GCC

You have heard about Coroutine in C++, and you want to use it. There're two coroutine implementations that are considered most mature - cppcoro and folly::coro. They are written by the same guy - Lewis Baker. He's brilliant, and you shoud watch his cppcon talk on structured concurrency if…

How `AUTO_INCREMENT` works on MySQL

You have created tables with AUTO_INCREMENT. Something like CREATE TABLE user ( user_id INT(11) NOT NULL AUTO_INCREMENT, ... ) MySQL manages user_id for you automatically. You get user_id 1, 2, 3, 4, ... But how does it work? InnoDB, one of the most common storage engine MySQL uses,…

MyRocks and Repeatable Read Isolation

tl;dr: Repeatable read isolation on MyRocks is effectively Snapshot isolation (unless it's affecting an unique index). According to MySQL reference, REPEATABLE READ This is the default isolation level for InnoDB. Consistent reads within the same transaction read the snapshot established by the first read. This means that if you…

SeqLock

Sequential lock is a common technique used to protect data that's frequently read and rarely updated. Writers are required to take exclusive lock to mutate the data. Reads are effectively lock free and optimistic. The data protected by a SeqLock usually needs to be trivially_copy_constructible. // reader std::atomoc&…

`folly::Indestructible`

folly::Indestructible<T> is a class template that makes a static variable well, indestructible. Notice that it's meant for static variables in the Meyers Singleton pattern. If it's for heap allocated memory, it would just be called memory leak instead of "indestructible". It boils down to making a…

C++ Map Lookup Memoization

Memoization is an old technique. It's basically caching outputs for giving inputs (usually in a map). But a map lookup itself is not free. How can we memoize map lookups? I learned from my coworker this nifty trick recently. Let's say we have a map (e.g. std::unordered_map&…

C++ Type Erasure

You have heard about Type Erasure of C++. You probably know std::function is a classic usage of the pattern. But what is Type Erasure in C++? It's effectively trying to achieve Duck Typing, which is common in languages like Python. # Notice that there's no relationship between Bar1 or Bar2…

atomic_thread_fence

Just like you can have a std::atomic synchronizes two threads with each other with release-acquire semantic, you can also have Fence-Fence, Atomic-Fence, Fence-Atomic synchronizations. C++ reference has very detailed documentation about when there exists a valid synchronizes-with relationship, https://en.cppreference.com/w/cpp/atomic/atomic_thread_fence. Rust's…

Paxos Commit with Constraints

Correction: The following description of Paxos Commit is inaccurate. The algorithm is still correct but it's not the same Paxos Commit proposed in the paper. 4/25/2021. Lamport proposed Paxos Commit algorithm as an alternative to 2PC for achieving Transaction Commit. It's a very natural application of the Paxos…