2006-03-20
An optimization thought
Today's random optimization thought, brought to my mind by the process of trying to make DWiki run slightly faster:
If you can't make it faster, maybe you can call it less.
Caches are the degenerate brute force case of this, but program restructuring is better. Repeatedly calling the same core routine can be a sign that the upstream interfaces aren't really doing what higher levels want, so the higher levels have to do redundant work.
Besides, if you can figure out a way to call something less you don't have to worry about cache invalidation.
For example, DWiki's routine to get a page from the file store used to
first call store.exists()
to see if the page existed, and then call
store.get()
to retrieve a low-level file object. Since the file store
is deliberately paranoid for security reasons, this
resulted in redundant calls to the 'is this a valid filename?' routine
from the two file store routines. Some sort of cache would have been
the wrong answer; the right one was to fiddle the interface to .get()
so the higher level 'get a page' routine could stop calling .exists()
beforehand.
(The 'is this a valid path' routine is simple and small, but it got called enough to turn up in profiles. DWiki can do a lot of path lookups; with a couple of tweaks, a typical page dropped from 144 calls to 105.)