Skip to content

Avoid array lookups where simple constants are fine #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 18, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions std/d/ast.d
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ class LabeledStatement : NonEmptyStatementNoCaseNoDefault
interface ExpressionStatement : NonEmptyStatementNoCaseNoDefault {}
interface DeclarationStatement : NonEmptyStatementNoCaseNoDefault {}

/+

/**
* $(LINK2 http://dlang.org/statement.html#IfStatement)
*/
Expand Down Expand Up @@ -395,3 +397,6 @@ class Inherits : DeclDef
//FunctionDeclaration[] functions;
}
+/


+/
22 changes: 14 additions & 8 deletions std/d/lexer.d
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,6 @@ L_advance:
// since this branch at most is taken once per file
_empty = true;
return;
// pragma(msg, generateCaseTrie(
mixin(generateCaseTrie(
"=", "TokenType.assign",
"@", "TokenType.at",
Expand Down Expand Up @@ -485,7 +484,7 @@ L_advance:
if (!src.canPeek())
{
current.type = TokenType.dot;
current.value = getTokenValue(TokenType.dot);
current.value = tokenValue!(TokenType.dot);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised the optimizer didn't do this for us.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here he most likely did it, but see other cases where current.type is used.
Plus debug builds matter and there is dmd to care about ;)

return;
}
switch (src.peek())
Expand All @@ -501,13 +500,15 @@ L_advance:
{
current.type = TokenType.vararg;
nextCharNonLF();
current.value = tokenValue!(TokenType.vararg);
}
current.value = getTokenValue(current.type);
else
current.value = tokenValue!(TokenType.slice);
return;
default:
nextCharNonLF();
current.type = TokenType.dot;
current.value = getTokenValue(TokenType.dot);
current.value = tokenValue!(TokenType.dot);
return;
}
case '0': .. case '9':
Expand Down Expand Up @@ -1464,7 +1465,7 @@ L_advance:
else
{
current.type = TokenType.hash;
current.value = getTokenValue(TokenType.hash);
current.value = tokenValue!(TokenType.hash);
}
}

Expand Down Expand Up @@ -2705,6 +2706,11 @@ pure string getTokenValue(const TokenType type)
return tokenValues[type];
}

template tokenValue(TokenType val)
{
enum tokenValue = getTokenValue(val);
}

private pure bool isNewline(ubyte ch)
{
return ch == '\n' || ch == '\r';
Expand Down Expand Up @@ -2955,7 +2961,7 @@ string printCaseStatements(K, V)(TrieNode!(K,V) node, string indentString)
caseStatement ~= indentString;
caseStatement ~= "\t{\n";
caseStatement ~= indentString;
caseStatement ~= "\t\tcurrent.value = getTokenValue(current.type);\n";
caseStatement ~= "\t\tcurrent.value = tokenValue!("~node.children[k].value~");\n";
caseStatement ~= indentString;
caseStatement ~= "\t\tcurrent.type = " ~ node.children[k].value;
caseStatement ~= ";\n";
Expand All @@ -2975,7 +2981,7 @@ string printCaseStatements(K, V)(TrieNode!(K,V) node, string indentString)
caseStatement ~= v.value;
caseStatement ~= ";\n";
caseStatement ~= indentString;
caseStatement ~= "\t\tcurrent.value = getTokenValue(current.type);\n";
caseStatement ~= "\t\tcurrent.value = tokenValue!("~v.value~");\n";
caseStatement ~= indentString;
caseStatement ~= "\t\treturn;\n";
caseStatement ~= indentString;
Expand All @@ -2988,7 +2994,7 @@ string printCaseStatements(K, V)(TrieNode!(K,V) node, string indentString)
caseStatement ~= v.value;
caseStatement ~= ";\n";
caseStatement ~= indentString;
caseStatement ~= "\tcurrent.value = getTokenValue(current.type);\n";
caseStatement ~= "\tcurrent.value = tokenValue!("~v.value~");\n";
caseStatement ~= indentString;
caseStatement ~= "\treturn;\n";
}
Expand Down