Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Navigation link: prime caches for all posts in menu items #40752

Merged
merged 11 commits into from
May 9, 2022

Conversation

spacedmonkey
Copy link
Member

What?

For menu items in the navigation, instead of loading each post one at a time, load all posts into memory by get all post ids and running them through the _prime_post_caches function.
Fixes #40750.

Why?

This improves database performance.

How?

Testing Instructions

Screenshots or screencast

@spacedmonkey spacedmonkey self-assigned this May 1, 2022
@spacedmonkey spacedmonkey added [Type] Performance Related to performance efforts [Block] Navigation Affects the Navigation Block [Block] Navigation Link Affects the Navigation Link Block labels May 1, 2022
@spacedmonkey spacedmonkey changed the title Fix/prime menu items Navigation link: prime caches for all posts in menu items May 1, 2022
Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@spacedmonkey Mostly looks good, just a few minor comments.

The higher-level feedback though is that I think we should add a test for this.

packages/block-library/src/navigation/index.php Outdated Show resolved Hide resolved
packages/block-library/src/navigation/index.php Outdated Show resolved Hide resolved
packages/block-library/src/navigation/index.php Outdated Show resolved Hide resolved
Copy link
Member

@adamsilverstein adamsilverstein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me

Copy link
Contributor

@costdev costdev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some minor docblock suggestions and some stricter assertions.

phpunit/blocks/render-block-navigation-test.php Outdated Show resolved Hide resolved
phpunit/blocks/render-block-navigation-test.php Outdated Show resolved Hide resolved
lib/compat/wordpress-6.1/navigation.php Outdated Show resolved Hide resolved
lib/compat/wordpress-6.1/navigation.php Outdated Show resolved Hide resolved
lib/compat/wordpress-6.1/navigation.php Outdated Show resolved Hide resolved
lib/compat/wordpress-6.1/navigation.php Outdated Show resolved Hide resolved
lib/compat/wordpress-6.1/navigation.php Outdated Show resolved Hide resolved
@spacedmonkey
Copy link
Member Author

@anton-vlasenko @draganescu @gziolo Any recommendation on this PR to improve the backport. The files in 6.1 compat file do not seem right to be but it was the only way to get the unit tests to run.

Copy link
Contributor

@peterwilsoncc peterwilsoncc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks sensible to me, I've added a note about a more complex test but it would be great to have the cache priming in even if the test waits a little longer.

@adamziel
Copy link
Contributor

adamziel commented May 9, 2022

@spacedmonkey when I checked in the build files (build/block-library/blocks/navigation.php) how do these functions come through when defined with the block, this is what I saw:

/**
 * Get post IDs from a navigation link block instance.
 *
 * @param WP_Block $block Instance of a block.
 *
 * @return array Array of post IDs.
 */
function gutenberg_gutenberg_block_core_navigation_get_post_ids_from_block( $block ) {
	$post_ids = array();

	if ( $block->inner_blocks ) {
		$post_ids = gutenberg_block_core_navigation_get_post_ids( $block->inner_blocks );
	}

	if ( 'core/navigation-link' === $block->name || 'core/navigation-submenu' === $block->name ) {
		if ( $block->attributes && isset( $block->attributes['kind'] ) && 'post-type' === $block->attributes['kind'] ) {
			$post_ids[] = $block->attributes['id'];
		}
	}

	return $post_ids;
}

/**
 * Iterate through all inner blocks recursively and get navigation link block's post IDs.
 *
 * @param WP_Block_List $inner_blocks Block list class instance.
 *
 * @return array Array of post IDs.
 */
function gutenberg_block_core_navigation_get_post_ids( $inner_blocks ) {
	$post_ids = array_map( 'gutenberg_block_core_navigation_get_post_ids_from_block', iterator_to_array( $inner_blocks ) );
	return array_unique( array_merge( ...$post_ids ) );
}

I have no idea where does the double gutenberg_gutenberg_ prefix come from yet, but that's likely the culprit here.

@spacedmonkey
Copy link
Member Author

@adamziel So what do you recommend here? Is this PR okay as it is?

@adamziel
Copy link
Contributor

adamziel commented May 9, 2022

@spacedmonkey I'd like to see these functions bundled with the navigation block since they are only needed there. At the same time, the time before the RC2 is short and this looks like a bug in the build process. I am investigating whether this can be fixed quickly.

@adamziel
Copy link
Contributor

adamziel commented May 9, 2022

@spacedmonkey got it! The matcher finds function block_core_navigation_get_post_ids in that file and replaces block_core_navigation_get_post_ids with gutenberg_block_core_navigation_get_post_ids In the process, block_core_navigation_get_post_ids_from_block gets renamed to gutenberg_block_core_navigation_get_post_ids_from_block. Then, the matcher acts on the block_core_navigation_get_post_ids_from_block, hence the double prefix.

If you rename these functions in such a way that the name of the first is not a substring of a second, it should work. You may also need to call the gutenberg_ prefixed version in the tests.

I've filed an issue to keep track of this problem separately: #40938

@spacedmonkey
Copy link
Member Author

@adamziel Thanks. I have pushed up a change.

@adamziel adamziel added the Backport to WP 6.6 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta label May 9, 2022
@spacedmonkey spacedmonkey merged commit 722d640 into trunk May 9, 2022
@spacedmonkey spacedmonkey deleted the fix/prime_menu_items branch May 9, 2022 17:02
@github-actions github-actions bot added this to the Gutenberg 13.3 milestone May 9, 2022
@adamziel
Copy link
Contributor

I cherry-picked this to wp/6.0 branch to be included in WordPress 6.0 RC2 later today 57e4435

adamziel pushed a commit that referenced this pull request May 10, 2022
* Prime posts in navigation menus.

* Improve logic again.

* Change variable name and function names.

* Feedback and tweaks.

* Add a unit test.

* Fix lints.

* Apply suggestions from code review

Co-authored-by: Colin Stewart <[email protected]>

* Move functions

* Improve comment.

* Add another unit test.

Co-authored-by: Colin Stewart <[email protected]>
@adamziel adamziel removed the Backport to WP 6.6 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta label May 10, 2022
@gziolo
Copy link
Member

gziolo commented May 10, 2022

We will still need to add unit tests separately to WordPress core.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Navigation Link Affects the Navigation Link Block [Block] Navigation Affects the Navigation Block [Type] Performance Related to performance efforts
Projects
No open projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Performance issue with navigation link block
7 participants