Upgrade to Pro — share decks privately, control downloads, hide ads and more …

DIフレームワークは どこまで速くできるのか - Ray.Diのコンパイルとpreload

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.

DIフレームワークは どこまで速くできるのか - Ray.Diのコンパイルとpreload

2026年8月24日に行われた、PHPerKaigi mini #4 の資料です。

connpass: https://phperkaigi.connpass.com/event/401560/

Avatar for Naoki Tsuchiya

Naoki Tsuchiya

August 27, 2026

More Decks by Naoki Tsuchiya

Other Decks in Programming

Transcript

  1. 1 2 3 4 5 6 7 8 9 10

    11 12 13 14 15 // reflection $injector = (new Injector(new FakeCarModule()); $injector->getInstance(CarInterface::class); // serialize $injector = (new Injector(new FakeCarModule()); (new FileSystemCache())->set($key, serialize($injector)); $restored = unserialize(new FileSystemCache()->get($key)); assert($restored instanceof Injector); $restored->getInstance(CarInterface::class); // compile (new Compiler())->compile(new FakeCarModule(), $dir); (new CompiledInjector($dir))->getInstance(CarInterface::class);
  2. 実行戦略とコスト 戦略 ビルド・デプロイ・起動(初回) OPcache/preload リフレクション 関連クラスのパース 関連クラスの require シリアライズ グラフ構築

    関連クラスのパース グラフの serialize と cache 関連クラスの require コンパイル グラフ構築 生成コードのパース 関連クラスのパース 関連クラスの require パース = opcache_compile_file 実行時 グラフ構築 インスタンス生成 cache 読み取りと unserialize インスタンス生成 実行される生成コー ドの require インスタンス生成
  3. 実行戦略ごとの速度比較 1 2 3 4 5 6 7 strategy |

    cold start | steady (build-many) | deploy artifact -------------------------------------------------------------------------------------- reflection | 6.8 ms (build) | 24.5 us | - serialize | 0.07 ms (unserialize) | 24.8 us | 29 KB blob (ser 0.0 ms) compiled | 0.02 ms (new injector) | 11.7 us | 26 scripts (compile 5 ms) -------------------------------------------------------------------------------------- compiled-no-op | 0.02 ms (new injector) | 91.4 us | 26 scripts (compile 4 ms) https://github.com/ray-di/Ray.Compiler/blob/1.x/demo/benchmark/di_benchmark.php
  4. 758 の require を含む preload.php を 9 回実行した際の中央値 preload で

    pm.max_children の値だけ 102 ms 削減できる(理論値) 1 2 3 4 vendor/autoload.php alone the whole preload require list per-process compile avoided 12 ms 114 ms ------ ~102 ms https://github.com/bearsunday/BEAR.Package/issues/492#issuecomment-5284091560
  5. preload の内容による比較 preload 自体が性能の差を決めるわけではない(BEAR.Sunday の場合) 1 2 3 4 variant

    cold ms (median of 3) no preload 73.9 preload *72.3 preload + all scripts -81.2 warm ms -76.0 71.6 *71.4 burst rps 11.6 -9.9 *11.9 steady rps 8.3 -6.8 *11.7 https://github.com/bearsunday/BEAR.Package/issues/492#issuecomment-5284091560