Low-Latency Systems Development #17 14 min read

Wait-Free

Wait-free is not a faster lock-free: it turns a failed CAS from “try again” into “I come after you”, which bounds every thread’s steps, at the cost of more atomics and code that is harder to get right.

Common Bottlenecks and Optimizations Topic 23 min read

perf: How to Use It, What to Read

stat for the totals, record and report to find the function holding 98% of the samples, annotate to see every sample land on the instruction after an add: a chain of adds, each waiting for the last, and splitting it four ways is …

Low-Latency Systems Development #16 35 min read

Spinlocks

Whether a spinlock is fast depends on what its waiters do: if every attempt is a read-modify-write, they drag the lock’s line back and forth. Read before you grab, put PAUSE in the loop, back off after losing, and waiting costs …

Low-Latency Systems Development Topic 22 min read

A Cache Case Study: Repricing 64 Options per Tick

One tick touches 140 cache lines, and most of the time goes into waiting for them. None of the four steps changes the computation; they change when data arrives, where it lands and who holds it: put the misses in flight together, …

Low-Latency Systems Development #15 10 min read

SIMD Vectorization and Intrinsics

One instruction computes a row of numbers only if the compiler knows how wide a register it may use, is sure nothing overlaps, and is allowed to reorder; when it can’t get all three, intrinsics let you write it yourself, at the …

Low-Latency Systems Development #14 10 min read

Prefetching, Cache Warming, and Double Buffering

There are three ways to get data into cache ahead of time, each with a precondition: prefetching needs you to know the address before the hardware does, warming has to happen on the core that reads the data, and double buffering …

Low-Latency Systems Development #13 14 min read

Type Punning, Function Calls, and Compiler Hints

How far the compiler can optimize depends on what it can see and what it dares to assume: different types are treated as non-overlapping, an invisible function body means arguments go by the calling convention, and helper macros …

Low-Latency Systems Development #12 13 min read

Strength Reduction and Arithmetic Optimization

The compiler only makes rewrites it can prove don’t change the result, and it does constant multiplies and divides better than you. What’s left for you is where it lacks information or where the rewrite changes the result: …

Low-Latency Systems Development #9 21 min read

Loop Optimization

A dot-product loop reports “vectorized” at -O3, yet its four additions still wait on each other: floating-point addition isn’t associative, so the compiler won’t reorder it. What loop optimization really asks of you is to spot …

Low-Latency Systems Development #8 22 min read

CRTP and Static Polymorphism

The base class writes the flow and the subclass fills in the details: virtual functions pick the subclass at run time, CRTP at compile time. What you save is more than one indirect jump: the call inlines completely and the object …

Low-Latency Systems Development #11 10 min read

Return Value Optimization and Pure Functions

Returning a struct by value is usually free, so there’s no need for out-parameters; what to watch are the few patterns that always move or copy. Pure-function attributes are a promise to the compiler: right, and it calls less; …

Low-Latency Systems Development #10 14 min read

Pointer and Memory-Access Optimization

In a loop like a[i] = *b, the compiler re-reads *b every iteration, because writing a[i] might change it. Pointer optimization is about letting the compiler read less, and fitting linked structures into nearer caches.

Low-Latency Systems Development #7 41 min read

Branch Prediction and Branch Optimization

The front end has to fetch instructions every cycle, but a branch isn’t resolved until a dozen-plus cycles later, so the CPU guesses. A right guess costs almost nothing; a wrong one throws away everything after it, about 15–20 …

Low-Latency Systems Development Topic 36 min read

Memory Latency and Bandwidth

One memory access takes about 100 ns and brings back 64 B. One at a time, that’s 0.64 GB/s, yet one core reading sequentially gets 15–60 GB/s. The missing factor is the number of requests in flight. Whether the next load’s address …

Low-Latency Systems Development Topic 15 min read

Context Switches, Page Tables and the TLB

Threads are lighter than processes because the TLB and caches stay usable after the switch, not because the switch itself is cheaper. The TLB has no MESI: when any thread in a process changes the page tables, every core still …

Low-Latency Systems Development #6 41 min read

Alignment, Layout, and Cache and DRAM Geometry

An access never pays for bytes; it pays for cache lines, and the address decides which lines, which cache set they compete for, and which DRAM channel and bank they live on. Part 1 is alignment: why a double aligned to 8 never …

Low-Latency Systems Development #5 22 min read

An SPMC Broadcast Ring in Shared Memory

Market data has one producer and several consumers, each of which must see every message, and the producer is the hot path that may not wait for anyone. The answer is a broadcast ring: every slot carries its own sequence stamp, …

Low-Latency Systems Development Topic 14 min read

MESI Cache Coherence

MESI enforces one rule: a cache line has either one core that may write it or any number of cores that may only read it, never both. The four states are the answers to two questions — does anyone else hold this line, and have I …

