Skip to content

Commit e1b286d

Browse files
authored
Merge pull request libre-tube#7214 from FineFindus/feat/increase-batch-size
perf(LocalFeedRepository): speed up local feed extraction
2 parents 51ab533 + 7f52592 commit e1b286d

1 file changed

Lines changed: 23 additions & 13 deletions

File tree

app/src/main/java/com/github/libretube/repo/LocalFeedRepository.kt

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -82,31 +82,32 @@ class LocalFeedRepository : FeedRepository {
8282
if (channelIds.isEmpty()) return
8383

8484
val totalExtractionCount = AtomicInteger()
85-
val chunkedExtractionCount = AtomicInteger()
85+
val channelExtractionCount = AtomicInteger()
8686
withContext(Dispatchers.Main) {
8787
onProgressUpdate(FeedProgress(0, channelIds.size))
8888
}
8989

9090
for (channelIdChunk in channelIds.chunked(CHUNK_SIZE)) {
91-
// add a delay after each BATCH_SIZE amount of visited channels
92-
val count = chunkedExtractionCount.get();
91+
val count = channelExtractionCount.get();
9392
if (count >= BATCH_SIZE) {
94-
delay(BATCH_DELAY.random())
95-
chunkedExtractionCount.set(0)
93+
// add a delay after each BATCH_SIZE amount of fully-fetched channels
94+
delay(CHANNEL_BATCH_DELAY.random())
95+
channelExtractionCount.set(0)
9696
}
9797

9898
val collectedFeedItems = channelIdChunk.parallelMap { channelId ->
9999
try {
100-
getRelatedStreams(channelId, minimumDateMillis)
100+
getRelatedStreams(channelId, minimumDateMillis).also {
101+
if (it.isNotEmpty())
102+
// increase counter if we had to fully fetch the channel
103+
channelExtractionCount.incrementAndGet()
104+
}
101105
} catch (e: Exception) {
102106
Log.e(channelId, e.stackTraceToString())
103107
null
104108
} finally {
105-
chunkedExtractionCount.incrementAndGet()
106-
val currentProgress = totalExtractionCount.incrementAndGet()
107-
108109
withContext(Dispatchers.Main) {
109-
onProgressUpdate(FeedProgress(currentProgress, channelIds.size))
110+
onProgressUpdate(FeedProgress(totalExtractionCount.incrementAndGet(), channelIds.size))
110111
}
111112
}
112113
}.filterNotNull().flatten().map(StreamItem::toFeedItem)
@@ -161,17 +162,26 @@ class LocalFeedRepository : FeedRepository {
161162
}
162163

163164
companion object {
164-
private const val CHUNK_SIZE = 2
165+
/**
166+
* Amount of feeds that are fetched concurrently.
167+
*
168+
* Should ideally be a factor of `BATCH_SIZE` to be correctly applied.
169+
*/
170+
private const val CHUNK_SIZE = 5
165171

166172
/**
167173
* Maximum amount of feeds that should be fetched together, before a delay should be applied.
168174
*/
169175
private const val BATCH_SIZE = 50
170176

171177
/**
172-
* Millisecond delay between two consecutive batches to avoid throttling.
178+
* Millisecond delay after fetching `BATCH_SIZE` channels to avoid throttling.
179+
*
180+
* A channel is only counted as fetched when it had a recent upload, requiring to fetch
181+
* the channelInfo via Innertube.
173182
*/
174-
private val BATCH_DELAY = (500L..1500L)
183+
private val CHANNEL_BATCH_DELAY = (500L..1500L)
184+
175185
private const val MAX_FEED_AGE_DAYS = 30L // 30 days
176186
}
177187
}

0 commit comments

Comments
 (0)