Index: src/wp-includes/class-wp-hook.php
===================================================================
--- src/wp-includes/class-wp-hook.php	(revision 55878)
+++ src/wp-includes/class-wp-hook.php	(working copy)
@@ -27,6 +27,14 @@
 	public $callbacks = array();
 
 	/**
+	 * Hook callbacks keys.
+	 *
+	 * @since 6.x
+	 * @var array
+	 */
+	protected $callbacks_keys = array();
+
+	/**
 	 * The priority keys of actively running iterations of a hook.
 	 *
 	 * @since 4.7.0
@@ -86,6 +94,9 @@
 			ksort( $this->callbacks, SORT_NUMERIC );
 		}
 
+
+		$this->callbacks_keys = array_keys( $this->callbacks );
+
 		if ( $this->nesting_level > 0 ) {
 			$this->resort_active_iterations( $priority, $priority_existed );
 		}
@@ -102,7 +113,7 @@
 	 *                                    filter was added. Default false.
 	 */
 	private function resort_active_iterations( $new_priority = false, $priority_existed = false ) {
-		$new_priorities = array_keys( $this->callbacks );
+		$new_priorities = $this->callbacks_keys;
 
 		// If there are no remaining hooks, clear out all running iterations.
 		if ( ! $new_priorities ) {
@@ -187,6 +198,8 @@
 			if ( ! $this->callbacks[ $priority ] ) {
 				unset( $this->callbacks[ $priority ] );
 
+				$this->callbacks_keys = array_keys( $this->callbacks );
+
 				if ( $this->nesting_level > 0 ) {
 					$this->resort_active_iterations();
 				}
@@ -263,10 +276,13 @@
 
 		if ( false === $priority ) {
 			$this->callbacks = array();
+			$this->callbacks_keys = array();
 		} elseif ( isset( $this->callbacks[ $priority ] ) ) {
 			unset( $this->callbacks[ $priority ] );
+			$this->callbacks_keys = array_keys( $this->callbacks );
 		}
 
+
 		if ( $this->nesting_level > 0 ) {
 			$this->resort_active_iterations();
 		}
@@ -289,7 +305,7 @@
 
 		$nesting_level = $this->nesting_level++;
 
-		$this->iterations[ $nesting_level ] = array_keys( $this->callbacks );
+		$this->iterations[ $nesting_level ] = $this->callbacks_keys;
 
 		$num_args = count( $args );
 
@@ -348,7 +364,7 @@
 	 */
 	public function do_all_hook( &$args ) {
 		$nesting_level                      = $this->nesting_level++;
-		$this->iterations[ $nesting_level ] = array_keys( $this->callbacks );
+		$this->iterations[ $nesting_level ] = $this->callbacks_keys;
 
 		do {
 			$priority = current( $this->iterations[ $nesting_level ] );
@@ -481,6 +497,8 @@
 		} else {
 			$this->callbacks[ $offset ] = $value;
 		}
+
+		$this->callbacks_keys = array_keys( $this->callbacks );
 	}
 
 	/**
@@ -495,6 +513,7 @@
 	#[ReturnTypeWillChange]
 	public function offsetUnset( $offset ) {
 		unset( $this->callbacks[ $offset ] );
+		$this->callbacks_keys = array_keys( $this->callbacks );
 	}
 
 	/**
