Skip to content

GetConnectedRoutesのメモリ負荷を削減 - #1616

Merged
TinyKitten merged 3 commits into
devfrom
agent/connected-routes-memory
Aug 4, 2026
Merged

GetConnectedRoutesのメモリ負荷を削減#1616
TinyKitten merged 3 commits into
devfrom
agent/connected-routes-memory

Conversation

@TinyKitten

@TinyKitten TinyKitten commented Aug 4, 2026

Copy link
Copy Markdown
Member

概要

GetConnectedRoutes の探索時メモリ使用量とSQL負荷を削減します。

原因

探索に必要なのは line_group_cdstation_g_cdpass だけでしたが、各探索階層で駅名・住所・座標・路線属性・列車種別属性を含む完全な Station を取得していました。さらに、経路候補を展開するたびに Vec<Station> を複製していたため、最大4,096状態の上限へ近づくとRaspberry PiでメモリとCPUを大きく消費していました。

変更内容

  • 探索専用の軽量な駅列クエリを追加
    • 取得列を line_group_cdstation_station_types.idstation_g_cdpass に限定
    • 探索に不要なaliases・types等のJOINと巨大な Station の生成を回避
  • 探索状態の Vec<Station> を軽量参照へ置換
  • 候補区間の一時 Vec<Station> 生成を廃止
  • 最大32件の返却候補が確定した後、実際に使用する列車種別の詳細駅データだけを一括取得
  • 到達不能時は詳細駅データのクエリ自体を省略
  • station_station_types.id で軽量駅列と詳細駅列を対応付け、alias JOIN等による行順の差に依存しないようにした
  • アーキテクチャ資料を更新

影響

探索中のメモリ量が「探索状態数 × 完全なStation列」から「探索状態数 × 数値だけの軽量参照」になります。詳細なStationの生成は返却対象経路だけに限定されます。APIレスポンス形式と探索上限は変更しません。

検証

GitHub Actionsですべて成功:

  • cargo test
  • cargo check
  • cargo clippy -- -D warnings
  • cargo fmt --all -- --check
  • CodeRabbit: Approved

@coderabbitai

coderabbitai Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

連結経路探索は軽量な停車参照を使って探索し、候補確定後に必要な線区グループの駅詳細を一括取得する方式へ変更されました。リポジトリには専用の停車情報取得処理を追加しました。

Changes

連結経路探索の遅延ロード

Layer / File(s) Summary
軽量停車参照の取得
stationapi/src/domain/repository/station_repository.rs, stationapi/src/infrastructure/station_repository.rs
ConnectedRoutePatternStop と専用取得メソッドを追加しました。SQL は指定された線区グループの有効な停車情報を取得します。
軽量参照による経路探索
stationapi/src/use_case/interactor/query.rs
探索状態を停車参照へ変更しました。駅グループ単位で訪問済み駅と経路内交差を判定します。
候補確定後の駅詳細復元
stationapi/src/use_case/interactor/query.rs, docs/architecture.md, AGENTS.md
完成候補が使用する線区グループの駅詳細を一括取得し、停車参照から結果を復元します。説明とテスト用停車データを更新しました。

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
  participant QueryInteractor
  participant StationRepository
  participant Database
  QueryInteractor->>StationRepository: 軽量停車参照を取得
  StationRepository->>Database: 連結経路パターン停車情報を検索
  Database-->>StationRepository: 停車参照データ
  StationRepository-->>QueryInteractor: ConnectedRoutePatternStop
  QueryInteractor->>QueryInteractor: 軽量参照で経路候補を探索
  QueryInteractor->>StationRepository: 候補の線区グループから駅詳細を取得
  StationRepository-->>QueryInteractor: 完全な Station データ
  QueryInteractor-->>QueryInteractor: 候補を駅詳細へ復元
Loading

Possibly related PRs

Suggested labels: feature

Poem

ぴょんと参照を軽くして
駅の詳細はあとで取得
経路を静かにたどります
候補が決まれば駅が咲く
月明かりにも速い探索です

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed タイトルはGetConnectedRoutesのメモリ負荷削減という主要変更を明確に示しています。
Description check ✅ Passed 概要、変更内容、テスト結果を具体的に記載しており、変更の目的と影響を十分に説明しています。
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch agent/connected-routes-memory

Comment @coderabbitai help to get the list of available commands.

Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
stationapi/src/use_case/interactor/query.rs (1)

1428-1439: 🗄️ Data Integrity & Integration | 🔵 Trivial | 💤 Low value

変数名のシャドーイングを解消してください。 query.rs:1428detailed_stopsrestored_stops などへ変更し、query.rs:1409 の変数と区別してください。

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@stationapi/src/use_case/interactor/query.rs` around lines 1428 - 1439, Rename
the inner detailed_stops binding in the candidate restoration block to
restored_stops (or an equivalent distinct name), and update its uses so it no
longer shadows the detailed_stops variable near the earlier query logic. No
direct change is required in
stationapi/src/domain/repository/station_repository.rs:62-84.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@stationapi/src/use_case/interactor/query.rs`:
- Around line 1428-1439: Rename the inner detailed_stops binding in the
candidate restoration block to restored_stops (or an equivalent distinct name),
and update its uses so it no longer shadows the detailed_stops variable near the
earlier query logic. No direct change is required in
stationapi/src/domain/repository/station_repository.rs:62-84.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 80f1dcc1-f9f0-44a9-b67a-18ac813d3fa2

📥 Commits

Reviewing files that changed from the base of the PR and between 3acd4e4 and a4d4665.

📒 Files selected for processing (5)
  • AGENTS.md
  • docs/architecture.md
  • stationapi/src/domain/repository/station_repository.rs
  • stationapi/src/infrastructure/station_repository.rs
  • stationapi/src/use_case/interactor/query.rs

@TinyKitten
TinyKitten merged commit 1a4acc6 into dev Aug 4, 2026
7 checks passed
@TinyKitten
TinyKitten deleted the agent/connected-routes-memory branch August 4, 2026 04:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant