Repository navigation
購読の「今すぐ確認」をメインスレッドの外で行い、新着の取り込みを区切りごとに1つのトランザクションで書いて、確認の間アプリが重くなるのを直す - #98
Merged
Merged
Conversation
- 購読の「今すぐ確認」と、購読を足した直後の確認は、画面(viewModelScope = メインスレッド)から呼ばれ、フィードの XML(1件で数 MB・最大 100 件)を メインスレッドで読んでいた。SubscriptionRepositoryImpl.checkNow を背景(io)で行う - 取り込み(importLinks)は1件ずつ別々に書いていたため、書き込みのたびに一覧などの見ている問い合わせが全件を読み直していた (購読の新着 30 件で約 90 回)。50 件ごとに1つのトランザクションで書き、入れたばかりのものを処理待ちにし直す書き込みもやめる - 確認・フィードの読み込み・取り込みの区間を Perfetto のトレースに出す(androidx.tracing:tracing-ktx 1.2.0 を足した) - debug ビルドでは StrictMode で、メインスレッドでのディスクの読み書き・通信を Logcat に出す - 測るテスト(ImportPerformanceTest・FeedParserPerformanceTest)と、確認が背景で動くことのテストを足した Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Nov 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
概要
利用者の報告「購読機能でのチェック処理のときに、ときどきとても重くなる」の調査の1本目。購読の確認の経路にあった2つの原因を直す。
原因(根拠)
SubscriptionViewModel.checkNow/addはviewModelScope.launch(メインスレッド)からSubscriptionRepositoryImpl.checkNowを呼ぶが、checkNowだけwithContextが無かった。FeedFetcher.fetchは通信の間だけ io に移り、戻るとメインスレッドで続くため、FeedParser.parse(jsoup の XML。1件で最大 2 MB、1回に最大 100 件のフィード)とplanNewItemsがメインスレッドで動いていた。夜間の
FeedCheckWorkerは CoroutineWorker(Dispatchers.Default)なので問題ないimportLinks)が1件ずつ別々に書いていた新着 30 件(1回の確認の上限)で、追加 30 回 + タグ付け 30 回 + 処理待ちにし直す 30 回 = 約 90 回の書き込み。そのたびに
bookmarksが変わったと知らされ、一覧(
observeFiltered)や全件の問い合わせ(getAllWithTags)など、見ている問い合わせが全件(本文・タグ込み)を読み直していた変更内容
SubscriptionRepositoryImpl.checkNowをwithContext(dispatcherProvider.io)で行う(ほかのメソッドと同じ)importLinksは 50 件(進み具合を知らせる区切り)ごとに1つのトランザクションで書く(BookmarkDao.inTransaction。ほかの DAO の書き込みも含められる)。入れたばかりのものはすでに処理待ち(
queuedAtは今)なので、markQueuedで書き直さずに順番の列に積む。1件の失敗・重複の数え方、進み具合の通知の値は変わらないFeedChecker.check(非同期の区間)・FeedParser.parse・BookmarkRepository.importLinks(非同期の区間)。androidx.tracing:tracing-ktx:1.2.0を足したStrictMode.ThreadPolicy(ディスクの読み書き・通信、penaltyLog)を有効にする。止めずに Logcat(タグStrictMode)に出す測った結果
ImportPerformanceTest(Robolectric): 5,000 件(本文 約 2,000 字・タグ 2 つ)のある DB で、一覧の問い合わせと全件の問い合わせを見ている間に、新着 30 件を取り込む。FeedParserPerformanceTest(JVM、PC): 1,629 KB・100 件のフィードを読むのに中央値 13〜21 ms。端末では数倍かかり、変更前はこれをフィードの数だけメインスレッドで続けていたSubscriptionRepositoryImplTestに、checkNowが呼び出し元のスレッドではなく背景で動くことのテストを足した(変更前は呼び出し元のスレッドで保存まで動く)テスト
./gradlew :app:assembleDebug :app:testDebugUnitTest :app:lintDebug(絞り込みなし)を通した: 単体テスト 2,514 件・失敗 0(スキップ 4)、Lint の問題なし端末で確かめること(未検証)
Perfetto(または Android Studio の System Trace)で
FeedChecker.check・FeedParser.parse・BookmarkRepository.importLinksの区間と、メインスレッドの長いフレームを見るStrictModeを見ないと分からない(Robolectric は検出しない)RediscoverWidgetUpdater)が、ウィジェットを置いていなくてもアプリの動いている間ずっと全件(本文・タグ込み)を見ていて、AI 処理の段が進むたび・取り込みのたびに全件を読み直している🤖 Generated with Claude Code