Skip to content

Commit 12e3fc6

Browse files
committed
Block Processor: Inline per-token checks when extracting blocks.
`extract_full_block_and_advance()` called `get_depth()` for every token, `get_html_content()` for every HTML span, and `opens_block()` for every delimiter. Compute the same values in place. Extracting every top-level block takes 3% to 9% less time. See #66138.
1 parent 070704b commit 12e3fc6

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

‎src/wp-includes/class-wp-block-processor.php‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,9 +1292,16 @@ public function extract_full_block_and_advance(): ?array {
12921292
);
12931293

12941294
$depth = $this->get_depth();
1295-
while ( $this->next_token() && $this->get_depth() > $depth ) {
1296-
if ( self::HTML_SPAN === $this->state ) {
1297-
$chunk = $this->get_html_content();
1295+
while ( $this->next_token() ) {
1296+
$is_html = self::HTML_SPAN === $this->state;
1297+
1298+
// Equivalent to `$this->get_depth() <= $depth`.
1299+
if ( count( $this->open_blocks_at ) + ( $is_html || self::VOID === $this->type ? 1 : 0 ) <= $depth ) {
1300+
break;
1301+
}
1302+
1303+
if ( $is_html ) {
1304+
$chunk = substr( $this->source_text, $this->after_previous_delimiter, $this->matched_delimiter_at - $this->after_previous_delimiter );
12981305
$block['innerHTML'] .= $chunk;
12991306
$block['innerContent'][] = $chunk;
13001307
continue;
@@ -1306,7 +1313,7 @@ public function extract_full_block_and_advance(): ?array {
13061313
* @todo This is a decent place to call {@link \render_block()}
13071314
* @todo Use iteration instead of recursion, or at least refactor to tail-call form.
13081315
*/
1309-
if ( $this->opens_block() ) {
1316+
if ( self::CLOSER !== $this->type ) {
13101317
$inner_block = $this->extract_full_block_and_advance();
13111318
$block['innerBlocks'][] = $inner_block;
13121319
$block['innerContent'][] = null;
@@ -1318,7 +1325,7 @@ public function extract_full_block_and_advance(): ?array {
13181325
* moving on to the next token at the start of the next loop iteration.
13191326
*/
13201327
if ( self::HTML_SPAN === $this->state ) {
1321-
$chunk = $this->get_html_content();
1328+
$chunk = substr( $this->source_text, $this->after_previous_delimiter, $this->matched_delimiter_at - $this->after_previous_delimiter );
13221329
$block['innerHTML'] .= $chunk;
13231330
$block['innerContent'][] = $chunk;
13241331
}

0 commit comments

Comments
 (0)