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.
Problem/Motivation
When many queries happen on the same tables the sheer volume of preg_replace() calls can easily save upwards of 100ms-200ms on approx 8000 sql calls by avoiding doing the preg_replace on the same table or field names.
More typical 10-30ms savings on a 600 sql query page.
Proposed resolution
Add a static to each method to memo-ize the preg_match results.
Comment | File | Size | Author |
---|---|---|---|
#36 | 2672088-36.patch | 2.49 KB | stefan.r |
#32 | 2672088-32.patch | 2.48 KB | stefan.r |
#28 | 2672088-28.patch | 2.22 KB | joelpittet |
#21 | add_static_cache_to-2672088-21.patch | 1.76 KB | joelpittet |
#21 | interdiff.txt | 728 bytes | joelpittet |
Comments
Comment #2
joelpittetComment #3
joelpittetComment #4
joelpittetHere's some xhprof with 500 line items in a commerce cart.
Diff%
Diff
(microsec)
Diff%
(microsec)
Diff%
MemUse
Diff
(bytes)
Diff%
PeakMemUse
Diff
(bytes)
Diff%
Comment #5
joelpittetComment #6
mikeytown2 CreditAttribution: mikeytown2 commentedLooking over some cachegrind's of mine and I see right around 10ms for 3k calls for each of the escape methods (~30ms total). Is this similar to what you're experiencing?
Comment #7
joelpittetYes around 30ms
Comment #8
joelpittetyeah last test I just did on a different site was only 9ms savings. Not a really big win here.
Comment #9
joelpittetThat table is messy in the IS.
It seems to keep coming up in my profiling even though it's not huge it seems pretty straight forward.
Comment #10
stefan.r CreditAttribution: stefan.r commentedShouldn't this be against 8.x first?
Comment #11
stefan.r CreditAttribution: stefan.r commentedComment #12
daffie CreditAttribution: daffie commentedThis sounds like a very good idea to me.
Comment #13
dawehnerThis doesn't have to be a static one. It would be totally enough to have a property on the object, caching the result.
Comment #14
daffie CreditAttribution: daffie commentedI think that input from Crell would be wise.
Comment #15
joelpittet@dawehner naming is a big hard;) Good suggestion though. We'd have to have a new property for each function because though similar the
preg_match()
regex is subtly different for aliases. I think I'd rather have it in context as a method static, is that ok?Comment #16
joelpittetForgot to negate the
isset()
Comment #18
daffie CreditAttribution: daffie commentedThe patch look good, but I have two remarks:
@dawehner stated in comment #13 that the variables do not have to be static ones. Can you explain why you keep them static.
Can we rename the variable fields to aliases, because we already have a variable fields and its is holding an array of aliases.
Comment #19
joelpittet@daffie thanks for the review. Regarding point 1). I tried to answer that in #15
2) Probably out of scope and I thought the same thing but left it to avoid unnecessary changes. Though not against it.
Comment #20
daffie CreditAttribution: daffie commented@joelpittet: If you change the variable name $fields to $aliases in the method escapeAlias(), the patch will for me be good enough for RTBC. Thank you for your explanation in the previous comment.
Comment #21
joelpittetOk here's the variable name change from
$fields
to$aliases
Comment #22
daffie CreditAttribution: daffie commentedI all looks good to me. But I am not 100% sure this is the right solution and I would like to ask one of our framework managers to be the committer for this issue.
Comment #23
xjmThanks @daffie.
Adding the rc deadline tag because I think this improvement could go into 8.2.x during its beta but not RC, if we do choose to commit it with its current implementation. (After the beta it can go straight into 8.3.x of course.)
Comment #24
catchSince these are normal methods, we could use a regular protected property on the class - no need for an actual static here. I see above this was avoided because it would be three properties, that doesn't seem like a reason not to do it?
It would also be good to see xhprof profiling with a standard install - I imagine we'll see a small improvement because we hit the same cache tables often.
Comment #25
joelpittet@catch one property per method(4) or one per regex difference(2). See why I did statics in #19
Comment #27
catch@joelpittet one per regex difference seems best - also if a database/table/column happened to be named the same (albeit unlikely) we could re-use the sanitization.
I see the reasoning for the static, but the disadvantage outweighs the advantage for me.
Comment #28
joelpittetThanks @catch, here's a first crack at naming those 2 member properties.
escapedNames()
andescapedAliases()
for the two regex'd patterns. Slight benefit that it catches names that are the same between table, database and field.Comment #29
catchThanks! That looks a lot better. Moving back to RTBC.
Comment #31
xjmA nice little performance improvement. I was going to assign it to @catch for signoff but he RTBCed the issue, so I've committed 4295f59 and pushed to 8.3.x. Thanks!
Comment #32
stefan.r CreditAttribution: stefan.r commentedAnd now we can add this to 7.x as well :)
Comment #34
stefan.r CreditAttribution: stefan.r commentedComment #36
stefan.r CreditAttribution: stefan.r commentedComment #37
joelpittetThanks RTBC #36, checked their is no database name escaping and the rest is identical to D8. The previous failure was PHP 5.4 short array syntax.
Comment #38
stefan.r CreditAttribution: stefan.r commentedComment #39
Fabianx CreditAttribution: Fabianx as a volunteer and at Tag1 Consulting commentedMarking for commit.
Very nice change.
Comment #41
stefan.r CreditAttribution: stefan.r commentedRe-reviewed with Fabianx, and committed and pushed to 7.x, thanks!