On Thu, Jun 26, 2008 at 4:20 PM, Travis (wikiHow) travis@wikihow.com wrote:
Do you think it might be an improvement if both Title::getParentCategories and Title::getParentCategoryTree could store their results in a local variable? They currently fetch the resuts DB each time regardless of whether they've been called already or not. I'm not sure how much this is called in a normal installation, but we call it a few times to figure out the category structure of the article.
Storing the results seems to cut down our # of db calls by about 12 per page view.
It might be a reasonable idea. I would suggest only caching the result object, not the array, because arrays eat up a lot more memory, according to Tim. Something like this (diff won't apply cleanly because this is trunk):
- # NEW SQL - $sql = "SELECT * FROM $categorylinks" - ." WHERE cl_from='$titlekey'" - ." AND cl_from <> '0'" - ." ORDER BY cl_sortkey"; + if( !isset( $this->mParentCategories ) ) { + # NEW SQL + $sql = "SELECT * FROM $categorylinks" + ." WHERE cl_from='$titlekey'" + ." AND cl_from <> '0'" + ." ORDER BY cl_sortkey";
- $res = $dbr->query( $sql ); + $this->mParentCategories = $dbr->query( $sql ); + } + $res = $this->mParentCategories;
However, if this is being caused by a specific extension, it might be a better idea to look at that extension for higher-level optimization. For instance, my diff here only catches repeated queries from the same Title object, which will not be all repeated queries. On the other hand, if you kept all the results in a static array, you're wasting memory, since they can never be garbage-collected, and this might be quite a lot of memory wasted if this method is called for a few hundred different pages in one request. A cache at the caller level is probably a better idea, since it can be comprehensive but at the same time fall out of scope and be garbage-collected when it's no longer needed.
If you find it's being caused by core code or an extension in SVN, of course, we'd like to hear about it.