Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
This patch adds static caching to taxonomy_get_term. It is hard to find examples of where this makes a difference in core, but the contrib repository is full of examples of taxonomy_get_term being called directly (which is correct), and the case often arises where it is called more than one time with the same term. Rather than put the burden of caching or optimizing on the contrib module developers, I believe that the right place is in taxonomy_get_term.
Comment | File | Size | Author |
---|---|---|---|
#17 | taxonomy_get_term_cache_2.patch | 2.83 KB | robertDouglass |
#6 | taxonomy_get_term_cache_1.patch | 965 bytes | robertDouglass |
#3 | taxonomy_get_term_cache_0.patch | 964 bytes | robertDouglass |
taxonomy_get_term_cache.patch | 966 bytes | robertDouglass | |
Comments
Comment #1
robertDouglass CreditAttribution: robertDouglass commentedThis patch can be used for 4.7 and 4.6 as well.
Comment #2
chx CreditAttribution: chx commentedis_null is slow, use isset instead.
Comment #3
robertDouglass CreditAttribution: robertDouglass commentedchx hath spoken
Comment #4
robertDouglass CreditAttribution: robertDouglass commentedComment #5
m3avrck CreditAttribution: m3avrck commentedShouldn't that be:
if (!isset()) {}
Missing the *!* part?
Comment #6
robertDouglass CreditAttribution: robertDouglass commenteddoh! Caught by the double negative. Thanks.
Comment #7
robertDouglass CreditAttribution: robertDouglass commentedComment #8
Dries CreditAttribution: Dries commentedIt would be good to look at the other (get-) functions in taxonomy.module to investigate which do caching and which don't. There are quite a few of these and making their behavior inconsistent is not adviced.
Comment #9
robertDouglass CreditAttribution: robertDouglass commentedHere's a summary of the taxonomy_get_* functions and their caching status:
With caching
Without caching
What level of consistency are you looking for, Dries? How much static caching is too much? I think the case at hand (taxonomy_get_term) is a clear win.
Comment #10
m3avrck CreditAttribution: m3avrck commentedHmmm, I think I agree with Dries, consistency is best.
While this patch addresses taxonomy_get_term(), I think that's an isolate case--I know we are using that function a ton, just because :-)
However, that is not to say another site might be using another function that currently isn't cached -- in that case another patch would come up later to add it in.
I don't see any reason not to use the same static !isset() for all them --- speeding them all up consistently is a clear, easy, and future proof win :-)
Comment #11
robertDouglass CreditAttribution: robertDouglass commentedI'm not convinced that adding static caching to all of them is worth it. I have a suspicion that we'd be increasing the memory footprint by storing copies of variables that would otherwise be released. Like you said, taxonomy_get_term gets called a lot (and not just in this specific case), whereas some of the functions listed may not. I'd like some finer grained guidelines (or benchmarks) before adding static to every lookup function. And then, what about the rest of Drupal? There are lots of other cases where things get looked up from the database but not static cached. While I'm all for consistency, I'm also for incremental change =)
Comment #12
robertDouglass CreditAttribution: robertDouglass commentedAnd just to be a devil's advocate: aren't all of these lookup functions operating on what is essentially the same data model? Couldn't they be sharing the same static cached data structure? If we cache taxonomy_get_tree, and taxonomy_get_term, aren't we caching the same information twice in a lot of cases?
If we want to be consistent about caching, I would advocate something more radical than just adding a static array to the beginning of all those functions listed. I'd advocate defining the taxonomy data structure (taxonomy_get_tree comes pretty close already) and centralizing the cache so that the structure gets built a maximum of one time per request. This would also make a *sweet* point of departure for things like memcache.
Comment #13
robertDouglass CreditAttribution: robertDouglass commentedbut the attached patch reduces queries now =)
Comment #14
drummI read through the patch, and think this is okay.
Comment #15
Dries CreditAttribution: Dries commentedPatch looks OK but let's document it in the phpDoc/API documentation?
Comment #16
drummAgreed.
Comment #17
robertDouglass CreditAttribution: robertDouglass commentedPHP Docs updated for all functions that do static caching.
Comment #18
robertDouglass CreditAttribution: robertDouglass commentedComment #19
Dries CreditAttribution: Dries commentedCommitted to CVS HEAD. Thanks.
Comment #20
(not verified) CreditAttribution: commented