Skip to content

Commit

Permalink
Avoid unnecessary linked list traversals in MethodLinker.
Browse files Browse the repository at this point in the history
The chains themselves are assumed to be an implementation detail of this class, in which case it's fine to flatten them at any time.
  • Loading branch information
LlamaLad7 committed Jan 3, 2025
1 parent 933e69e commit 2c1f04d
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions base/src/main/java/proguard/classfile/util/MethodLinker.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@

import static proguard.classfile.instruction.Instruction.OP_INVOKESTATIC;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import proguard.classfile.AccessConstants;
import proguard.classfile.Clazz;
Expand Down Expand Up @@ -132,12 +134,19 @@ private static void link(Member member1, Member member2) {
* @return The last method in the linked list.
*/
public static Member lastMember(Member member) {
List<Member> chain = new ArrayList<>();
Member lastMember = member;
while (lastMember.getProcessingInfo() != null
&& lastMember.getProcessingInfo() instanceof Member) {
chain.add(lastMember);
lastMember = (Member) lastMember.getProcessingInfo();
}

// Point every member in the chain directly to the last element to save time in the future
for (Member relatedMember : chain) {
relatedMember.setProcessingInfo(lastMember);
}

return lastMember;
}

Expand All @@ -149,6 +158,9 @@ public static Member lastMember(Member member) {
*/
public static Processable lastProcessable(Processable processable) {
Processable lastProcessable = processable;
if (lastProcessable instanceof Member) {
lastProcessable = lastMember((Member) lastProcessable);
}
while (lastProcessable.getProcessingInfo() != null
&& lastProcessable.getProcessingInfo() instanceof Processable) {
lastProcessable = (Processable) lastProcessable.getProcessingInfo();
Expand Down

0 comments on commit 2c1f04d

Please sign in to comment.