[wp-trac] [WordPress Trac] #51691: Call get_template_directory() once instead of multiple times for loading theme's files (performance boost)

WordPress Trac noreply at wordpress.org
Sun Nov 1 19:36:48 UTC 2020


#51691: Call get_template_directory() once instead of multiple times for loading
theme's files (performance boost)
----------------------------+------------------------------
 Reporter:  hellofromTonya  |       Owner:  (none)
     Type:  enhancement     |      Status:  new
 Priority:  normal          |   Milestone:  Awaiting Review
Component:  Bundled Theme   |     Version:  trunk
 Severity:  normal          |  Resolution:
 Keywords:                  |     Focuses:  performance
----------------------------+------------------------------
Description changed by hellofromTonya:

Old description:

> In the bundled themes, `get_template_directory()` is invoked for loading
> each of the theme's files, i.e. in the theme's `functions.php` file.
>
> This degrades the performance of the theme's initialization. How so?
> ''Each time'' `get_template_directory()` runs, ''multiple'' other
> functions and filters also run including these 5 hooks:
>
> - `pre_option_template`
> - `alloptions`
> - `option_template`
> - `theme_root`
> - `template_directory`
>
> The number of times `get_template_directory()` is called for loading
> theme files:
>
> - Twenty Twenty One: 12 times
> - Twenty Twenty: 12 times
> - Twenty Nineteen: 10 times
> - etc.
>
> This ticket proposes a performance gain for the bundled themes by:
>
> - replacing the `get_template_directory()` with a private memoized theme
> function
> - calling `get_template_directory()` only once and then storing it into a
> `static` variable for reuse within the private function
>

> This change reduces memory, CPU cycles, and processing time.

New description:

 In the bundled themes, `get_template_directory()` is invoked for loading
 each of the theme's files, i.e. in the theme's `functions.php` file.

 This degrades the performance of the theme's initialization. How so?
 ''Each time'' `get_template_directory()` runs, ''multiple'' other
 functions and filters also run including these 5 hooks:

 - `pre_option_template`
 - `alloptions`
 - `option_template`
 - `theme_root`
 - `template_directory`

 The number of times `get_template_directory()` is called for loading theme
 files:

 - Twenty Twenty One: 12 times
 - Twenty Twenty: 12 times
 - Twenty Nineteen: 10 times
 - etc.

 This ticket proposes to call `get_template_directory()` only once for
 loading of theme's local files.

 There are different ways to achieve this a performance gain.

 One way:

 - replacing the `get_template_directory()` with a private memoized theme
 function
 - calling `get_template_directory()` only once and then storing it into a
 `static` variable for reuse within the private function

 Another way:
 - Store in a variable and reuse

 Another way:
 - Implement within Core


 The overall goal is to improve performance by reducing memory, CPU cycles,
 and processing time.

--

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/51691#comment:5>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list