Low-Latency Systems Development #4 24 min read

Lock-Free Queues and Micro-Batching

A lock-free SPSC queue works because every shared variable has exactly one writer: each side updates only its own counter and merely reads the other’s. The rest is keeping that property from being quietly undone — a shared element …

Low-Latency Systems Development #3 16 min read

Memory Pools and Allocators

Every technique in this post does the same thing at a different layer: move an uncertain, kernel-involving operation off the hot path and force it to happen once, upfront, before it can ambush a single request. An object pool …

Low-Latency Systems Development #2 25 min read

Memory Ordering and Cache Lines

The store buffer and the cache-coherence protocol are one piece of hardware, but they create two unrelated engineering problems. One is correctness: does a thread see the right value, in the right order — solved by memory_order …

Low-Latency Systems Development #1 17 min read

CPU Isolation and NUMA

The enemy of a low-latency system is jitter, not the mean. The goal is to make your hot thread the only thing that will ever run on its core: pinning kills migration cost, the isolation stack evicts the tick / RCU / interrupts one …

Common Bottlenecks and Optimizations #21 13 min read

Measuring Trading-System Performance

What TTT and RTT each cover, why to report p50, p99 and p999 instead of the mean, how marks at thread boundaries separate processing from queueing, and how to use the latency table as a budget.

Low-Latency Systems Development #20 25 min read

Measuring Latency with the TSC

What a TSC reading means, when it is taken, which core it is taken on, how to record timestamps on the hot path, and how ticks become ns: where the frequency comes from and how to calibrate it yourself.

Low-Latency Systems Development #19 26 min read

Scheduling Policies, Priority Inversion and Kernel Tuning

Who runs first and for how long: scheduling classes and time slices, three traps of real-time priorities, two fixes for priority inversion, why HFT often skips PREEMPT_RT, and which knobs a low-latency box turns from BIOS to …

Hands-on Practice #18 16 min read

Thread-Safe Queues

From one mutex to many producers: condition variables and lost wakeups in a blocking queue, rte_ring’s two pairs of indices and in-order publishing, Disruptor’s availability flags, and whether a full queue should block, drop, or …

AI Native 4 min read

AGENTS.md Practice Guide — Map, Commands, and Verification Loops for AI Coding Agents

AGENTS.md is a README for AI agents: a compact project map that exposes hard rules, build/test commands, verification loops, and deeper documentation links so agents can understand, modify, and verify code without relying on …

Number Theory #4 12 min read

Primality, Factorisation & Residues (Miller-Rabin, Pollard Rho, Tonelli-Shanks)

Miller-Rabin primality test, Pollard Rho factorisation, Tonelli-Shanks for quadratic residues, cubic residues via F_p[t]/(t^3-a), quartic residues by double square root, and general k-th residues via primitive root + BSGS, plus …

Number Theory #3.1 6 min read

Sieve Problems — Coin System DP via Sieve

Greedy optimality for divisibility-chain coin systems, DP over the maximum denomination, and a sieve-style transition by adding one larger coin.

Number Theory #3 5 min read

From Eratosthenes to Möbius Inclusion-Exclusion

Eratosthenes sieve for μ; g/f/h framework; Conclusions 1 and 2 with derivations and tables; GCD table intuition; CF 803F Coprime Subsequences with bottom-up and Möbius approaches.

Number Theory #2 15 min read

Multiplicative Functions, Möbius Inversion & Dirichlet Convolution

Multiplicative functions, single-point and linear-sieve evaluation; Möbius function, both forms of Möbius inversion with full derivations and examples; Dirichlet convolution, key identities, Du Jiao sieve; generalized Möbius …

Number Theory #2-ex 6 min read

Möbius Techniques

Seven core techniques for Möbius inversion problems: loop shortening, gcd indicator expansion, coprimality reduction, Dirichlet convolution via T=dk, divisor symmetry, diagonal symmetry, and value-domain cnt[] tricks.

Number Theory #2-ex 9 min read

Möbius Practice — LCM Sums, Divisor-Count, Omega Numbers

Practice problems for Möbius inversion: LCMSUM, two-variable lcm sum (O(n^3/4)), FSF’s game with offline difference, divisor-count sum d(ij), Omega Numbers with Dirichlet suffix sum.

Number Theory #1.1 3 min read

Divisibility Blocking (Floor-Division Decomposition)

Divisibility blocking for O(sqrt n) floor-sum evaluation: lemma, single-variable loop, O(1) interval sum requirements, two-variable min-trick, multi-variable pseudocode, and ceiling-division blocking.

Number Theory #1 9 min read

Elementary Number Theory — GCD, LCM, Euler, CRT

GCD/LCM properties, Bézout/Frobenius, Euler’s theorem with power-tower reduction and φ-chain technique, congruence system merging via exGCD, CRT, and n! prime factorisation via Legendre’s formula.

