tools/pack_shader_cache.py reads the loose files with 8 bounded readers

pack() read every loose file one after the other. On Windows each open costs milliseconds (the antivirus scans it), so
folding a fresh cache was dominated by waiting: 12000 loose files took 79-90 s. The files are now read ahead by a pool
of 8 threads (--read-workers, 1..16; 1 reads serially), at most twice that many in flight, and written in the same
sorted order, so the pack is byte-identical. 12000 files: 18-20 s. The CLI prints read and cleanup progress every 5 s.
tests/pack_shader_cache_test.py checks parallel against serial packs, fresh and incremental, and the bounded read-ahead.
This commit is contained in:
Ruan Shannon committed 2026-11-22 05:10:48 -08:00
1 parent afae80c908
commit b4dcc3431f
2 files changed
+75 -9

No files matched your search

+25
View File
@@ -9,6 +9,7 @@ import struct
import sys
import tempfile
import unittest
import shutil
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
@@ -100,6 +101,30 @@ class PackTest(unittest.TestCase):
self.assertTrue((self.cache / f'{k}.spv').exists())
self.assertEqual(len(read_pack(self.cache / psc.PACK)), 4)
def test_parallel_pack_is_byte_identical_for_fresh_and_incremental_inputs(self):
for i in range(80):
self.shader(str(i))
serial = self.cache.parent / 'serial'
shutil.copytree(self.cache, serial)
self.assertEqual(psc.pack(self.cache, read_workers=8), psc.pack(serial, read_workers=1))
self.assertEqual((self.cache / psc.PACK).read_bytes(), (serial / psc.PACK).read_bytes())
self.shader('new')
(self.cache / f'{name("0")}.res.json').write_bytes(b'{"changed":true}')
(self.cache / f'{name("use1")}.use').write_bytes(b'invalid loose copy')
shutil.copytree(self.cache, serial, dirs_exist_ok=True)
self.assertEqual(psc.pack(self.cache, read_workers=8), psc.pack(serial, read_workers=1))
self.assertEqual((self.cache / psc.PACK).read_bytes(), (serial / psc.PACK).read_bytes())
def test_prefetch_preserves_order_and_bounds_inflight_reads(self):
keys = [(1, bytes([i])) for i in range(100)]
# The first yielded result must not require reading every file; only the bounded prefetch can start.
from unittest.mock import patch
with patch.object(psc, 'read_loose', return_value=b'data') as read:
iterator = psc.prefetched_files(keys, dict.fromkeys(keys, Path('fixture')), 3)
self.assertEqual(next(iterator), (keys[0], b'data'))
self.assertLessEqual(read.call_count, 6)
self.assertEqual([key for key, _ in iterator], keys[1:])
def test_a_broken_pack_is_rebuilt_from_the_loose_files(self):
self.shader('a')
(self.cache / psc.PACK).write_bytes(b'garbage')