Faster explain #3843

wants to merge 5 commits into


None yet

2 participants


This is a bit hard to benchmark and pinpoint as a whole but I did isolation benchmarks. This commit:

  • Avoids constant lookups
  • Caches the result of Thread.current in local variables
  • Avoid &block
  • Avoid subscribed blocks (and consequently avoid expiring notifications internal caches). In fact, the API was removed to discourage the behavior.

/cc @fxn

@fxn fxn added a commit that closed this pull request Dec 4, 2011
@fxn fxn implements a much faster auto EXPLAIN, closes #3843 [José Valim & Xav…
…ier Noria]

This commit vastly reduces the impact of auto
explain logging when enabled, while keeping
a negligible cost when disabled.

The first implementation was based on the idea
of subscribing to "sql.active_record" when
needed, and unsubscribing once done. This is
the idea behind AR::Relation#explain. Subscribe,
collect, unsubscribe.

But with the current implementation of notifications
unsubscribing is costly, because it wipes an internal
cache and that puts a penalty on the next event.

So we are switching to an approach where a long-running
subscriber is listening. Instead of collecting the
queries with a closure in a dedicated subscriber, now
we setup a thread local.

If the feature is disabled by setting the threshold
to nil, the subscriber will call a method that does
nothing. That's totally cheap.
@fxn fxn closed this in cfeac38 Dec 4, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment