Repository navigation
perf(@angular/build): verify cache metadata before reading file and eliminate sqlite read-locks - #34042
Conversation
There was a problem hiding this comment.
Code Review
This pull request simplifies the cache key calculation in PersistentLoadResultCache by removing the file content from the key generation, which avoids redundant disk reads when checking the L2 cache. Additionally, it adjusts the flushing behavior in SqliteCacheStore by removing the immediate flush threshold and increasing the timeout. The reviewer suggested simplifying and unifying the deduplication of watch files using a single Set initialization to prevent redundant operations even when filePath is undefined.
…liminate sqlite read-locks Previously, PersistentLoadResultCache.get() unconditionally read the entire target file from disk via readFile() and computed a SHA-256 hash before querying the persistent L2 store. This introduced two primary bottlenecks: 1. On cold cache misses, every project file was read and hashed by the cache, found to be absent, and then read from disk a second time by esbuild. On warm cache hits, reading and hashing multi-megabyte JavaScript files from disk was redundant when a fast stat() check (mtimeMs + size) would confirm whether the cached entry remains valid. 2. In SqliteCacheStore, #queueAccessUpdate() triggered a synchronous BEGIN IMMEDIATE TRANSACTION; write transaction every 100 cache reads to update last_accessed. In multi-process worker pools and parallel builds, this converted concurrent read operations into serialized write transactions that blocked on SQLite busy timeouts. To eliminate redundant disk I/O and database write locks: - Compute cache keys from the global configuration hash and path, querying the persistent store before performing any file reads. - Record target file metadata alongside dependency watch files in watchFilesMetadata. - Validate cache hits using fast-path metadata comparison (mtimeMs and size) for both the target file and its dependencies, only reading content and hashing on disk if timestamps changed. - Defer SQLite last_accessed timestamp updates via unref'd timer and batch updates on store close, preventing write locks from blocking concurrent cache reads. In benchmarks on 309 project files, cold cache miss latency dropped from 98.5 ms to 1.5 ms (65.2x faster), warm cache hit latency dropped from 77.7 ms to 17.9 ms (4.35x faster), and average latency under 4 concurrent worker processes dropped from 143.6 ms to 17.5 ms (8.2x faster).
61ee772 to
57c2ceb
Compare
|
This PR was merged into the repository. The changes were merged into the following branches:
|
|
This pull request has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Summary
When using persistent caching in
@angular/build,PersistentLoadResultCache.get()previously read the entire target file from disk viareadFile()and computed a SHA-256 hash before querying the persistent L2 store.This introduced two primary bottlenecks:
stat()check (mtimeMs+size) would confirm whether the cached entry remains valid.SqliteCacheStore,#queueAccessUpdate()triggered a synchronousBEGIN IMMEDIATE TRANSACTION;write transaction every 100 cache reads to updatelast_accessed. In multi-process worker pools and parallel builds, this converted concurrent read operations into serialized write transactions that blocked on SQLite busy timeouts.Optimizations
watchFilesMetadata.mtimeMsandsize) for both the target file and its dependencies, only reading content and hashing on disk if timestamps changed.last_accessedtimestamp updates via unref'd timer and flushes batch updates on store close, preventing write locks from blocking concurrent cache reads.Benchmark Results
In benchmarks on 309 project files: