merge call chain with flatMap (slight performance improvement)

This commit is contained in:
davotoula
2026-03-06 21:49:35 +01:00
parent a89bfd0b05
commit 4e614a3179
5 changed files with 32 additions and 34 deletions
@@ -1898,7 +1898,7 @@ class Account(
fun getRelevantReports(note: Note): Set<Note> { fun getRelevantReports(note: Note): Set<Note> {
val innerReports = val innerReports =
if (note.event is RepostEvent || note.event is GenericRepostEvent) { if (note.event is RepostEvent || note.event is GenericRepostEvent) {
note.replyTo?.map { getRelevantReports(it) }?.flatten() ?: emptyList() note.replyTo?.flatMap { getRelevantReports(it) } ?: emptyList()
} else { } else {
emptyList() emptyList()
} }
@@ -2770,10 +2770,9 @@ object LocalCache : ILocalCache, ICacheProvider {
val childrenToBeRemoved = mutableListOf<Note>() val childrenToBeRemoved = mutableListOf<Note>()
val toBeRemoved = val toBeRemoved =
account.hiddenUsers.flow.value.hiddenUsers account.hiddenUsers.flow.value.hiddenUsers.flatMap { userHex ->
.map { userHex ->
(notes.filter { _, it -> it.event?.pubKey == userHex } + addressables.filter { _, it -> it.event?.pubKey == userHex }).toSet() (notes.filter { _, it -> it.event?.pubKey == userHex } + addressables.filter { _, it -> it.event?.pubKey == userHex }).toSet()
}.flatten() }
toBeRemoved.forEach { toBeRemoved.forEach {
removeFromCache(it) removeFromCache(it)
@@ -170,7 +170,7 @@ internal fun <T> Iterable<T>.fillToSize(
transform: (List<T>) -> T, transform: (List<T>) -> T,
): List<T> { ): List<T> {
val capacity = ceil(size.safeDiv(count())).roundToInt() val capacity = ceil(size.safeDiv(count())).roundToInt()
return map { data -> List(capacity) { data } }.flatten().chunkToSize(size, transform) return flatMap { data -> List(capacity) { data } }.chunkToSize(size, transform)
} }
internal fun <T> Iterable<T>.chunkToSize( internal fun <T> Iterable<T>.chunkToSize(
@@ -66,15 +66,15 @@ fun DisplayUncitedHashtags(
val tagsInContent = val tagsInContent =
state state
.paragraphs .paragraphs
.map { .flatMap { paragraphState ->
it.words.mapNotNull { paragraphState.words.mapNotNull {
if (it is HashTagSegment) { if (it is HashTagSegment) {
it.hashtag it.hashtag
} else { } else {
null null
} }
} }
}.flatten() }
val unusedHashtags = val unusedHashtags =
tagsInEvent.filterNot { eventTag -> tagsInEvent.filterNot { eventTag ->
@@ -76,8 +76,7 @@ fun filterUserProfilePosts(
user.outboxRelays()?.ifEmpty { null } user.outboxRelays()?.ifEmpty { null }
?: (user.allUsedRelays() + LocalCache.relayHints.hintsForKey(user.pubkeyHex)) ?: (user.allUsedRelays() + LocalCache.relayHints.hintsForKey(user.pubkeyHex))
return relays return relays.flatMap { relay ->
.map { relay ->
listOf( listOf(
RelayBasedFilter( RelayBasedFilter(
relay = relay, relay = relay,
@@ -100,5 +99,5 @@ fun filterUserProfilePosts(
), ),
), ),
) )
}.flatten() }
} }