Use sets, generator expressions, and caching to increase performance.