PERF: Cython optimizations for period module, round one
Each commit passes `test_fast.sh`, so performance can be measured between commits.

I can break any of these commits into separate PRs if needed.
