You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2022/04/05 19:47:47 UTC
[groovy] 01/01: Tweak bytecode: eliminate duplicated `NOP`
This is an automated email from the ASF dual-hosted git repository.
sunlan pushed a commit to branch danielsun/tweak-bytecode
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit f67a31a9a8450fd943e7206f47b7645f51991769
Author: Daniel Sun <su...@apache.org>
AuthorDate: Wed Apr 6 03:47:25 2022 +0800
Tweak bytecode: eliminate duplicated `NOP`
---
.../groovy/classgen/asm/SmartMethodVisitor.java | 51 ++++++++++++++++++++++
.../groovy/classgen/asm/WriterController.java | 2 +-
2 files changed, 52 insertions(+), 1 deletion(-)
diff --git a/src/main/java/org/codehaus/groovy/classgen/asm/SmartMethodVisitor.java b/src/main/java/org/codehaus/groovy/classgen/asm/SmartMethodVisitor.java
new file mode 100644
index 0000000000..b5cd88117d
--- /dev/null
+++ b/src/main/java/org/codehaus/groovy/classgen/asm/SmartMethodVisitor.java
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.codehaus.groovy.classgen.asm;
+
+import org.codehaus.groovy.control.CompilerConfiguration;
+import org.objectweb.asm.MethodVisitor;
+
+import java.util.ArrayDeque;
+import java.util.Deque;
+import java.util.Objects;
+
+import static org.objectweb.asm.Opcodes.NOP;
+
+/**
+ * Represents a method visitor for tweaking bytecode
+ *
+ * @since 4.0.2
+ */
+public class SmartMethodVisitor extends MethodVisitor {
+ private final Deque<Integer> opcodeDeque = new ArrayDeque<>();
+
+ protected SmartMethodVisitor(final MethodVisitor methodVisitor) {
+ super(CompilerConfiguration.ASM_API_VERSION, methodVisitor);
+ }
+
+ public void visitInsn(final int opcode) {
+ // avoid duplicated `NOP`
+ if (opcode == NOP && Objects.equals(opcodeDeque.peekLast(), NOP)) {
+ return;
+ }
+
+ super.visitInsn(opcode);
+ opcodeDeque.addLast(opcode);
+ }
+}
diff --git a/src/main/java/org/codehaus/groovy/classgen/asm/WriterController.java b/src/main/java/org/codehaus/groovy/classgen/asm/WriterController.java
index 39f05cb26e..fd13743e6c 100644
--- a/src/main/java/org/codehaus/groovy/classgen/asm/WriterController.java
+++ b/src/main/java/org/codehaus/groovy/classgen/asm/WriterController.java
@@ -160,7 +160,7 @@ public class WriterController {
}
public void setMethodVisitor(final MethodVisitor methodVisitor) {
- this.methodVisitor = methodVisitor;
+ this.methodVisitor = new SmartMethodVisitor(methodVisitor);
}
public GeneratorContext getContext() {