Improves the debug of the memory use per event kind

This commit is contained in:
Vitor Pamplona 2024-05-24 19:26:39 -04:00
parent 2c2e4a1694
commit cd008d78a0
2 changed files with 49 additions and 6 deletions

View File

@ -990,6 +990,8 @@ fun debugState(context: Context) {
"Notes: " +
LocalCache.notes.filter { _, it -> it.liveSet != null }.size +
" / " +
LocalCache.notes.filter { _, it -> it.flowSet != null }.size +
" / " +
LocalCache.notes.filter { _, it -> it.event != null }.size +
" / " +
LocalCache.notes.size(),
@ -999,6 +1001,8 @@ fun debugState(context: Context) {
"Addressables: " +
LocalCache.addressables.filter { _, it -> it.liveSet != null }.size +
" / " +
LocalCache.addressables.filter { _, it -> it.flowSet != null }.size +
" / " +
LocalCache.addressables.filter { _, it -> it.event != null }.size +
" / " +
LocalCache.addressables.size(),
@ -1008,6 +1012,8 @@ fun debugState(context: Context) {
"Users: " +
LocalCache.users.filter { _, it -> it.liveSet != null }.size +
" / " +
LocalCache.users.filter { _, it -> it.flowSet != null }.size +
" / " +
LocalCache.users.filter { _, it -> it.latestMetadata != null }.size +
" / " +
LocalCache.users.size(),
@ -1020,12 +1026,22 @@ fun debugState(context: Context) {
" MB",
)
LocalCache.notes
.countByGroup { _, it -> it.event?.kind() }
.forEach { Log.d("STATE DUMP", "Kind ${it.key}: \t${it.value} elements ") }
LocalCache.addressables
.countByGroup { _, it -> it.event?.kind() }
.forEach { Log.d("STATE DUMP", "Kind ${it.key}: \t${it.value} elements ") }
val qttNotes = LocalCache.notes.countByGroup { _, it -> it.event?.kind() }
val qttAddressables = LocalCache.addressables.countByGroup { _, it -> it.event?.kind() }
val bytesNotes =
LocalCache.notes
.sumByGroup(groupMap = { _, it -> it.event?.kind() }, sumOf = { _, it -> it.event?.countMemory() ?: 0L })
val bytesAddressables =
LocalCache.addressables
.sumByGroup(groupMap = { _, it -> it.event?.kind() }, sumOf = { _, it -> it.event?.countMemory() ?: 0L })
qttNotes.forEach { kind, qtt ->
Log.d("STATE DUMP", "Kind $kind:\t$qtt elements\t${bytesNotes.get(kind)?.div((1024 * 1024))}MB ")
}
qttAddressables.forEach { kind, qtt ->
Log.d("STATE DUMP", "Kind $kind:\t$qtt elements\t${bytesAddressables.get(kind)?.div((1024 * 1024))}MB ")
}
}
@OptIn(ExperimentalMaterial3Api::class)

View File

@ -136,6 +136,15 @@ class LargeCache<K, V> {
return runner.results
}
fun <R> sumByGroup(
groupMap: BiNotNullMapper<K, V, R>,
sumOf: BiNotNullMapper<K, V, Long>,
): Map<R, Long> {
val runner = BiSumByGroupCollector(groupMap, sumOf)
innerForEach(runner)
return runner.results
}
fun count(consumer: BiFilter<K, V>): Int {
val runner = BiCountIfCollector(consumer)
innerForEach(runner)
@ -356,6 +365,24 @@ class BiCountByGroupCollector<K, V, R>(val mapper: BiNotNullMapper<K, V, R>) : B
}
}
class BiSumByGroupCollector<K, V, R>(val mapper: BiNotNullMapper<K, V, R>, val sumOf: BiNotNullMapper<K, V, Long>) : BiConsumer<K, V> {
var results = HashMap<R, Long>()
override fun accept(
k: K,
v: V,
) {
val group = mapper.map(k, v)
val sum = results[group]
if (sum == null) {
results[group] = sumOf.map(k, v)
} else {
results[group] = sum + sumOf.map(k, v)
}
}
}
class BiCountIfCollector<K, V>(val filter: BiFilter<K, V>) : BiConsumer<K, V> {
var count = 0