xcrun simctl runtime dyld_shared_cacheは、シミュレータランタイムが起動時に使う共有ライブラリキャッシュ(dyld shared cache)を更新・削除するサブコマンドである。
ランタイムの指定方法に注意する
update/removeともに対象のランタイムを指定する必要がある。
xcrun simctl runtime listでインストール済みのiOSシミュレータランタイムを確認する
で表示されるUUIDを指定してみたところ、実際にはInvalid runtimeというエラーになった。
$ xcrun simctl runtime dyld_shared_cache update D5C38F8E-E94E-40BF-B664-991E000343C2
Invalid runtime: D5C38F8E-E94E-40BF-B664-991E000343C2
代わりにcom.apple.CoreSimulator.SimRuntime.iOS-17-5のような識別子(runtime list -jのJSON出力に含まれるruntimeIdentifierフィールドの値)を指定する必要がある。
$ xcrun simctl runtime dyld_shared_cache update com.apple.CoreSimulator.SimRuntime.iOS-17-5
Updating dyld shared cache if needed for runtime: iOS 17.5 (17.5 - 21F79) - com.apple.CoreSimulator.SimRuntime.iOS-17-5.
dyld shared cache is usable at path: '/Library/Developer/CoreSimulator/Caches/dyld/25G83/com.apple.CoreSimulator.SimRuntime.iOS-17-5.21F79/'.
キャッシュを更新する
updateは、キャッシュが既に存在する場合は再構築せず即座に終了する。
実際に計測したところ、キャッシュが存在する状態での実行は0.14秒で完了した。
一方、キャッシュが存在しない状態(後述のremove実行後など)でupdateを実行すると、実際にキャッシュの構築が行われる。
このときは47秒かかった。
キャッシュを強制的に再構築する
--forceを付けると、キャッシュが既に存在していても削除してから再構築する。
$ xcrun simctl runtime dyld_shared_cache update com.apple.CoreSimulator.SimRuntime.iOS-17-5 --force
Rebuilding dyld shared cache for runtime: iOS 17.5 (17.5 - 21F79) - com.apple.CoreSimulator.SimRuntime.iOS-17-5.
dyld shared cache is usable at path: '/Library/Developer/CoreSimulator/Caches/dyld/25G83/com.apple.CoreSimulator.SimRuntime.iOS-17-5.21F79/'.
このときは56秒かかった。
出力メッセージもUpdating ... if neededからRebuildingに変わっており、実際に再構築が行われたと分かる。
キャッシュを削除する
removeでキャッシュを削除できる。
$ xcrun simctl runtime dyld_shared_cache remove com.apple.CoreSimulator.SimRuntime.iOS-17-5
Removed dyld shared cache for runtime: iOS 17.5 (17.5 - 21F79) - com.apple.CoreSimulator.SimRuntime.iOS-17-5.
実際に削除後のパスを確認したところ、キャッシュのディレクトリ自体が存在しなくなっていた。
削除はupdateの--forceと違って即座に完了し、実測でも0.3秒程度だった。
存在しないランタイムを指定するとエラーになる
存在しないランタイム識別子を指定すると、UUIDを指定した場合と同様にInvalid runtimeというエラーになる。
$ xcrun simctl runtime dyld_shared_cache update com.apple.CoreSimulator.SimRuntime.iOS-99-9
Invalid runtime: com.apple.CoreSimulator.SimRuntime.iOS-99-9
全ランタイムをまとめて操作する
ヘルプによると、--allを指定すると対象ランタイムをインストール済みの全ランタイムに広げられる。
# ヘルプの記載に基づく例(未実行)
$ xcrun simctl runtime dyld_shared_cache update --all
全ランタイム分のキャッシュ構築には相応の時間がかかると予想されるため、本記事では個別のランタイムを指定した検証にとどめ、--all自体は実行していない。
活用例: CI環境でのキャッシュ事前構築
CIでシミュレータを使ったテストを実行する場合、初回のシミュレータ起動時にキャッシュ構築が走ると、その分テストの実行時間が延びてしまう。
テスト実行前のセットアップ手順にdyld_shared_cache updateを組み込んでおけば、キャッシュ構築を事前に済ませ、テスト本体の実行時間への影響を減らせる。
