Toolbar, Privacy: Use array_key_first() to read the first key of an array - #68
Draft
mukeshpanchal27 wants to merge 2 commits into
Draft
Toolbar, Privacy: Use array_key_first() to read the first key of an array#68mukeshpanchal27 wants to merge 2 commits into
mukeshpanchal27 wants to merge 2 commits into
Conversation
…rray. Replace `current( array_keys( $array ) )` with `array_key_first( $array )`. The former builds a complete array of every key only to read the first one and throw the rest away, which costs O(n) time and O(n) memory; `array_key_first()` reads the first bucket directly in constant time and allocates nothing. `wp_admin_bar_new_content_menu()` runs on every admin page load and on every front-end load for logged-in users with the toolbar visible, and its `$actions` array grows with the number of registered post types. `array_key_first()` is available in PHP 7.3 and later, which is below the current minimum supported version. Props mukesh.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Replaces
current( array_keys( $array ) )witharray_key_first( $array )in the two places core uses that idiom.current( array_keys( $a ) )reads a single key, but gets there by materialising a brand-new array containing every key in$a, reading element 0 of that throwaway array, then discarding it. That is O(n) time plus O(n) memory to answer a question PHP can answer in O(1) —array_key_first()reads the first bucket of the hash table directly and allocates nothing.array_key_first()is available in PHP 7.3 and later, below the current minimum supported version, so no polyfill is involved.Benchmark
200,000 iterations per row, PHP 8.x:
current( array_keys( $a ) )array_key_first( $a )The shape matters more than the absolute numbers:
array_key_first()is flat regardless of array size, whilecurrent( array_keys() )grows linearly with data that is never used.Sites changed
src/wp-includes/admin-bar.phpwp_admin_bar_new_content_menu()— runs on every admin page load and on every front-end load for logged-in users with the toolbar visible.$actionsholds one entry per registered post type the user can create, so sites with many CPTs pay proportionally more.src/wp-admin/includes/privacy-tools.php_wp_personal_data_handle_actions()— the retry branch of the privacy request list table.Behaviour
The two idioms differ in exactly one respect: on an empty array,
current( array_keys( $a ) )returnsfalsewhilearray_key_first( $a )returnsnull. Both sites are safe:admin-bar.php— the function returns early ten lines above the change (if ( ! $actions ) { return; }), so$actionsis guaranteed non-empty. The empty case is unreachable.privacy-tools.php— the value goes straight intoabsint(), andabsint( false )andabsint( null )both evaluate to0. Identical result.For every non-empty array both expressions return the same key with the same type. There is also no array-pointer side effect to preserve:
current()was being applied to the temporary array returned byarray_keys(), never to$actionsitself.Testing instructions
Results on this branch:
php -l— no syntax errors in either file.phpcs— clean, 0 errors / 0 warnings.Manually: load any admin screen as a user who can create posts and confirm the toolbar "New" node links to the same target as before.
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See Contributing to WordPress for more information.