Make WordPress Core

Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#55428 closed enhancement (fixed)

Improve cache priming in wp_get_nav_menu_items

Reported by: spacedmonkey's profile spacedmonkey Owned by: spacedmonkey's profile spacedmonkey
Milestone: 6.0 Priority: normal
Severity: normal Version: 3.0
Component: Menus Keywords: has-patch has-dev-note
Focuses: performance Cc:

Description

In the wp_get_nav_menu_items, there are calls to get_terms and get_posts to prime object caches. There are new and better functions calls, _prime_post_caches and _prime_term_caches to prime caches.

Change History (8)

This ticket was mentioned in PR #2437 on WordPress/wordpress-develop by spacedmonkey.


2 years ago
#1

  • Keywords has-patch added

spacedmonkey commented on PR #2437:


2 years ago
#2

This PR has a number of benefits.

  • For menus with multiple post types, multiple SQL requires and replaced with a simple query.
  • For menus with multiple taxonomy terms, multiple SQL requires and replaced with a simple query.
  • If posts / terms are already in memory, no query at all.
  • Those using object caching, will have caches primed.
  • Those that use object cache that support multiple gets, will get caches primed in one object cache call.
  • One less call to get_post_meta
  • Much simplier code

#3 @peterwilsoncc
2 years ago

  • Milestone changed from Awaiting Review to 6.0

The PR bot missed my review. Linked for reference.

While testing the pull request, I discovered the current priming of the term cache misses and each term is queried individually. I've moved this to the 6.0 milestone as I think it warrants it for the potential performance boost.

Unit test notes:

  • it would be good to make sure the caches continue to be hit during the fetching and rendering of menus. If tests currently exist, I suspect they are not testing a render, terms or both but this needs confirming.

Manual testing notes:

  • REST API endpoint for FSE

#4 @spacedmonkey
2 years ago

  • Owner set to spacedmonkey
  • Status changed from new to assigned

#5 @spacedmonkey
2 years ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 52975:

Menus: Improve cache priming in the wp_get_nav_menu_items function.

In wp_get_nav_menu_items multiple function calls to get_terms and get_posts were being used to load post/term objects into
memory. This change replaces calls to get_terms and get_posts with calls to _prime_post_caches and 
_prime_term_caches. These functions are designed to prime object caches and do not have the overhead of a query object. This
change also replaces multiple queries with a single query, saving many SQL queries per page load.

Props Spacedmonkey, peterwilsoncc.
Fixes #55428.

--This line, and those below,

will be ignored--

M src/wp-includes/nav-menu.php

spacedmonkey commented on PR #2437:


2 years ago
#6

Merged.

#7 @spacedmonkey
2 years ago

Dev note ready for review.

Note: See TracTickets for help on using tickets.