Number Theory #0 2 min read

Common Formulae — Legendre's Formula & Divisor Convolutions

Legendre’s formula, divisor and multiple convolutions in O(n log n), offline-difference batch processing for H(n)=Σf(d)g(⌊n/d⌋).

Matrix #4 6 min read

Matrix Problems — Fibonacci Subarray Sums, Gaussian Elimination & Expected Value

Hard matrix problems: Fibonacci squared subarray sum with segment-tree 4-tuple and companion matrix; cycle-decomposition partition state space for random-swap sorting, first-step analysis, and Gaussian elimination.

Matrix #3 8 min read

Linear Basis (XOR Basis) & Simplex Method

XOR linear basis: full template with kth, range basis, normalisation; Nowcoder median example; CF 1101G prefix-XOR rank; gym 104768 C XOR-divisibility; Simplex LP solver.

Matrix #2 15 min read

Gaussian Elimination — Real, Mod, Mod-2, Incremental

Gauss elimination: real/mod/mod-2 templates (solve, det, inv, rank); incremental online RREF; Extended Lights Out, CF 1344F abstract GF(2) modelling, 2024 ICPC Kunming interactive tree queries.

Matrix #1 17 min read

Matrix Fast Power — Linear Recurrence, DP Semirings & Kitamasa

Matrix fast power for linear recurrences (4 patterns), 4 semiring templates (count/bool/min-plus/max-plus), matrix+segment tree for range-add Fibonacci queries, Kitamasa O(k²logn) and NTT O(k log²k logn).

Combinatorics 7 min read

Order Theory — Chromatic Polynomials, Chordal Graphs, Order Polynomials, Dilworth & Hall

Chromatic polynomial, chordal/interval graphs, PEO product formula, order polynomials (chain/antichain/tree/DAG/divisor lattice), MacMahon formula, Dilworth, Mirsky, Hall’s marriage theorem, Erdős–Szekeres, missile interception.

Combinatorics #3.2 15 min read

More Operations on Formal Power Series

Newton iteration for polynomial inverse and square root, derivative/integral/composition of formal power series, logarithm and exponential of series, and worked examples including CF438E and a bad-modulus complete-knapsack …

Combinatorics #3.1 8 min read

Ordinary Generating Functions

Formal power series basics, ordinary generating functions, inversion identities, generalized binomial expansions, bounded counting examples, and recurrence-to-closed-form examples such as Fibonacci and Catalan.

Combinatorics #2.2 12 min read

Primitive Roots, Discrete Logarithm, NTT, Divide-and-Conquer NTT

Order and primitive roots (Euler’s theorem, BSGS, exBSGS), discrete index as logarithm analogue, SGU 261 Discrete Roots, NTT theory and templates, divide-and-conquer NTT (product tree), Wannafly D team selection, Nowcoder 50F …

Combinatorics #2.1 10 min read

FFT and Lagrange Interpolation

Polynomial representations, FFT/DFT over complex numbers, Triple Sums, Fuzzy Search (difference convolution), FFT template, Lagrange interpolation (O(n²) and O(n) equidistant), sum of k-th powers, Assigning Prizes polynomial DP.

Combinatorics #2-ex2 5 min read

Cyclic Convolution

Cyclic convolution definition, FFT/NTT implementation for n=2^k, zero-padding, why n must be a power of two, and the fold-back technique for arbitrary n.

Combinatorics #2-ex1 2 min read

Difference (Correlation) Convolution

Cross-correlation via ordinary convolution: reverse-A and reverse-B extraction methods, derivation, and Ring Trick II (cyclic shift score maximisation via difference convolution).

Combinatorics #1.1 9 min read

Permutations and Combinations

Hockey Stick identity, addition/multiplication principles, binomial identities, example problems: bitonic sequences, multiset permutations, multi-layer coloring DP, CCPC Subpermutation.

Combinatorics #1.2 1 min read

容斥原理、不定方程、错排

容斥原理的集合与算子形式、有上下界不定方程计数(前缀和DP与分治NTT)、错排公式与第二类斯特林数。

Combinatorics #1.2 2 min read

Inclusion-Exclusion, Bounded Diophantine Equations, and Derangements

Inclusion-exclusion in set and operator form, equal-count subproblems, bounded Diophantine counting, derangements, Stirling numbers, and selected contest problems.

Combinatorics #0.1 9 min read

计数中常用的结论

常见计数结论、Lucas 定理、不定方程解计数、平面分拆、DAG 路径计数、多项式平移、XOR 均匀性与若干竞赛常用结论。

Combinatorics #0 4 min read

Common Formulae & Conclusions

A compact toolkit of geometric sums, floor-sum transformations, XOR facts, Fibonacci identities, and perfect-square patterns.