alphagov/bouncer — pull request #99 by rgarner (2014-10-30 12:06:27+00:00)
Title: Performance tweaks

We imagined we might be able to prevent a lot of duplicate queries here. We also imagined we might be able to amalgamate the initial host query into one joined with site and possibly org. We imagined that doing that while also memoizing some of the values in [`RequestContext`](https://github.com/alphagov/bouncer/blob/master/lib/bouncer/request_context.rb) might get us to performance nirvana.

If you'd like an idea of why much of this didn't work, you can stroll around the [failed experiments branch](https://github.com/alphagov/bouncer/compare/performance-tweaks-failure?expand=1). I'd invite you to try `rake benchmark:all` with any ideas you have, too, as the results are surprising.

What did work was getting a reliable benchmark, running it against the last dev database we sync'd, and then dropping in the `ActiveRecord::QueryCache` middleware. That's this pull request. In this way we prevent repeat round trips at the ActiveRecord level, but we don't explicitly memoize or attempt eager loading in our own code. This nets us around 30%. Everything else slows things down.