# RULEBOOK: Python Performance **Load into coding subagents. ~200 tokens. See `/papers/*.md` for deep dives.** ## The Numbers - Async overhead: **1,400x** (sync 20ns, async 28us) - Set vs list membership: **200x** at 1000 items - orjson vs stdlib json: **8x** faster - Exception raise: **6.5x** (try/except is free) - SQLite commit: **98%** of write latency ## Rules **Collections**: Default to `set` for membership. Use `dict.get()` not check-then-access. **Async**: Default sync. Only async for I/O >1ms. Use `gather()` not loops. **JSON**: Default `orjson`. stdlib only for zero-deps requirement. **Exceptions**: try/except is free. Use `.get()` for dicts. EAFP for <15% failure rate. **Strings**: f-strings default. `%` for logging (deferred eval). `join()` for many parts. **Memory**: `@dataclass(slots=True)` for 100+ instances. **Imports**: `TYPE_CHECKING` for heavy types. Lazy imports in CLI tools. **Database**: DiskCache for key-value. Batch SQLite writes. Reads cheap, writes expensive. --- *Source: python-numbers-everyone-should-know (Python 3.14.2, Apple Silicon)*