Use static-cached variable for drupal_is_front_page()

Dave Cohen - November 29, 2008 - 16:22
Project:Drupal
Version:6.x-dev
Component:base system
Category:task
Priority:normal
Assigned:Unassigned
Status:needs review
Issue tags:Performance
Description

I have a site which implements custom_url_rewrite_inbound(). I noticed this function was being called an obscene amount of times for many requests, when what I expected was for it to be called only once per request.

It turns out that drupal_get_normal_path calls custom_url_rewrite_inbound, and the latter is called with every request to drupal_is_front_page. Frankly, I'm not convinced these calls to drupal_is_front_page need to call drupal_get_normal_path, but the code is there so someone probably had a reason.

At any rate, the attached patch caches the front page path, so calls to drupal_get_normal_path and custom_url_rewrite_inbound only happen the first time drupal_is_front_page is called. With this patch applied, custom_url_rewrite_inbound() is called twice per request. Once when it is actually needed, to rewrite the incoming request. And a second time to rewrite the front page path, which as I said earlier I'm not convinced is actually necessary or good.

AttachmentSizeStatusTest resultOperations
front_page.diff701 bytesIdleFailed: Failed to apply patch.View details | Re-test

#1

Dave Reid - November 29, 2008 - 18:34
Version:6.x-dev» 7.x-dev
Category:bug report» task
Assigned to:Anonymous» Dave Reid
Status:active» needs review

We use the same static caching with nearly every other function in path.inc, so it makes sense to implement it for drupal_is_front_page() as well. With a base install, 10-node front page, and a site mission statement enabled, drupal_is_front_page() is called 16 times! I did some ab benchmarking (-c 2 -n 500) and it did marginally improve performance:

Before patch (current HEAD):

Requests per second:    5.87 [#/sec] (mean)
Time per request:       340.586 [ms] (mean)
Time per request:       170.293 [ms] (mean, across all concurrent requests)
Transfer rate:          90.37 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   1.2      0      26
Processing:   298  339  35.2    334     671
Waiting:      272  302  35.6    304     657
Total:        298  340  35.2    334     671

After patch:

Requests per second:    5.90 [#/sec] (mean)
Time per request:       339.184 [ms] (mean)
Time per request:       169.592 [ms] (mean, across all concurrent requests)
Transfer rate:          90.75 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   1.0      0      23
Processing:   304  338  25.7    335     653
Waiting:      277  301  25.2    303     626
Total:        304  338  25.8    335     653

This is a good find and moving this to the 7.x queue so we can fix there first and then back-port since this won't be an API change.

AttachmentSizeStatusTest resultOperations
340557-drupal-is-frontpage-D7.patch1.23 KBIdlePassed: 7497 passes, 0 fails, 0 exceptionsView details | Re-test

#2

Dave Reid - November 29, 2008 - 18:35
Title:drupal_is_front_page calls drupal_get_normal_path too often» Use static-cached variable for drupal_is_front_page()

#3

justinrandell - November 30, 2008 - 00:09
Status:needs review» reviewed & tested by the community

nice catch! simple patch, all tests pass, RTBC.

#4

Dave Cohen - December 1, 2008 - 08:14

The performance improvement will be even more for sites that implement custom url rewrites. Thanks for pushing this through.

#5

justinrandell - December 1, 2008 - 08:53

@Dave: in #drupal, webchick asked if its possible to add a test for this?

#6

Dave Reid - December 1, 2008 - 19:18

Hmm, I'll work on it. It might take a little workaround to get it to test.

#7

Dries - December 2, 2008 - 20:46
Status:reviewed & tested by the community» needs work

Patch looks good to me but let's wait for a test to emerge (per webchick's request). :)

#8

Dave Reid - December 6, 2008 - 18:07
Status:needs work» needs review

Test completed and ready for review.

AttachmentSizeStatusTest resultOperations
340557-drupal-is-frontpage-D7.patch3.71 KBIdlePassed: 7555 passes, 0 fails, 0 exceptionsView details | Re-test

#9

Dave Reid - December 6, 2008 - 18:10

Marked #313123: Statically cache drupal_is_front_page() as a duplicate of this issue.

#10

Dave Reid - December 6, 2008 - 18:31

Fixed a commented section that should have been un-commented.

AttachmentSizeStatusTest resultOperations
340557-drupal-is-frontpage-D7.patch3.7 KBIdlePassed: 7510 passes, 0 fails, 0 exceptionsView details | Re-test

#11

