Skip to content
Snippets Groups Projects
Commit 9136acca authored by dgelessus's avatar dgelessus
Browse files

Avoid creating intermediate LinkedLists in Parser new methods

parent 80afaf94
No related branches found
No related tags found
No related merge requests found
......@@ -139,14 +139,7 @@ public class GenerateAlternativeCodeForParser extends DepthFirstAdapter
macros.apply(file, "ParserBraceOpening");
}
if(typeName.startsWith("L"))
{
macros.apply(file, "ParserListVariableDeclaration", new String[] {variableTypeFromInternalType(typeName), getVariableName(term)});
}
else
{
macros.apply(file, "ParserSimpleVariableDeclaration", new String[] {variableTypeFromInternalType(typeName), getVariableName(term)});
}
macros.apply(file, "ParserVariableDeclaration", new String[] {variableTypeFromInternalType(typeName), getVariableName(term)});
return true;
}
......@@ -260,6 +253,12 @@ public class GenerateAlternativeCodeForParser extends DepthFirstAdapter
@Override
public void caseAListTerm(AListTerm node)
{
if(node.getListTerms().isEmpty())
{
// Empty lists are generated inline and don't have a variable.
return;
}
try
{
boolean openingBraceGenerated = false;
......@@ -273,13 +272,57 @@ public class GenerateAlternativeCodeForParser extends DepthFirstAdapter
listTerm.apply(this);
}
int nonNullTermsCount = 0;
PListTerm firstNonNullListTerm = null;
String firstNonNullTypeName = null;
for(PListTerm listTerm : node.getListTerms())
{
String type_name = lookupInternalTypeName(listTerm);
String typeName = lookupInternalTypeName(listTerm);
// Null and empty list terms never add any elements to the list and so don't need to be counted.
if("null".equals(typeName) || "Lnull".equals(typeName))
{
continue;
}
nonNullTermsCount++;
if(firstNonNullListTerm == null)
{
firstNonNullListTerm = listTerm;
firstNonNullTypeName = typeName;
}
}
if(nonNullTermsCount == 0)
{
throw new RuntimeException("List has non-empty type, but contains no elements!");
}
assert firstNonNullListTerm != null;
assert firstNonNullTypeName != null;
if(!type_name.equals("null"))
if(nonNullTermsCount == 1)
{
if(firstNonNullTypeName.startsWith("L"))
{
macros.apply(file, "ParserTypedListAssign", new String[] {getVariableName(node), getVariableName(firstNonNullListTerm)});
}
else
{
macros.apply(file, "ParserTypedSingleElementList", new String[] {getVariableName(node), getVariableName(firstNonNullListTerm)});
}
}
else
{
if(type_name.startsWith("L"))
macros.apply(file, "ParserTypedLinkedListInit", new String[] {getVariableName(node)});
for(PListTerm listTerm : node.getListTerms())
{
String typeName = lookupInternalTypeName(listTerm);
// Null and empty list terms never add any elements to the list.
if("null".equals(typeName) || "Lnull".equals(typeName))
{
continue;
}
if(typeName.startsWith("L"))
{
macros.apply(file, "ParserTypedLinkedListAddAll", new String[] {getVariableName(node), getVariableName(listTerm)});
}
......
This diff is collapsed.
......@@ -330,16 +330,11 @@ Macro:ParserBraceClosing
$
Macro:ParserSimpleVariableDeclaration
Macro:ParserVariableDeclaration
$0$ $1$;
$
Macro:ParserListVariableDeclaration
$0$ $1$ = new LinkedList<>();
$
Macro:ParserSimpleTerm
$0$ = ($1$)nodeArrayList$2$.get($3$);
......@@ -359,6 +354,25 @@ Macro:ParserNewBodyNewTail
$
Macro:ParserTypedSingleElementList
if($1$ != null) {
$0$ = Collections.singletonList($1$);
} else {
$0$ = Collections.emptyList();
}
$
Macro:ParserTypedListAssign
$0$ = $1$;
$
Macro:ParserTypedLinkedListInit
$0$ = new LinkedList<>();
$
Macro:ParserTypedLinkedListAdd
if($1$ != null)
{
......@@ -370,10 +384,10 @@ $
Macro:ParserTypedLinkedListAddAll
if($1$ != null)
{
if(!$0$.isEmpty()){
$0$.addAll($1$);
}else{
if ($0$.isEmpty() && $1$ instanceof LinkedList<?>) {
$0$ = $1$;
} else {
$0$.addAll($1$);
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment