On Saturday, September 1, 2012 8:09:27 AM UTC-4, Massimo Di Pierro wrote:
I think this is an excellent idea. It is simple and does the job.
It adds a re.compile at every request. We should time that and make sure it is negligible.
Good point. It's actually an re.compile for every file in the models folder (and sub-folders). However, I believe when you call re.compile with a particular regex, the interpreter caches the compiled regex, so subsequent re.compile calls for the same regex are much faster. For a standard request with no custom models_to_run, there will be only one regex to compile. For requests that set one custom models_to_run, there will be two regexes to compile (the default regex, followed by the custom regex).
I used timeit to measure the time to compile the default models_to_run regex. To get the time to compile a new regex (not cached), I appended a random.random() number each time so each regex would be unique. In that case, it took about 0.6ms per regex -- a bit high, but not too bad. I then ran it without appending the random number in order to get the time to do a repeat compile, and that drops to about 1 microsecond.
The question is, does the cached compiled regex survive across requests? If so, then assuming an app has a relatively small fixed number of distinct models_to_run regexes, once there have been a few requests forcing the regexes to get cached, the additional time on new requests should be trivial.
Anthony