mirror of
https://github.com/ReVanced/revanced-patcher.git
synced 2024-11-10 01:02:22 +01:00
perf: optimize indexOf call away
This commit is contained in:
parent
86cb053566
commit
f8e978af88
1 changed files with 22 additions and 5 deletions
|
@ -19,14 +19,13 @@ class Cache(
|
|||
*/
|
||||
fun findClass(predicate: (ClassDef) -> Boolean): ClassProxy? {
|
||||
// if we already proxied the class matching the predicate,
|
||||
val proxiedClass = classProxy.singleOrNull { classProxy -> predicate(classProxy.immutableClass) }
|
||||
val proxiedClass = classProxy.find { predicate(it.immutableClass) }
|
||||
// return that proxy
|
||||
if (proxiedClass != null) return proxiedClass
|
||||
// else search the original class list
|
||||
val foundClass = classes.singleOrNull(predicate) ?: return null
|
||||
val (foundClass, index) = classes.findIndexed(predicate) ?: return null
|
||||
// create a class proxy with the index of the class in the classes list
|
||||
// TODO: There might be a more elegant way to the comment above
|
||||
val classProxy = ClassProxy(foundClass, classes.indexOf(foundClass))
|
||||
val classProxy = ClassProxy(foundClass, index)
|
||||
// add it to the cache and
|
||||
this.classProxy.add(classProxy)
|
||||
// return the proxy class
|
||||
|
@ -40,4 +39,22 @@ class MethodMap : LinkedHashMap<String, SignatureResolverResult>() {
|
|||
}
|
||||
}
|
||||
|
||||
class MethodNotFoundException(s: String) : Exception(s)
|
||||
internal class MethodNotFoundException(s: String) : Exception(s)
|
||||
|
||||
internal inline fun <T> Iterable<T>.find(predicate: (T) -> Boolean): T? {
|
||||
for (element in this) {
|
||||
if (predicate(element)) {
|
||||
return element
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
internal inline fun <T> Iterable<T>.findIndexed(predicate: (T) -> Boolean): Pair<T, Int>? {
|
||||
for ((index, element) in this.withIndex()) {
|
||||
if (predicate(element)) {
|
||||
return element to index
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue