[DOC-CVS] [doc-base] master: Optimize xinclude_run_byid by caching elements by xml:id (#300)

[email protected] (Jordi Kroon via GitHub) Mon, 15 Jun 2026 16:35:02 +0000
Newsgroups php.doc.cvs
Message-ID <[email protected]>
Author: Jordi Kroon (jordikroon)
Committer: GitHub (web-flow)
Pusher: jordikroon
Date: 2026-06-15T18:34:59+02:00

Commit: https://github.com/php/doc-base/commit/d0d9c8b2649646249e75e93618ff6a1baeba8beb
Raw diff: https://github.com/php/doc-base/commit/d0d9c8b2649646249e75e93618ff6a1baeba8beb.diff

Optimize xinclude_run_byid by caching elements by xml:id (#300)

Changed paths:
  M  configure.php


Diff:

diff --git a/configure.php b/configure.php
index 8f83848a5f..b8851a1280 100755
--- a/configure.php
+++ b/configure.php
@@ -704,17 +704,25 @@ function xinclude_run_byid( DOMDocument $dom )
     // so we need to *simulate* its *recursive* nature here.
 
     $total = 0;
+    $xpath = new DOMXPath( $dom );
+    $xpath->registerNamespace( "xi" , "http://www.w3.org/2001/XInclude" );
+
+    // Resolve xpointers via a precomputed map; on duplicate xml:ids, first wins.
+    // Avoids quadratic tree walks (~ 90% performance gain).
+    $byId = [];
+    foreach( $xpath->query( "//*[@xml:id]" ) as $node ) {
+        $byId[$node->getAttribute("xml:id")] ??= $node;
+    }
+
     for( $run = 0 ; $run < 10 ; $run++ )
     {
-        $xpath = new DOMXPath( $dom );
-        $xpath->registerNamespace( "xi" , "http://www.w3.org/2001/XInclude" );
         $xincludes = $xpath->query( "//xi:include" );
 
         $changed = false;
         foreach( $xincludes as $xinclude )
         {
             $xpointer = $xinclude->getAttribute( "xpointer" );
-            $target = $xinclude->ownerDocument->getElementById( $xpointer );
+            $target = $byId[ $xpointer ] ?? null;
 
             if ( $target == null )
                 continue;