Skip to content

Commit

Permalink
perf: cache, lazy invoke
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Jul 3, 2024
1 parent c78d7c0 commit 0c1cc78
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 20 deletions.
15 changes: 8 additions & 7 deletions app/src/main/kotlin/li/songe/gkd/data/ResolvedRule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.accessibilityservice.AccessibilityService
import android.view.accessibility.AccessibilityNodeInfo
import kotlinx.coroutines.Job
import li.songe.gkd.service.createCacheTransform
import li.songe.gkd.service.createTransform
import li.songe.gkd.service.createNoCacheTransform
import li.songe.gkd.service.lastTriggerRule
import li.songe.gkd.service.lastTriggerTime
import li.songe.gkd.service.querySelector
Expand Down Expand Up @@ -130,8 +130,8 @@ sealed class ResolvedRule(
else -> true
}

private val canCacheIndex = (matches + excludeMatches).any { s -> s.useCache }
private val transform = if (canCacheIndex) defaultCacheTransform.transform else defaultTransform
private val useCache = (matches + excludeMatches).any { s -> s.useCache }
private val transform = if (useCache) defaultCacheTransform else defaultTransform

fun query(
nodeInfo: AccessibilityNodeInfo?,
Expand All @@ -141,24 +141,25 @@ sealed class ResolvedRule(
var target: AccessibilityNodeInfo? = null
if (anyMatches.isNotEmpty()) {
for (selector in anyMatches) {
target = nodeInfo.querySelector(selector, quickFind, transform)
target = nodeInfo.querySelector(selector, quickFind, transform.transform)
?: break
}
if (target == null) return null
}
for (selector in matches) {
target = nodeInfo.querySelector(selector, quickFind, transform)
target = nodeInfo.querySelector(selector, quickFind, transform.transform)
?: return null
}
for (selector in excludeMatches) {
nodeInfo.querySelector(
selector,
quickFind,
transform
transform.transform
)?.let { return null }
}
return target
} finally {
defaultTransform.cache.clear()
defaultCacheTransform.cache.clear()
}
}
Expand Down Expand Up @@ -247,7 +248,7 @@ fun getFixActivityIds(
}
}

private val defaultTransform = createTransform()
private val defaultTransform = createNoCacheTransform()
private val defaultCacheTransform = createCacheTransform()


7 changes: 4 additions & 3 deletions app/src/main/kotlin/li/songe/gkd/service/AbExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -493,14 +493,14 @@ fun createCacheTransform(): CacheTransform {
return CacheTransform(transform, cache)
}

private fun List<Any?>.getIntOrNull(i: Int = 0): Int? {
private fun List<Any>.getIntOrNull(i: Int = 0): Int? {
return getOrNull(i) as? Int ?: return null
}

fun createTransform(): Transform<AccessibilityNodeInfo> {
fun createNoCacheTransform(): CacheTransform {
val cache = NodeCache()
val getNodeAttr = createGetNodeAttr(cache)
return Transform(
val transform = Transform(
getAttr = { target, name ->
when (target) {
is AccessibilityNodeInfo -> getNodeAttr(target, name)
Expand Down Expand Up @@ -569,4 +569,5 @@ fun createTransform(): Transform<AccessibilityNodeInfo> {
}
},
)
return CacheTransform(transform, cache)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import kotlin.js.JsExport
@Suppress("UNCHECKED_CAST", "UNUSED")
class MultiplatformTransform<T : Any>(
getAttr: (Any?, String) -> Any?,
getInvoke: (Any?, String, List<Any?>) -> Any?,
getInvoke: (Any?, String, List<Any>) -> Any?,
getName: (T) -> String?,
getChildren: (T) -> Array<T>,
getParent: (T) -> T?,
Expand Down
7 changes: 2 additions & 5 deletions selector/src/commonMain/kotlin/li/songe/selector/Selector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,7 @@ class Selector internal constructor(
}

val useCache = run {
if (connectKeys.contains(ConnectOperator.BeforeBrother.key)) {
return@run true
}
if (connectKeys.contains(ConnectOperator.AfterBrother.key)) {
if (connectKeys.isNotEmpty()) {
return@run true
}
binaryExpressions.forEach { b ->
Expand Down Expand Up @@ -139,7 +136,7 @@ class Selector internal constructor(
}

private val useCacheProperties by lazy {
arrayOf("index", "parent")
arrayOf("index", "parent", "depth")
}
private val useCacheMethods by lazy {
arrayOf("getChild")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package li.songe.selector
@Suppress("UNUSED")
class Transform<T>(
val getAttr: (Any?, String) -> Any?,
val getInvoke: (Any?, String, List<Any?>) -> Any? = { _, _, _ -> null },
val getInvoke: (Any?, String, List<Any>) -> Any? = { _, _, _ -> null },
val getName: (T) -> CharSequence?,
val getChildren: (T) -> Sequence<T>,
val getParent: (T) -> T?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,23 @@ sealed class ValueExpression(open val value: Any?, open val type: String) : Posi
override fun <T> getAttr(node: T, transform: Transform<T>): Any? {
return when (callee) {
is CallExpression -> {
// not support
null
}

is Identifier -> {
transform.getInvoke(
node,
callee.value,
arguments.map { it.getAttr(node, transform) }
arguments.map { it.getAttr(node, transform).whenNull { return null } }
)
}

is MemberExpression -> {
transform.getInvoke(
callee.object0.getAttr(node, transform),
callee.object0.getAttr(node, transform).whenNull { return null },
callee.property,
arguments.map { it.getAttr(node, transform) }
arguments.map { it.getAttr(node, transform).whenNull { return null } }
)
}
}
Expand Down
7 changes: 7 additions & 0 deletions selector/src/commonMain/kotlin/li/songe/selector/util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ internal fun optimizeMatchString(value: String): ((CharSequence) -> Boolean)? {
return null
}

internal inline fun <T> T?.whenNull(block: () -> Nothing): T {
if (this == null) {
block()
}
return this
}

@JsExport
class DefaultTypeInfo(
val booleanType: TypeInfo,
Expand Down

0 comments on commit 0c1cc78

Please sign in to comment.