@@ -1096,23 +1096,22 @@ function ( array $row ) use ( $predicate ): bool {
10961096 /** @return array<int,array<string,mixed>>|WP_Markdown_Query_Result */
10971097 private function snapshot ( ?array $ autoload_values = null ): array |WP_Markdown_Query_Result {
10981098 $ scope = null === $ autoload_values ? '* ' : implode ( ', ' , array_map ( 'strval ' , $ autoload_values ) );
1099- if ( $ this -> loaded && $ scope === $ this -> snapshot_scope ) {
1100- $ signature = $ this -> options_signature ();
1101- if ( $ signature === $ this ->signature ) {
1102- return $ this ->snapshot ;
1103- }
1099+ // Taken before any read: a file that changes while the snapshot is built
1100+ // makes the next comparison differ, so a stale snapshot is never reused.
1101+ $ signature = $ this ->options_signature ();
1102+ if ( $ this ->loaded && $ scope === $ this -> snapshot_scope && $ signature === $ this -> signature ) {
1103+ return $ this -> snapshot ;
11041104 }
11051105 $ this ->loaded = true ;
11061106 $ this ->snapshot_scope = $ scope ;
1107+ $ this ->signature = $ signature ;
11071108 $ root = $ this ->options_root ();
11081109 if ( $ root instanceof WP_Markdown_Query_Result ) {
1109- $ this ->signature = $ this ->options_signature ();
11101110 return $ this ->snapshot = $ root ;
11111111 }
11121112 if ( null === $ root ) {
11131113 // Verified absence is the only route to an empty snapshot; an
11141114 // unresolved read arrives above as a failure result instead.
1115- $ this ->signature = $ this ->options_signature ();
11161115 return $ this ->snapshot = array ();
11171116 }
11181117
@@ -1124,7 +1123,6 @@ private function snapshot( ?array $autoload_values = null ): array|WP_Markdown_Q
11241123 }
11251124 }
11261125 } catch ( UnexpectedValueException $ error ) {
1127- $ this ->signature = $ this ->options_signature ();
11281126 return $ this ->snapshot = $ this ->failure (
11291127 'markdown_db_native_unsafe_path ' ,
11301128 'unreadable_options_directory ' ,
@@ -1134,7 +1132,6 @@ private function snapshot( ?array $autoload_values = null ): array|WP_Markdown_Q
11341132 if ( $ root !== realpath ( $ this ->state_root . DIRECTORY_SEPARATOR . '_options ' )
11351133 || is_link ( $ this ->state_root . DIRECTORY_SEPARATOR . '_options ' )
11361134 ) {
1137- $ this ->signature = $ this ->options_signature ();
11381135 return $ this ->snapshot = $ this ->failure (
11391136 'markdown_db_native_unsafe_path ' ,
11401137 'changed_options_directory ' ,
@@ -1143,34 +1140,38 @@ private function snapshot( ?array $autoload_values = null ): array|WP_Markdown_Q
11431140 }
11441141
11451142 sort ( $ paths , SORT_STRING );
1143+ // The catalogue answers every file whose identity is unchanged; only new
1144+ // or changed files are read, so one option write no longer forces a
1145+ // re-read of the whole store.
11461146 $ catalogued = $ this ->catalogue ->restore ( $ root , $ paths );
11471147 $ rows = array ();
11481148 $ ids = array ();
11491149 $ names = array ();
1150- $ signatures = array ();
1150+ $ persist_rows = array ();
11511151 $ ordered_signatures = array ();
11521152 foreach ( $ paths as $ offset => $ path ) {
1153- $ path_signature = null === $ catalogued ? null : ( $ catalogued ['signatures ' ][ $ offset ] ?? null );
1154- $ row = null === $ catalogued ? $ this ->read_option ( $ path , $ root , null , $ path_signature ) : ( $ catalogued ['rows ' ][ $ offset ] ?? null );
1155- if ( null !== $ catalogued && null === $ row ) {
1153+ $ stale = null === $ catalogued || ! empty ( $ catalogued ['stale ' ][ $ offset ] );
1154+ $ path_signature = $ stale ? null : ( $ catalogued ['signatures ' ][ $ offset ] ?? null );
1155+ $ row = $ stale ? null : ( $ catalogued ['rows ' ][ $ offset ] ?? null );
1156+ if ( ! $ stale && null === $ row ) {
11561157 $ autoload = $ catalogued ['autoloads ' ][ $ offset ] ?? null ;
11571158 if ( null !== $ autoload_values && is_string ( $ autoload ) && ! in_array ( $ autoload , $ autoload_values , true ) ) {
1158- $ signatures [ basename ( $ path ) ] = ( string ) $ path_signature ;
1159+ $ persist_rows [ ] = array ( ' autoload ' => $ autoload ) ;
11591160 $ ordered_signatures [] = (string ) $ path_signature ;
11601161 continue ;
11611162 }
1163+ }
1164+ if ( null === $ row ) {
11621165 $ row = $ this ->read_option ( $ path , $ root , null , $ path_signature );
11631166 }
11641167 if ( $ row instanceof WP_Markdown_Query_Result ) {
1165- $ this ->signature = $ this ->options_signature ();
11661168 return $ this ->snapshot = $ row ;
11671169 }
11681170 if ( ! is_array ( $ row ) || true !== $ this ->schema ->validate_row ( $ row )
11691171 || basename ( $ path ) !== WP_Markdown_Canonical_Option_Path ::filename ( $ row ['option_name ' ] )
11701172 || isset ( $ ids [ $ row ['option_id ' ] ] )
11711173 || isset ( $ names [ $ row ['option_name ' ] ] )
11721174 ) {
1173- $ this ->signature = $ this ->options_signature ();
11741175 return $ this ->snapshot = $ this ->failure (
11751176 'markdown_db_native_malformed_option ' ,
11761177 'invalid_option_identity ' ,
@@ -1180,18 +1181,17 @@ private function snapshot( ?array $autoload_values = null ): array|WP_Markdown_Q
11801181 $ ids [ $ row ['option_id ' ] ] = true ;
11811182 $ names [ $ row ['option_name ' ] ] = true ;
11821183 $ rows [] = $ row ;
1183- $ signatures [ basename ( $ path ) ] = ( string ) $ path_signature ;
1184- $ ordered_signatures [] = (string ) $ path_signature ;
1184+ $ persist_rows [] = $ row ;
1185+ $ ordered_signatures [] = (string ) $ path_signature ;
11851186 $ key = (string ) $ this ->schema ->value_key ( 'option_name ' , $ row ['option_name ' ] );
11861187 $ this ->option_cache [ $ key ] = array (
11871188 'signature ' => (string ) $ path_signature ,
11881189 'value ' => $ row ,
11891190 );
11901191 }
1191- if ( null === $ catalogued ) {
1192- $ this ->catalogue ->persist ( $ paths , $ rows , $ ordered_signatures );
1192+ if ( null === $ catalogued || ! empty ( $ catalogued [ ' stale ' ] ) ) {
1193+ $ this ->catalogue ->persist ( $ paths , $ persist_rows , $ ordered_signatures );
11931194 }
1194- $ this ->signature = $ this ->options_signature ( $ signatures );
11951195 return $ this ->snapshot = $ rows ;
11961196 }
11971197
@@ -1247,17 +1247,26 @@ private function named_rows( array $values ): array|WP_Markdown_Query_Result {
12471247 return $ rows ;
12481248 }
12491249
1250- /** @param array<string,string>|null $file_signatures */
1251- private function options_signature ( ?array $ file_signatures = null ): string {
1250+ /**
1251+ * Cheap change detector for the whole options store.
1252+ *
1253+ * Built from each file's lstat identity (device, inode, mode, size, mtime,
1254+ * ctime, link count), the same trust basis the option catalogue uses.
1255+ * Canonical writes publish by rename, so every write changes the inode.
1256+ * Hashing every file's contents here read the entire store to answer "did
1257+ * anything change?" and made each reused snapshot cost ~0.5 s on a large site.
1258+ */
1259+ private function options_signature (): string {
12521260 $ directory = $ this ->state_root . DIRECTORY_SEPARATOR . '_options ' ;
12531261 $ parts = array ( $ this ->path_signature ( $ directory ) );
1254- if ( null !== $ file_signatures ) {
1255- $ parts += $ file_signatures ;
1256- } elseif ( is_dir ( $ directory ) && ! is_link ( $ directory ) ) {
1262+ if ( is_dir ( $ directory ) && ! is_link ( $ directory ) ) {
12571263 try {
12581264 foreach ( new FilesystemIterator ( $ directory , FilesystemIterator::SKIP_DOTS ) as $ entry ) {
12591265 if ( str_ends_with ( $ entry ->getFilename (), '.json ' ) ) {
1260- $ parts [ $ entry ->getFilename () ] = $ this ->path_signature ( $ directory . DIRECTORY_SEPARATOR . $ entry ->getFilename () );
1266+ $ stat = @lstat ( $ entry ->getPathname () );
1267+ $ parts [ $ entry ->getFilename () ] = false === $ stat
1268+ ? 'missing '
1269+ : implode ( ': ' , array ( $ stat ['dev ' ], $ stat ['ino ' ], $ stat ['mode ' ], $ stat ['size ' ], $ stat ['mtime ' ], $ stat ['ctime ' ], $ stat ['nlink ' ] ?? 0 ) );
12611270 }
12621271 }
12631272 } catch ( UnexpectedValueException $ error ) {
0 commit comments