From 3e96a54c4fde2c67703371d867eb0e9bf90f0c09 Mon Sep 17 00:00:00 2001
From: dgelessus <dgelessus@users.noreply.github.com>
Date: Fri, 21 Jul 2023 14:55:33 +0200
Subject: [PATCH] Optimize away list empty check in Parser new methods where
 possible

---
 .../GenerateAlternativeCodeForParser.java     |  17 +-
 .../org/sablecc/sablecc/parser/Parser.java    | 210 ++++++++----------
 .../resources/org/sablecc/sablecc/parser.txt  |  11 +
 3 files changed, 115 insertions(+), 123 deletions(-)

diff --git a/src/main/java/org/sablecc/sablecc/GenerateAlternativeCodeForParser.java b/src/main/java/org/sablecc/sablecc/GenerateAlternativeCodeForParser.java
index bef0247..15d28ff 100644
--- a/src/main/java/org/sablecc/sablecc/GenerateAlternativeCodeForParser.java
+++ b/src/main/java/org/sablecc/sablecc/GenerateAlternativeCodeForParser.java
@@ -311,8 +311,7 @@ public class GenerateAlternativeCodeForParser extends DepthFirstAdapter
       }
       else
       {
-        macros.apply(file, "ParserTypedLinkedListInit", new String[] {getVariableName(node)});
-
+        boolean first = true;
         for(PListTerm listTerm : node.getListTerms())
         {
           String typeName = lookupInternalTypeName(listTerm);
@@ -324,12 +323,24 @@ public class GenerateAlternativeCodeForParser extends DepthFirstAdapter
 
           if(typeName.startsWith("L"))
           {
-            macros.apply(file, "ParserTypedLinkedListAddAll", new String[] {getVariableName(node), getVariableName(listTerm)});
+            if(first)
+            {
+              macros.apply(file, "ParserTypedLinkedListInitAddAll", new String[] {getVariableName(node), getVariableName(listTerm)});
+            }
+            else
+            {
+              macros.apply(file, "ParserTypedLinkedListAddAll", new String[] {getVariableName(node), getVariableName(listTerm)});
+            }
           }
           else
           {
+            if(first)
+            {
+              macros.apply(file, "ParserTypedLinkedListInit", new String[] {getVariableName(node)});
+            }
             macros.apply(file, "ParserTypedLinkedListAdd", new String[] {getVariableName(node), getVariableName(listTerm)});
           }
+          first = false;
         }
       }
 
diff --git a/src/main/java/org/sablecc/sablecc/parser/Parser.java b/src/main/java/org/sablecc/sablecc/parser/Parser.java
index 5e7c148..b44a61b 100644
--- a/src/main/java/org/sablecc/sablecc/parser/Parser.java
+++ b/src/main/java/org/sablecc/sablecc/parser/Parser.java
@@ -8031,14 +8031,12 @@ public class Parser implements IParser
         TPkgId tpkgidNode2;
         listNode1 = (List<TPkgId>)nodeArrayList1.get(0);
         tpkgidNode2 = (TPkgId)nodeArrayList2.get(0);
