Add UnusedMethodTransformer #83

Manually merged
major merged 1 commits from :unused_methods into master 4 years ago
major commented 4 years ago

The transformer does not check for use via reflection. The only cases
in the 550 and OSRS clients where methods are accessed via reflection
are either 1) JRE classes, 2) when the method name is sent from the
server.

PSVM and methods declared in TypedRemapper.EXCLUDED_METHODS are never
removed.

Signed-off-by: Major major@emulate.rs

The transformer does _not_ check for use via reflection. The only cases in the 550 and OSRS clients where methods are accessed via reflection are either 1) JRE classes, 2) when the method name is sent from the server. PSVM and methods declared in TypedRemapper.EXCLUDED_METHODS are never removed. Signed-off-by: Major <major@emulate.rs>
gpe requested changes 4 years ago
gpe left a comment
Owner

A few comments, mostly nit-picking

A few comments, mostly nit-picking
import org.objectweb.asm.tree.MethodNode
class UnusedMethodTransformer : Transformer() {
Owner

I'd remove this blank line for consistency with existing classes, where there is no gap

I'd remove this blank line for consistency with existing classes, where there is no gap
}
val references = methodReferences[partition]
if (references.isNotEmpty() && (references.size != 1 || references.first() != member)) {
Owner

Finding this logic a little bit hard to follow. Is it to ensure methods which only reference themselves are deleted?

I wonder if val references = methodReferences[partition].minus(member) followed by if (references.isNotEmpty()) { would be clearer? (albeit slightly slower, I expect)

Finding this logic a little bit hard to follow. Is it to ensure methods which only reference themselves are deleted? I wonder if `val references = methodReferences[partition].minus(member)` followed by `if (references.isNotEmpty()) {` would be clearer? (albeit slightly slower, I expect)
continue
}
val partitionOwners = partition.mapTo(mutableSetOf(), MemberRef::owner)
Owner

This variable seems to be unused?

This variable seems to be unused?
val partitionOwners = partition.mapTo(mutableSetOf(), MemberRef::owner)
partitionOwners -= clazz.name
Owner

Remove one of these blank lines (I think IDEA's formatter should be configured to only allow a max of one blank line inside method code? I'm not sure if you ran it or not.)

Remove one of these blank lines (I think IDEA's formatter should be configured to only allow a max of one blank line inside method code? I'm not sure if you ran it or not.)
val owner = library[ref.owner]!!
val superMethods = owner.methods.iterator()
for (superMethod in superMethods) {
Owner

Maybe replace with owner.methods.removeIf { it.name == ref.name && it.desc == ref.desc }?

More concise, and we use removeIf similarly elsewhere.

Maybe replace with `owner.methods.removeIf { it.name == ref.name && it.desc == ref.desc }`? More concise, and we use `removeIf` similarly elsewhere.
}
}
logger.info { "Removed $methodsRemoved unused methods." }
Owner

Remove trailing period for consistency with other log messages in the codebase

Remove trailing period for consistency with other log messages in the codebase
private fun retain(method: MethodNode): Boolean {
val name = method.name
if (method.access and Opcodes.ACC_STATIC != 0 && name == "main" && method.desc == PSVM_DESCRIPTOR) {
Owner

I don't think we need this check, because it will already be covered by "main" being in TypedRemapper.EXCLUDED_METHODS.

(It does mean we remove it anyway regardless of access flags/descriptor, but most of the other code using EXCLUDED_* is like that anyway. And dropping the first condition doesn't change any of that, because it always falls back to the second one.)

I don't think we need this check, because it will already be covered by `"main"` being in `TypedRemapper.EXCLUDED_METHODS`. (It does mean we remove it anyway regardless of access flags/descriptor, but most of the other code using `EXCLUDED_*` is like that anyway. And dropping the first condition doesn't change any of that, because it always falls back to the second one.)
gpe approved these changes 4 years ago
major closed this pull request 4 years ago
The pull request has been manually merged as 488e8ef8c3.
Sign in to join this conversation.
Loading…
There is no content yet.