Dries - December 6, 2008 - 21:30

Small request: can we change 'drupal_is_front_page' in +    drupal_set_message('drupal_is_front_page: ' . (drupal_is_front_page() ? 'TRUE' : 'FALSE')); to just 'front page'?

#12

justinrandell - December 6, 2008 - 21:49
Status:needs review» needs work

looking good, more core code with tests. comments on the new test:

+    variable_set('drupal_is_front_page_output', 1);

could we have a comment with that?

+    variable_set('site_frontpage', $node_path);
+    $this->refreshVariables();
+    $this->assertEqual(variable_get('site_frontpage', 'node'), $node_path, t('Set front page path to %path', array('%path' => $node_path)));

this looks to be testing variable_(set|get) more than the site frontpage functionality. perhaps it would be better to test this via drupal's interface? i.e., create an admin user, drupalGet('admin/settings/site-information'), etc?

#13

Dave Reid - December 6, 2008 - 22:22

Thanks for the review. This should probably be in system.test and not a path.test then if we're going to test the front page admin. Working on another revision!

#14

Dave Reid - December 6, 2008 - 23:41
Status:needs work» needs review

Revised test that's a part of system.test now. It tests the setting the front page at admin/settings/site-information (and also an invalid front page path). I couldn't resist doing a little cleanup on system.test as well, so forgive me. As for #11, I changed it to drupal_set_message(t('On front page.')); because on the default front page with no nodes, the "get started with your install" text has the string "front page" in it.

AttachmentSizeStatusTest resultOperations
340557-drupal-is-frontpage-D7.patch8.18 KBIdlePassed: 7562 passes, 0 fails, 0 exceptionsView details | Re-test

#15

Dave Reid - December 6, 2008 - 23:53

Had a line with whitespace in last patch.

AttachmentSizeStatusTest resultOperations
340557-drupal-is-frontpage-D7.patch8.18 KBIdlePassed: 7580 passes, 0 fails, 0 exceptionsView details | Re-test

#16

Dave Reid - December 7, 2008 - 00:36
Status:needs review» needs work

Found a little bug while trying drupalPost-ing to admin/settings/site-information. Please don't commit this until #343765: DrupalWebTestCase does not set required site_mail variable during setUp() is committed. I will provided a revised patch once that is committed.

#17

Dave Reid - December 7, 2008 - 00:40
Status:needs work» needs review

Nevermind... #343765: DrupalWebTestCase does not set required site_mail variable during setUp() is marked as "won't fix" now.

#18

Dave Reid - December 7, 2008 - 00:54

Also note the patch in #15 has passed testing-bot, the results have failed to be posted back here:
http://testing.drupal.org/pifr/file/1/2201

#19

Dave Reid - December 7, 2008 - 03:52

Revised de-meowed test.

AttachmentSizeStatusTest resultOperations
340557-drupal-is-frontpage-D7.patch4.72 KBIdleFailed: 7525 passes, 3 fails, 0 exceptionsView details | Re-test

#20

System Message - December 7, 2008 - 15:40
Status:needs review» needs work

The last submitted patch failed testing.

#21

Dave Reid - December 7, 2008 - 19:53
Status:needs work» needs review

Revised patch now that #343765: DrupalWebTestCase does not set required site_mail variable during setUp() landed in core.

AttachmentSizeStatusTest resultOperations
340557-drupal-is-frontpage-D7.patch4.72 KBIdlePassed: 7562 passes, 0 fails, 0 exceptionsView details | Re-test

#22

Dries - December 9, 2008 - 07:16
Status:needs review» fixed

Committed to CVS HEAD. Thanks!

#23

System Message - December 23, 2008 - 07:21
Status:fixed» closed

Automatically closed -- issue fixed for two weeks with no activity.

#24

dropcube - February 8, 2009 - 14:32
Version:7.x-dev» 6.x-dev
Status:closed» needs review

Can we port back this to 6.x ? It is a performance improvement, doesn't break any API, and is a simple patch.

AttachmentSizeStatusTest resultOperations
6.x-static-cache-drupal_is_front_page-340557-24.patch1.15 KBIgnoredNoneNone

#25

smk-ka - June 7, 2009 - 10:51
Assigned to:Dave Reid» Anonymous

Cleaned patch from added whitespace. Would be nice to see this fixed in D6, too.

AttachmentSizeStatusTest resultOperations
drupal_is_front_page-D6.patch1.04 KBIgnoredNoneNone
 
 

Drupal is a registered trademark of Dries Buytaert.