-        listNode3 = new LinkedList<>();
-        if(listNode1 != null)
-        {
-            if (listNode3.isEmpty() && listNode1 instanceof LinkedList<?>) {
-                listNode3 = listNode1;
-            } else {
-                listNode3.addAll(listNode1);
-            }
+        if (listNode1 instanceof LinkedList<?>) {
+            listNode3 = listNode1;
+        } else if (listNode1 == null) {
+            listNode3 = new LinkedList<>();
+        } else {
+            listNode3 = new LinkedList<>(listNode1);
         }
         if(tpkgidNode2 != null)
         {
@@ -8086,14 +8084,12 @@ public class Parser implements IParser
         PHelperDef phelperdefNode2;
         listNode1 = (List<PHelperDef>)nodeArrayList1.get(0);
         phelperdefNode2 = (PHelperDef)nodeArrayList2.get(0);
-        listNode3 = new LinkedList<>();
-        if(listNode1 != null)
-        {
-            if (listNode3.isEmpty() && listNode1 instanceof LinkedList<?>) {
-                listNode3 = listNode1;
-            } else {
-                listNode3.addAll(listNode1);
-            }
+        if (listNode1 instanceof LinkedList<?>) {
+            listNode3 = listNode1;
+        } else if (listNode1 == null) {
+            listNode3 = new LinkedList<>();
+        } else {
+            listNode3 = new LinkedList<>(listNode1);
         }
         if(phelperdefNode2 != null)
         {
@@ -8141,14 +8137,12 @@ public class Parser implements IParser
         TId tidNode2;
         listNode1 = (List<TId>)nodeArrayList1.get(0);
         tidNode2 = (TId)nodeArrayList2.get(0);
-        listNode3 = new LinkedList<>();
-        if(listNode1 != null)
-        {
-            if (listNode3.isEmpty() && listNode1 instanceof LinkedList<?>) {
-                listNode3 = listNode1;
-            } else {
-                listNode3.addAll(listNode1);
-            }
+        if (listNode1 instanceof LinkedList<?>) {
+            listNode3 = listNode1;
+        } else if (listNode1 == null) {
+            listNode3 = new LinkedList<>();
+        } else {
+            listNode3 = new LinkedList<>(listNode1);
         }
         if(tidNode2 != null)
         {
@@ -8196,14 +8190,12 @@ public class Parser implements IParser
         PTokenDef ptokendefNode2;
         listNode1 = (List<PTokenDef>)nodeArrayList1.get(0);
         ptokendefNode2 = (PTokenDef)nodeArrayList2.get(0);
-        listNode3 = new LinkedList<>();
-        if(listNode1 != null)
-        {
-            if (listNode3.isEmpty() && listNode1 instanceof LinkedList<?>) {
-                listNode3 = listNode1;
-            } else {
-                listNode3.addAll(listNode1);
-            }
+        if (listNode1 instanceof LinkedList<?>) {
+            listNode3 = listNode1;
+        } else if (listNode1 == null) {
+            listNode3 = new LinkedList<>();
+        } else {
+            listNode3 = new LinkedList<>(listNode1);
         }
         if(ptokendefNode2 != null)
         {
@@ -8251,14 +8243,12 @@ public class Parser implements IParser
         PStateListTail pstatelisttailNode2;
         listNode1 = (List<PStateListTail>)nodeArrayList1.get(0);
         pstatelisttailNode2 = (PStateListTail)nodeArrayList2.get(0);
-        listNode3 = new LinkedList<>();
-        if(listNode1 != null)
-        {
-            if (listNode3.isEmpty() && listNode1 instanceof LinkedList<?>) {
-                listNode3 = listNode1;
-            } else {
-                listNode3.addAll(listNode1);
-            }
+        if (listNode1 instanceof LinkedList<?>) {
+            listNode3 = listNode1;
+        } else if (listNode1 == null) {
+            listNode3 = new LinkedList<>();
+        } else {
+            listNode3 = new LinkedList<>(listNode1);
         }
         if(pstatelisttailNode2 != null)
         {
@@ -8306,14 +8296,12 @@ public class Parser implements IParser
         PConcat pconcatNode2;
         listNode1 = (List<PConcat>)nodeArrayList1.get(0);
         pconcatNode2 = (PConcat)nodeArrayList2.get(0);
-        listNode3 = new LinkedList<>();
-        if(listNode1 != null)
-        {
-            if (listNode3.isEmpty() && listNode1 instanceof LinkedList<?>) {
-                listNode3 = listNode1;
-            } else {
-                listNode3.addAll(listNode1);
-            }
+        if (listNode1 instanceof LinkedList<?>) {
+            listNode3 = listNode1;
+        } else if (listNode1 == null) {
+            listNode3 = new LinkedList<>();
+        } else {
+            listNode3 = new LinkedList<>(listNode1);
         }
         if(pconcatNode2 != null)
         {
@@ -8361,14 +8349,12 @@ public class Parser implements IParser
         PUnExp punexpNode2;
         listNode1 = (List<PUnExp>)nodeArrayList1.get(0);
         punexpNode2 = (PUnExp)nodeArrayList2.get(0);
-        listNode3 = new LinkedList<>();
-        if(listNode1 != null)
-        {
-            if (listNode3.isEmpty() && listNode1 instanceof LinkedList<?>) {
-                listNode3 = listNode1;
-            } else {
-                listNode3.addAll(listNode1);
-            }
+        if (listNode1 instanceof LinkedList<?>) {
+            listNode3 = listNode1;
+        } else if (listNode1 == null) {
+            listNode3 = new LinkedList<>();
+        } else {
+            listNode3 = new LinkedList<>(listNode1);
         }
         if(punexpNode2 != null)
         {
@@ -8416,14 +8402,12 @@ public class Parser implements IParser
         PProd pprodNode2;
         listNode1 = (List<PProd>)nodeArrayList1.get(0);
         pprodNode2 = (PProd)nodeArrayList2.get(0);
-        listNode3 = new LinkedList<>();
-        if(listNode1 != null)
-        {
-            if (listNode3.isEmpty() && listNode1 instanceof LinkedList<?>) {
-                listNode3 = listNode1;
-            } else {
-                listNode3.addAll(listNode1);
-            }
+        if (listNode1 instanceof LinkedList<?>) {
+            listNode3 = listNode1;
+        } else if (listNode1 == null) {
+            listNode3 = new LinkedList<>();
+        } else {
+            listNode3 = new LinkedList<>(listNode1);
         }
         if(pprodNode2 != null)
         {
@@ -8471,14 +8455,12 @@ public class Parser implements IParser
         PElem pelemNode2;
         listNode1 = (List<PElem>)nodeArrayList1.get(0);
         pelemNode2 = (PElem)nodeArrayList2.get(0);
-        listNode3 = new LinkedList<>();
-        if(listNode1 != null)
-        {
-            if (listNode3.isEmpty() && listNode1 instanceof LinkedList<?>) {
-                listNode3 = listNode1;
-            } else {
-                listNode3.addAll(listNode1);
-            }
+        if (listNode1 instanceof LinkedList<?>) {
+            listNode3 = listNode1;
+        } else if (listNode1 == null) {
+            listNode3 = new LinkedList<>();
+        } else {
+            listNode3 = new LinkedList<>(listNode1);
         }
         if(pelemNode2 != null)
         {
@@ -8526,14 +8508,12 @@ public class Parser implements IParser
         PAlt paltNode2;
         listNode1 = (List<PAlt>)nodeArrayList1.get(0);
         paltNode2 = (PAlt)nodeArrayList2.get(0);
-        listNode3 = new LinkedList<>();
-        if(listNode1 != null)
-        {
-            if (listNode3.isEmpty() && listNode1 instanceof LinkedList<?>) {
-                listNode3 = listNode1;
-            } else {
-                listNode3.addAll(listNode1);
-            }
+        if (listNode1 instanceof LinkedList<?>) {
+            listNode3 = listNode1;
+        } else if (listNode1 == null) {
+            listNode3 = new LinkedList<>();
+        } else {
+            listNode3 = new LinkedList<>(listNode1);
         }
         if(paltNode2 != null)
         {
@@ -8581,14 +8561,12 @@ public class Parser implements IParser
         PTerm ptermNode2;
         listNode1 = (List<PTerm>)nodeArrayList1.get(0);
         ptermNode2 = (PTerm)nodeArrayList2.get(0);
-        listNode3 = new LinkedList<>();
-        if(listNode1 != null)
-        {
-            if (listNode3.isEmpty() && listNode1 instanceof LinkedList<?>) {
-                listNode3 = listNode1;
-            } else {
-                listNode3.addAll(listNode1);
-            }
+        if (listNode1 instanceof LinkedList<?>) {
+            listNode3 = listNode1;
+        } else if (listNode1 == null) {
+            listNode3 = new LinkedList<>();
+        } else {
+            listNode3 = new LinkedList<>(listNode1);
         }
         if(ptermNode2 != null)
         {
@@ -8636,14 +8614,12 @@ public class Parser implements IParser
         PListTerm plisttermNode2;
         listNode1 = (List<PListTerm>)nodeArrayList1.get(0);
         plisttermNode2 = (PListTerm)nodeArrayList2.get(0);
-        listNode3 = new LinkedList<>();
-        if(listNode1 != null)
-        {
-            if (listNode3.isEmpty() && listNode1 instanceof LinkedList<?>) {
-                listNode3 = listNode1;
-            } else {
-                listNode3.addAll(listNode1);
-            }
+        if (listNode1 instanceof LinkedList<?>) {
+            listNode3 = listNode1;
+        } else if (listNode1 == null) {
+            listNode3 = new LinkedList<>();
+        } else {
+            listNode3 = new LinkedList<>(listNode1);
         }
         if(plisttermNode2 != null)
         {
@@ -8691,14 +8667,12 @@ public class Parser implements IParser
         PTerm ptermNode2;
         listNode1 = (List<PTerm>)nodeArrayList1.get(0);
         ptermNode2 = (PTerm)nodeArrayList2.get(0);
-        listNode3 = new LinkedList<>();
-        if(listNode1 != null)
-        {
-            if (listNode3.isEmpty() && listNode1 instanceof LinkedList<?>) {
-                listNode3 = listNode1;
-            } else {
-                listNode3.addAll(listNode1);
-            }
+        if (listNode1 instanceof LinkedList<?>) {
+            listNode3 = listNode1;
+        } else if (listNode1 == null) {
+            listNode3 = new LinkedList<>();
+        } else {
+            listNode3 = new LinkedList<>(listNode1);
         }
         if(ptermNode2 != null)
         {
@@ -8746,14 +8720,12 @@ public class Parser implements IParser
         PAstProd pastprodNode2;
         listNode1 = (List<PAstProd>)nodeArrayList1.get(0);
         pastprodNode2 = (PAstProd)nodeArrayList2.get(0);
-        listNode3 = new LinkedList<>();
-        if(listNode1 != null)
-        {
-            if (listNode3.isEmpty() && listNode1 instanceof LinkedList<?>) {
-                listNode3 = listNode1;
-            } else {
-                listNode3.addAll(listNode1);
-            }
+        if (listNode1 instanceof LinkedList<?>) {
+            listNode3 = listNode1;
+        } else if (listNode1 == null) {
+            listNode3 = new LinkedList<>();
+        } else {
+            listNode3 = new LinkedList<>(listNode1);
         }
         if(pastprodNode2 != null)
         {
@@ -8801,14 +8773,12 @@ public class Parser implements IParser
         PAstAlt pastaltNode2;
         listNode1 = (List<PAstAlt>)nodeArrayList1.get(0);
         pastaltNode2 = (PAstAlt)nodeArrayList2.get(0);
-        listNode3 = new LinkedList<>();
-        if(listNode1 != null)
-        {
-            if (listNode3.isEmpty() && listNode1 instanceof LinkedList<?>) {
-                listNode3 = listNode1;
-            } else {
-                listNode3.addAll(listNode1);
-            }
+        if (listNode1 instanceof LinkedList<?>) {
+            listNode3 = listNode1;
+        } else if (listNode1 == null) {
+            listNode3 = new LinkedList<>();
+        } else {
+            listNode3 = new LinkedList<>(listNode1);
         }
         if(pastaltNode2 != null)
         {
diff --git a/src/main/resources/org/sablecc/sablecc/parser.txt b/src/main/resources/org/sablecc/sablecc/parser.txt
index 951becb..3d1db58 100644
--- a/src/main/resources/org/sablecc/sablecc/parser.txt
+++ b/src/main/resources/org/sablecc/sablecc/parser.txt
@@ -373,6 +373,17 @@ Macro:ParserTypedLinkedListInit
 
 $
 
+Macro:ParserTypedLinkedListInitAddAll
+        if ($1$ instanceof LinkedList<?>) {
+            $0$ = $1$;
+        } else if ($1$ == null) {
+            $0$ = new LinkedList<>();
+        } else {
+            $0$ = new LinkedList<>($1$);
+        }
+
+$
+
 Macro:ParserTypedLinkedListAdd
         if($1$ != null)
         {
-- 
GitLab