Skip to content

Commit

Permalink
Use a SoftPool of ElementIterators vs a single ThreadLocal
Browse files Browse the repository at this point in the history
  • Loading branch information
itboy87 committed Sep 11, 2024
1 parent 77c05be commit 1af5450
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions ksoup/src/com/fleeksoft/ksoup/select/StructuralEvaluator.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fleeksoft.ksoup.select

import com.fleeksoft.ksoup.internal.SoftPool
import com.fleeksoft.ksoup.internal.StringUtil
import com.fleeksoft.ksoup.nodes.Element
import com.fleeksoft.ksoup.nodes.NodeIterator
Expand Down Expand Up @@ -48,8 +49,7 @@ public abstract class StructuralEvaluator(public val evaluator: Evaluator) : Eva

internal class Has(evaluator: Evaluator) : StructuralEvaluator(evaluator) {
companion object {
private val nodeIterator: ThreadLocal<NodeIterator<Element>> =
ThreadLocal { NodeIterator(Element("html"), Element::class) }
private val ElementIterPool: SoftPool<NodeIterator<Element>> = SoftPool { NodeIterator(Element("html"), Element::class) }
}

private val checkSiblings = evalWantsSiblings(evaluator) // evaluating against siblings (or children)
Expand All @@ -63,16 +63,18 @@ public abstract class StructuralEvaluator(public val evaluator: Evaluator) : Eva
}
sib = sib.nextElementSibling()
}
} else {
// otherwise we only want to match children (or below), and not the input element. And we want to minimize GCs so reusing the Iterator obj
val it = nodeIterator.get()
it.restart(element)
}
// otherwise we only want to match children (or below), and not the input element. And we want to minimize GCs so reusing the Iterator obj
val it = ElementIterPool.borrow()
it.restart(element)
try {
while (it.hasNext()) {
val el = it.next()
if (el === element) continue // don't match self, only descendants

if (evaluator.matches(element, el)) return true
}
} finally {
ElementIterPool.release(it)
}
return false
}
Expand Down

0 comments on commit 1af5450

Please sign in to comment.