perf: binaryExpressions

This commit is contained in:
lisonge 2024-03-26 21:09:29 +08:00
parent d9c56775e3
commit 0ac0b69a15
3 changed files with 19 additions and 16 deletions

View File

@ -168,12 +168,12 @@ val allowPropertyNames by lazy {
}
fun Selector.checkSelector(): String? {
nameToTypeList.forEach { (name, type) ->
if (!allowPropertyNames.contains(name)) {
return "未知属性:${name}"
binaryExpressions.forEach { e ->
if (!allowPropertyNames.contains(e.name)) {
return "未知属性:${e.name}"
}
if (type != "null" && allowPropertyNames[name] != type) {
return "非法类型:${name}=$type"
if (e.value.type != "null" && allowPropertyNames[e.name] != e.value.type) {
return "非法类型:${e.name}=${e.value.type}"
}
}
return null

View File

@ -17,7 +17,16 @@ class MultiplatformSelector private constructor(
val qfTextValue = selector.qfTextValue
val canQf = selector.canQf
val isMatchRoot = selector.isMatchRoot
val nameToTypeList = selector.nameToTypeList
// [name,operator,value][]
val binaryExpressions = selector.binaryExpressions.map { e ->
arrayOf(
e.name,
e.operator.key,
e.value.type,
e.value.toString()
)
}.toTypedArray()
fun <T : Any> match(node: T, transform: MultiplatformTransform<T>): T? {
return selector.match(node, transform.transform)

View File

@ -84,27 +84,21 @@ class Selector internal constructor(private val propertyWrapper: PropertyWrapper
keys.toTypedArray()
}
private val binaryExpressions = run {
val binaryExpressions = run {
var p: PropertyWrapper? = propertyWrapper
val names = mutableListOf<BinaryExpression>()
val expressions = mutableListOf<BinaryExpression>()
while (p != null) {
val s = p.propertySegment
names.addAll(s.binaryExpressions)
expressions.addAll(s.binaryExpressions)
p = p.to?.to
}
names.distinct().toTypedArray()
expressions.distinct().toTypedArray()
}
val propertyNames = run {
binaryExpressions.map { e -> e.name }.distinct().toTypedArray()
}
// [name,type][]
val nameToTypeList = run {
binaryExpressions.map { e -> arrayOf(e.name, e.value.type) }.distinct().toTypedArray()
}
val canCacheIndex =
connectKeys.contains(ConnectOperator.BeforeBrother.key) || connectKeys.contains(
ConnectOperator.AfterBrother.key