Skip to content

perf: Windows/macOSでのエミュレーション速度不足と描画スキップを解消 - #1

Merged
bubio merged 10 commits into
mainfrom
perf-improvements
Oct 5, 2026
Merged

bubio merged 10 commits into
mainfrom
perf-improvements

Conversation

@bubio

@bubio bubio commented Oct 5, 2026

Copy link
Copy Markdown
Owner

概要

Dragon Quest III (GBC) を対象に、エミュレーション本体の高速化と、オーディオ駆動ペーシングまわりの描画スキップ対策を行った。

主な変更

  • コアの高速化
    • Timer/PPU/DMA を CPU のメモリアクセスごとに進めるのをやめ、次のイベント時刻か I/O アクセスまで遅延同期するようにした
    • オペコードをビットフィールドの決定木でディスパッチし、フレームを CpuRun で回す
    • ROM/WRAM/HRAM をキャッシュ済みのバンク窓から直接読む
    • スキャンライン描画を分割・展開し、ARGB パレットをキャッシュした
    • HALT サイクルの一括処理、APU/PPU/Timer のイベント境界までの一括進行、MBC バンク折り返しのビットマスク化
  • 描画スキップ対策
    • 起動時に無音をペーシング目標までプリフィルする
    • macOS ではオーディオデバイスのバッファを 512 サンプルフレームにする
    • Windows (D3D11) で起動直後の Present が詰まる問題に対し、AudioInit の前に黒画面を 10 回 Present して詰まりを先に済ませる
  • ビルド: build_macos.sh で pkg-config が SDL3 を見つけられないとき brew --prefix にフォールバックする

結果

  • DQ3 ヘッドレス 1800 フレーム: 実行命令数 119.6G → 43.5G、フレームバッファのハッシュは一致
  • M4 Mac: 60 秒 × 3 回のウィンドウ実行で描画スキップ 0
  • Windows: 起動 5 回と 60 秒 × 3 回の実行で描画スキップ 0
  • テストの結果は変更前と同じ(Release/Debug)

詳細は docs/PerformanceNotes.md の 19・20 章以降を参照。

🤖 Generated with Claude Code

bubio and others added 10 commits September 5, 2026 23:19
同じBubiBoy Lite移植の姉妹プロジェクトharegirl(Hare言語)の高速化
コミットを監査し、RuxBoyに取り込めるものを移植した。ROM/RAMバンク数は
仕様上常に2の冪であるにもかかわらず、CPU全命令のオペコードフェッチを
含む最も熱いパスであるNormalizedRomBank/NormalizedRamBankが剰余演算
(bank % count)でバンクを折り返していたため、bank & (count - 1)へ
置き換えた。

APUのチャンネル周期タイマーを閉じた式(O(1))化する対策も試作したが、
CpuPerfベンチで221-227%→216-221%realtimeの退行を実測したため差し戻した
(RuxBoyはBusTickを4/8 T-cycle単位でしか呼ばず、haregirlのように
HALT待ちを一括バッチしないため、除算コストが短いインラインループを
上回った)。経緯・却下理由の一覧はdocs/PerformanceNotes.md 7章、
docs/DevelopmentNotes.mdに記録。

scripts/test.sh --release 全PASS(既知の3件のexpected-failのみ、差分なし)。

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016LsgvHbRrFDtbYJqpaCQh3
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Frame skips on M4 came from a shallow audio queue, not emulation speed
(~6ms/frame). Prefill silence up to the pacing target at startup and
request 512 sample frames on macOS so CoreAudio drains in smaller steps.
Dragon Quest III: 0 skipped frames in 3x60s windowed runs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Defer Timer/PPU/DMA updates until the next event time or an I/O access
instead of ticking them on every CPU memory access. Dispatch opcodes with
a bit-field decision tree, read ROM/WRAM/HRAM directly via cached bank
windows, run frames through CpuRun, and split/unroll the scanline
renderer with a cached ARGB palette.

Dragon Quest III headless 1800 frames: 119.6G -> 43.5G instructions
retired, identical framebuffer hash. All tests unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
On Windows (D3D11) the 4th-5th SDL_RenderPresent after the window is
shown took ~75ms in total, exhausting the ~66ms silence prefill and
causing 1-2 skipped frames right after startup. Present a black frame
10 times before AudioInit so the stall happens before audio playback.
Dragon Quest III opening: 0 skipped frames in 5 startups and 3x60s runs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@bubio
bubio merged commit 2d958fd into main Oct 5, 2026
4 checks passed
@bubio
bubio deleted the perf-improvements branch October 5, 2026 09:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant