Skip to content

Commit

Permalink
perf: lazy-ify all mutable clones
Browse files Browse the repository at this point in the history
  • Loading branch information
Sculas committed Apr 3, 2022
1 parent dbafe2a commit 05e4400
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/app/revanced/patcher/proxy/ClassProxy.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ClassProxy(

/**
* Creates and returns a mutable clone of the original class
* A patch should always use the original immutable class reference to avoid unnucessary allocations for the mutable class
* A patch should always use the original immutable class reference to avoid unnecessary allocations for the mutable class
*/
fun resolve(): MutableClass {
if (!proxyUsed) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import org.jf.dexlib2.iface.Annotation
class MutableAnnotation(annotation: Annotation) : BaseAnnotation() {
private val visibility = annotation.visibility
private val type = annotation.type
private val elements = annotation.elements.map { element -> element.toMutable() }.toMutableSet()
private val _elements by lazy { annotation.elements.map { element -> element.toMutable() }.toMutableSet() }

override fun getType(): String {
return type
}

override fun getElements(): MutableSet<MutableAnnotationElement> {
return elements
return _elements
}

override fun getVisibility(): Int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,18 @@ class MutableClass(classDef: ClassDef) : ClassDef, BaseTypeReference() {
private var accessFlags = classDef.accessFlags
private var superclass = classDef.superclass

private val interfaces = classDef.interfaces.toMutableList()
private val annotations = classDef.annotations.map { annotation -> annotation.toMutable() }.toMutableSet()
private val _interfaces by lazy { classDef.interfaces.toMutableList() }
private val _annotations by lazy { classDef.annotations.map { annotation -> annotation.toMutable() }.toMutableSet() }

// Methods
private val methods = classDef.methods.map { method -> method.toMutable() }.toMutableSet()
private val directMethods = classDef.directMethods.map { directMethod -> directMethod.toMutable() }.toMutableSet()
private val virtualMethods =
classDef.virtualMethods.map { virtualMethod -> virtualMethod.toMutable() }.toMutableSet()
private val _methods by lazy { classDef.methods.map { method -> method.toMutable() }.toMutableSet() }
private val _directMethods by lazy { classDef.directMethods.map { directMethod -> directMethod.toMutable() }.toMutableSet() }
private val _virtualMethods by lazy { classDef.virtualMethods.map { virtualMethod -> virtualMethod.toMutable() }.toMutableSet() }

// Fields
private val fields = classDef.fields.map { field -> field.toMutable() }.toMutableSet()
private val staticFields = classDef.staticFields.map { staticField -> staticField.toMutable() }.toMutableSet()
private val instanceFields =
classDef.instanceFields.map { instanceFields -> instanceFields.toMutable() }.toMutableSet()
private val _fields by lazy { classDef.fields.map { field -> field.toMutable() }.toMutableSet() }
private val _staticFields by lazy { classDef.staticFields.map { staticField -> staticField.toMutable() }.toMutableSet() }
private val _instanceFields by lazy { classDef.instanceFields.map { instanceFields -> instanceFields.toMutable() }.toMutableSet() }

fun setType(type: String) {
this.type = type
Expand Down Expand Up @@ -61,34 +59,34 @@ class MutableClass(classDef: ClassDef) : ClassDef, BaseTypeReference() {
}

override fun getInterfaces(): MutableList<String> {
return interfaces
return _interfaces
}

override fun getAnnotations(): MutableSet<MutableAnnotation> {
return annotations
return _annotations
}

override fun getStaticFields(): MutableSet<MutableField> {
return staticFields
return _staticFields
}

override fun getInstanceFields(): MutableSet<MutableField> {
return instanceFields
return _instanceFields
}

override fun getFields(): MutableSet<MutableField> {
return fields
return _fields
}

override fun getDirectMethods(): MutableSet<MutableMethod> {
return directMethods
return _directMethods
}

override fun getVirtualMethods(): MutableSet<MutableMethod> {
return virtualMethods
return _virtualMethods
}

override fun getMethods(): MutableSet<MutableMethod> {
return methods
return _methods
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class MutableEncodedValue(encodedValue: EncodedValue) : EncodedValue {

override fun compareTo(other: EncodedValue): Int {
return valueType - other.valueType

}

override fun getValueType(): Int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ class MutableField(field: Field) : Field, BaseFieldReference() {
private var type = field.type
private var accessFlags = field.accessFlags
private var initialValue = field.initialValue?.toMutable()
private val annotations = field.annotations.map { annotation -> annotation.toMutable() }.toMutableSet()
private val _annotations by lazy { field.annotations.map { annotation -> annotation.toMutable() }.toMutableSet() }

fun setDefiningClass(definingClass: String) {
this.definingClass
this.definingClass = definingClass
}

fun setName(name: String) {
Expand Down Expand Up @@ -46,7 +46,7 @@ class MutableField(field: Field) : Field, BaseFieldReference() {
}

override fun getAnnotations(): MutableSet<MutableAnnotation> {
return this.annotations
return this._annotations
}

override fun getAccessFlags(): Int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ class MutableMethod(method: Method) : Method, BaseMethodReference() {

// Create own mutable MethodImplementation (due to not being able to change members like register count)
private var implementation = method.implementation?.let { MutableMethodImplementation(it) }
private val annotations = method.annotations.map { annotation -> annotation.toMutable() }.toMutableSet()
private val parameters = method.parameters.map { parameter -> parameter.toMutable() }.toMutableList()
private val parameterTypes = method.parameterTypes.toMutableList()
private val _annotations by lazy { method.annotations.map { annotation -> annotation.toMutable() }.toMutableSet() }
private val _parameters by lazy { method.parameters.map { parameter -> parameter.toMutable() }.toMutableList() }
private val _parameterTypes by lazy { method.parameterTypes.toMutableList() }

override fun getDefiningClass(): String {
return this.definingClass
Expand All @@ -27,23 +27,23 @@ class MutableMethod(method: Method) : Method, BaseMethodReference() {
}

override fun getParameterTypes(): MutableList<CharSequence> {
return parameterTypes
return _parameterTypes
}

override fun getReturnType(): String {
return returnType
}

override fun getAnnotations(): MutableSet<MutableAnnotation> {
return annotations
return _annotations
}

override fun getAccessFlags(): Int {
return accessFlags
}

override fun getParameters(): MutableList<MutableMethodParameter> {
return parameters
return _parameters
}

override fun getImplementation(): MutableMethodImplementation? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class MutableMethodParameter(parameter: MethodParameter) : MethodParameter, Base
private var type = parameter.type
private var name = parameter.name
private var signature = parameter.signature
private val annotations = parameter.annotations.map { annotation -> annotation.toMutable() }.toMutableSet()
private val _annotations by lazy { parameter.annotations.map { annotation -> annotation.toMutable() }.toMutableSet() }

override fun getType(): String {
return type
Expand All @@ -24,7 +24,7 @@ class MutableMethodParameter(parameter: MethodParameter) : MethodParameter, Base
}

override fun getAnnotations(): MutableSet<MutableAnnotation> {
return annotations
return _annotations
}

companion object {
Expand Down

0 comments on commit 05e4400

Please sign in to comment.