Opening a 400K-line optimizer, the first question is usually “where do I start”. The answer isn’t line one — it’s one complete call path.
1. Four steps
| Step | Goal | How |
|---|---|---|
| 1. Find the entry | know where execution starts | main → init → handling one query |
| 2. Read the data structures | know where state lives | structs, containers, memo/group objects |
| 3. Follow the control flow | see one task complete | breakpoints or logging on a minimal input |
| 4. Change one thing | prove you understood | flip a branch, check the output changes as expected |
Step 4 is the point: reading without changing is usually an illusion of understanding.
2. gporca as an example
gporca is ~400K lines, but only about 10K of them are the “framework”:
main → CAutoOptCtxt → Optimizer → CJob → task scheduling
→ CXform (119 rules)
→ Memo (equivalent expressions)
→ DP (cells and costing)
The order to read:
main/ the optimize entry: what does an optimize call receive?CJob/ the task engine: how are tasks scheduled and dependencies expressed?Memo/Group: how expressions are stored and deduplicated;CXform: rules last — they’re homogeneous; three teach you the pattern.
The reverse (starting with 119 rules) drowns you in details before you see the frame.
3. Tools and habits
- grep to locate: search class and function names instead of reading directories;
- Minimal input: one simple query beats the whole TPC-H suite for tracing;
- Logs over breakpoints: breakpoints disturb timing in task schedulers;
- Read the tests: often the best usage docs;
- Take notes: draw the call chain and data structures; better than a second pass.
4. Three common mistakes
- Starting from utilities: you finish them and still don’t know what the system does;
- Trying to read it all: 400K lines is not a reading project; aim to “change one thing correctly”;
- Ignoring failure paths: happy-path-only reading leaves you helpless at the first bug.
5. In one sentence
Understanding isn’t “I finished reading”; it’s “I can change one place and explain its impact”.
Further reading: Reading gporca · Rewriting Lucene in C++ · Glossary
Get the next essay
New essays on products, business and tech — plus what the experiment's numbers actually say.