perf: clear node child cache

This commit is contained in:
二刺螈 2024-10-28 20:29:59 +08:00
parent ac51aae4ab
commit 44b0fd0066
2 changed files with 37 additions and 9 deletions

View File

@ -46,18 +46,40 @@ class A11yContext(
private var parentCache = LruCache<AccessibilityNodeInfo, AccessibilityNodeInfo>(MAX_CACHE_SIZE) private var parentCache = LruCache<AccessibilityNodeInfo, AccessibilityNodeInfo>(MAX_CACHE_SIZE)
var rootCache: AccessibilityNodeInfo? = null var rootCache: AccessibilityNodeInfo? = null
private var lastClearTime = 0L private fun clearChildCache(node: AccessibilityNodeInfo) {
private fun clearNodeCache(t: Long = System.currentTimeMillis()) { repeat(node.childCount.coerceAtMost(MAX_CHILD_SIZE)) { i ->
childCache.remove(node to i)?.let {
clearChildCache(it)
}
}
}
fun clearNodeCache(eventNode: AccessibilityNodeInfo? = null) {
if (rootCache?.packageName != topActivityFlow.value.appId) {
rootCache = null
}
if (eventNode != null) {
clearChildCache(eventNode)
parentCache[eventNode]?.let { p ->
getPureIndex(eventNode)?.let { i ->
childCache[p to i] = eventNode
}
}
if (rootCache == eventNode) {
rootCache = eventNode
} else {
if (META.debuggable) {
Log.d("cache", "clear node cache ${eventNode.packageName}/${eventNode.className}")
}
return
}
}
if (META.debuggable) { if (META.debuggable) {
val sizeList = listOf(childCache.size(), parentCache.size(), indexCache.size()) val sizeList = listOf(childCache.size(), parentCache.size(), indexCache.size())
if (sizeList.any { it > 0 }) { if (sizeList.any { it > 0 }) {
Log.d("cache", "clear cache -> $sizeList") Log.d("cache", "clear cache -> $sizeList")
} }
} }
lastClearTime = t
if (rootCache?.packageName != topActivityFlow.value.appId) {
rootCache = null
}
try { try {
childCache.evictAll() childCache.evictAll()
parentCache.evictAll() parentCache.evictAll()
@ -72,12 +94,13 @@ class A11yContext(
} }
private var lastAppChangeTime = appChangeTime private var lastAppChangeTime = appChangeTime
fun clearOldAppNodeCache() { fun clearOldAppNodeCache(): Boolean {
if (appChangeTime != lastAppChangeTime) { if (appChangeTime != lastAppChangeTime) {
lastAppChangeTime = appChangeTime lastAppChangeTime = appChangeTime
clearNodeCache() clearNodeCache()
return return true
} }
return false
} }
var currentRule: ResolvedRule? = null var currentRule: ResolvedRule? = null

View File

@ -255,7 +255,12 @@ private fun A11yService.useMatchRule() {
} }
} }
var lastNodeUsed = false var lastNodeUsed = false
a11yContext.clearOldAppNodeCache() if (!a11yContext.clearOldAppNodeCache()) {
if (byEvent != null) { // 此为多数情况
// 新事件到来时, 若缓存清理不及时会导致无法查询到节点
a11yContext.clearNodeCache(lastNode)
}
}
for (rule in activityRule.priorityRules) { // 规则数量有可能过多导致耗时过长 for (rule in activityRule.priorityRules) { // 规则数量有可能过多导致耗时过长
if (delayRule != null && delayRule !== rule) continue if (delayRule != null && delayRule !== rule) continue
val statusCode = rule.status val statusCode = rule.status