Data Access Patterns That Makes Your CPU Really Angry

Given an array of data, what is the slowest way to sum up the integers? Is it adding the numbers from left to right, adding them randomly, or doing something else? In this post, we are going to build a data access pattern from the ground up that sums numbers as slowly as possible by exploiting memory pitfalls. uint32_t* data = ...; // sequential data[0] + data[1] + data[2] + ... // random data[67] + data[69420] + data[42] + ... // the slowest data[A] + data[B] + data[C] + ... Spoiler: You can do >30% worse than a randomized access pattern. ...

June 26, 2026 · weineng

The smallest C binary

I thought of a cute problem: what is the smallest (size) ./a.out binary that I can create? Here are some rules the program should follow: ./a.out must run successfully. $? must deterministically be 0. The binary must be produced by GCC only; no post-processing with objcopy, hex editors, or manual patching. We begin with the simplest program possible: // compiled with gcc empty.c int main() { return 0; }This gives us a file size of 15816 bytes (from stat). Not too shabby, but we will need four of the RAM used in the Apollo guidance computer to fit our binary that does nothing. ...

June 4, 2026 · weineng

C++ tidbits

random c++ stuff that might be leeched off the internet std::shared_ptr<void> writing an allocator generating switch statements with templates counting the number of member fields of a trivial struct OmegaException destructors should almost never throw exceptions defining a static class member Probably prefer (... <op> Ts) to (Ts <op> ...>) type hack Separator argument delete move constructor strange syntax getting the offset of a base class from a derived class Calendar tricks random c++ stuff that might be leeched off the internet std::shared_ptr<void> since shared_ptr stores a type-erased deleter, you can do the following ...

weineng