Download & Extend

Duplicate calls to format_date() in node rendering

Project:Drupal core
Version:7.x-dev
Component:theme system
Category:bug report
Priority:normal
Assigned:Unassigned
Status:needs work

Issue Summary

http://api.drupal.org/api/function/template_preprocess_node/7

and

http://api.drupal.org/api/function/theme_node_submitted

both do format_date($node->submitted);

Those 20 calls to format_date() on a default front page account for 5.5% of execution time.

We should either split $submitted into author and date, or remove $date and let themes handle it themselves - but doing this work twice when we only use one variable in any one template is a waste.

AttachmentSizeStatusTest resultOperations
format_date.png63.29 KBIgnored: Check issue status.NoneNone

Comments

#1

Title:Duplicate calls to format_date() for nodes» Duplicate calls to format_date() in node rendering
Status:active» needs review

Let's try just removing it first. There must be an existing issue somewhere for making $submitted less horrible.

10 nodes on front page. ab -c1 -n500

HEAD:
8.92 [#/sec]
8.80 [#/sec]
9.20 [#/sec]

Patch:
9.37[#/sec]
9.40 [#/sec]
9.46 [#/sec]

Benchmarks come out more like 6% compared to the expected 2.5% from profiling, but close enough and it's definitely a measurable cost.

AttachmentSizeStatusTest resultOperations
format_date.patch682 bytesIdleFailed: Failed to apply patch.View details | Re-test
format_date.png63.92 KBIgnored: Check issue status.NoneNone
less_format_date.png49.11 KBIgnored: Check issue status.NoneNone

#2

Comments have the same problem.

AttachmentSizeStatusTest resultOperations
comments_too.png42.21 KBIgnored: Check issue status.NoneNone

#3

Nice work focusing on this function. Why is it such a pig? I would have thought that the new Date API in PHP is fast.

Somehow, we need to get to a pull system where we only prepare what the theme needs.

#4

Wow, that is a surprise. This sounds like RTBC to me, but I agree that a pull system would probably by desired at some point.

#5

#243129: Refactor format_date() to avoid multiple calls to date_format() has been around or a while but format_date() has changed significantly since then so it may not be the same issue any more.

I thought about adding a static cache to format_date() as well - would like to try that before we remove the variable.

#6

Here's a try with a static. Didn't seem to be any need to use drupal_static() here - can't see the static needing to be cleared but I might've overlooked something.

Attached webgrind screenshots.

AttachmentSizeStatusTest resultOperations
format_date.patch1.27 KBIdleFAILED: [[SimpleTest]]: [MySQL] Unable to apply patch format_date_5.patch.View details | Re-test
head.png31.49 KBIgnored: Check issue status.NoneNone
static.png25.19 KBIgnored: Check issue status.NoneNone

#7

#243129: Refactor format_date() to avoid multiple calls to date_format() is still needed. Instead of adding a static in format_date(), couldn't we avoid the duplicate call inside the field api?

#8

It's not field API though - it's template_preprocess_node/comment creating their own date variables as well as using the _submitted theme functions.

Removing $date from template_preprocess_node() (and the same with comments) removes the duplication in HEAD - but I can see themes adding that back in again all over the place,. Since Drupal 6 has the same issue, we could potentially backport a static (300 extra calls to format_date() on a long thread is potentially enough to warrant it).

Ideal thing for HEAD would be for format_date() to not be such a beast, but no-one's working on that issue and I'm not really up on date handling enough to tackle it, so it's a case of whether we do something interim or try to focus efforts over there.

#9

Yeah, themes are always wanting to do different date formats because every site owner wants their own bikeshed.

Since that new static cache patch landed, this patch needs to be re-worked to use that method, no? /me admits to being too sleepy to look it up at the 'mo. Leaving at "needs review" for now.

#10

#11

Status:postponed» needs review

Un-postponing. @catch: what's your current thinking on this?

#12

#6: format_date.patch queued for re-testing.

#13

Status:needs review» needs work

The last submitted patch, format_date.patch, failed testing.

nobody click here