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

StepGoalHow
1. Find the entryknow where execution startsmain → init → handling one query
2. Read the data structuresknow where state livesstructs, containers, memo/group objects
3. Follow the control flowsee one task completebreakpoints or logging on a minimal input
4. Change one thingprove you understoodflip 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:

  1. main / the optimize entry: what does an optimize call receive?
  2. CJob / the task engine: how are tasks scheduled and dependencies expressed?
  3. Memo / Group: how expressions are stored and deduplicated;
  4. 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

  1. Starting from utilities: you finish them and still don’t know what the system does;
  2. Trying to read it all: 400K lines is not a reading project; aim to “change one thing correctly”;
  3. 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.