Internal: ZScript: now using StringBuilder in the internal tokenizer loops, might speed it up a bit

This commit is contained in:
ZZYZX 2017-02-09 04:07:15 +02:00
parent 1fb1167e5d
commit e62bbba9ff
3 changed files with 34 additions and 29 deletions

View file

@ -30,6 +30,6 @@ using CodeImp.DoomBuilder;
// Build Number // Build Number
// Revision // Revision
// //
[assembly: AssemblyVersion("2.3.0.2873")] [assembly: AssemblyVersion("2.3.0.2875")]
[assembly: NeutralResourcesLanguageAttribute("en")] [assembly: NeutralResourcesLanguageAttribute("en")]
[assembly: AssemblyHash("1382d14")] [assembly: AssemblyHash("1fb1167")]

View file

@ -121,6 +121,7 @@ namespace CodeImp.DoomBuilder.ZDoom
private static Dictionary<string, ZScriptTokenType> namedtokentypes; // these are tokens that have precise equivalent in the enum (like operators) private static Dictionary<string, ZScriptTokenType> namedtokentypes; // these are tokens that have precise equivalent in the enum (like operators)
private static Dictionary<ZScriptTokenType, string> namedtokentypesreverse; // these are tokens that have precise equivalent in the enum (like operators) private static Dictionary<ZScriptTokenType, string> namedtokentypesreverse; // these are tokens that have precise equivalent in the enum (like operators)
private static List<string> namedtokentypesorder; // this is the list of said tokens ordered by length. private static List<string> namedtokentypesorder; // this is the list of said tokens ordered by length.
private static StringBuilder SB;
public BinaryReader Reader { get { return reader; } } public BinaryReader Reader { get { return reader; } }
public long LastPosition { get; private set; } public long LastPosition { get; private set; }
@ -129,6 +130,9 @@ namespace CodeImp.DoomBuilder.ZDoom
{ {
reader = br; reader = br;
if (SB == null)
SB = new StringBuilder();
if (namedtokentypes == null || namedtokentypesreverse == null || namedtokentypesorder == null) if (namedtokentypes == null || namedtokentypesreverse == null || namedtokentypesorder == null)
{ {
namedtokentypes = new Dictionary<string, ZScriptTokenType>(); namedtokentypes = new Dictionary<string, ZScriptTokenType>();
@ -179,14 +183,15 @@ namespace CodeImp.DoomBuilder.ZDoom
// check whitespace // check whitespace
if (whitespace.Contains(c)) if (whitespace.Contains(c))
{ {
string ws_content = ""; //string ws_content = "";
ws_content += c; SB.Length = 0;
SB.Append(c);
while (true) while (true)
{ {
char cnext = reader.ReadChar(); char cnext = reader.ReadChar();
if (whitespace.Contains(cnext)) if (whitespace.Contains(cnext))
{ {
ws_content += cnext; SB.Append(cnext);
continue; continue;
} }
@ -196,7 +201,7 @@ namespace CodeImp.DoomBuilder.ZDoom
ZScriptToken tok = new ZScriptToken(); ZScriptToken tok = new ZScriptToken();
tok.Type = ZScriptTokenType.Whitespace; tok.Type = ZScriptTokenType.Whitespace;
tok.Value = ws_content; tok.Value = SB.ToString();
return tok; return tok;
} }
@ -214,8 +219,8 @@ namespace CodeImp.DoomBuilder.ZDoom
(c >= 'A' && c <= 'Z') || (c >= 'A' && c <= 'Z') ||
(c == '_')) (c == '_'))
{ {
string id_content = ""; SB.Length = 0;
id_content += c; SB.Append(c);
while (true) while (true)
{ {
char cnext = reader.ReadChar(); char cnext = reader.ReadChar();
@ -224,7 +229,7 @@ namespace CodeImp.DoomBuilder.ZDoom
(cnext == '_') || (cnext == '_') ||
(cnext >= '0' && cnext <= '9')) (cnext >= '0' && cnext <= '9'))
{ {
id_content += cnext; SB.Append(cnext);
continue; continue;
} }
@ -234,7 +239,7 @@ namespace CodeImp.DoomBuilder.ZDoom
ZScriptToken tok = new ZScriptToken(); ZScriptToken tok = new ZScriptToken();
tok.Type = ZScriptTokenType.Identifier; tok.Type = ZScriptTokenType.Identifier;
tok.Value = id_content; tok.Value = SB.ToString();
return tok; return tok;
} }
@ -267,12 +272,12 @@ namespace CodeImp.DoomBuilder.ZDoom
{ {
bool isoctal = (c == '0'); bool isoctal = (c == '0');
bool ishex = false; bool ishex = false;
string i_content = ""; SB.Length = 0;
i_content += c; SB.Append(c);
while (true) while (true)
{ {
char cnext = reader.ReadChar(); char cnext = reader.ReadChar();
if (!isdouble && (cnext == 'x') && i_content.Length == 1) if (!isdouble && (cnext == 'x') && SB.Length == 1)
{ {
isoctal = false; isoctal = false;
ishex = true; ishex = true;
@ -281,21 +286,21 @@ namespace CodeImp.DoomBuilder.ZDoom
(!isoctal && cnext >= '8' && cnext <= '9') || (!isoctal && cnext >= '8' && cnext <= '9') ||
(ishex && ((cnext >= 'a' && cnext <= 'f') || (cnext >= 'A' && cnext <= 'F')))) (ishex && ((cnext >= 'a' && cnext <= 'f') || (cnext >= 'A' && cnext <= 'F'))))
{ {
i_content += cnext; SB.Append(cnext);
} }
else if (!ishex && !isdouble && !isexponent && cnext == '.') else if (!ishex && !isdouble && !isexponent && cnext == '.')
{ {
isdouble = true; isdouble = true;
isoctal = false; isoctal = false;
i_content += '.'; SB.Append('.');
} }
else if (!isoctal && !ishex && !isexponent && (cnext == 'e' || cnext == 'E')) else if (!isoctal && !ishex && !isexponent && (cnext == 'e' || cnext == 'E'))
{ {
isexponent = true; isexponent = true;
isdouble = true; isdouble = true;
i_content += 'e'; SB.Append('e');
cnext = reader.ReadChar(); cnext = reader.ReadChar();
if (cnext == '-') i_content += '-'; if (cnext == '-') SB.Append('-');
else reader.BaseStream.Position--; else reader.BaseStream.Position--;
} }
else else
@ -307,7 +312,7 @@ namespace CodeImp.DoomBuilder.ZDoom
ZScriptToken tok = new ZScriptToken(); ZScriptToken tok = new ZScriptToken();
tok.Type = (isdouble ? ZScriptTokenType.Double : ZScriptTokenType.Integer); tok.Type = (isdouble ? ZScriptTokenType.Double : ZScriptTokenType.Integer);
tok.Value = i_content; tok.Value = SB.ToString();
try try
{ {
if (ishex || isoctal || !isdouble) if (ishex || isoctal || !isdouble)
@ -354,7 +359,7 @@ namespace CodeImp.DoomBuilder.ZDoom
{ {
if (!allowline) break; if (!allowline) break;
// line comment: read until newline but not including it // line comment: read until newline but not including it
string cmt = ""; SB.Length = 0;
while (true) while (true)
{ {
cnext = reader.ReadChar(); cnext = reader.ReadChar();
@ -364,19 +369,19 @@ namespace CodeImp.DoomBuilder.ZDoom
break; break;
} }
cmt += cnext; SB.Append(cnext);
} }
ZScriptToken tok = new ZScriptToken(); ZScriptToken tok = new ZScriptToken();
tok.Type = ZScriptTokenType.LineComment; tok.Type = ZScriptTokenType.LineComment;
tok.Value = cmt; tok.Value = SB.ToString();
return tok; return tok;
} }
else if (cnext == '*') else if (cnext == '*')
{ {
if (!allowblock) break; if (!allowblock) break;
// block comment: read until closing sequence // block comment: read until closing sequence
string cmt = ""; SB.Length = 0;
while (true) while (true)
{ {
cnext = reader.ReadChar(); cnext = reader.ReadChar();
@ -389,12 +394,12 @@ namespace CodeImp.DoomBuilder.ZDoom
reader.BaseStream.Position--; reader.BaseStream.Position--;
} }
cmt += cnext; SB.Append(cnext);
} }
ZScriptToken tok = new ZScriptToken(); ZScriptToken tok = new ZScriptToken();
tok.Type = ZScriptTokenType.BlockComment; tok.Type = ZScriptTokenType.BlockComment;
tok.Value = cmt; tok.Value = SB.ToString();
return tok; return tok;
} }
break; break;
@ -405,7 +410,7 @@ namespace CodeImp.DoomBuilder.ZDoom
{ {
if ((c == '"' && !allowstring) || (c == '\'' && !allowname)) break; if ((c == '"' && !allowstring) || (c == '\'' && !allowname)) break;
ZScriptTokenType type = (c == '"' ? ZScriptTokenType.String : ZScriptTokenType.Name); ZScriptTokenType type = (c == '"' ? ZScriptTokenType.String : ZScriptTokenType.Name);
string s = ""; SB.Length = 0;
while (true) while (true)
{ {
// todo: parse escape sequences properly // todo: parse escape sequences properly
@ -413,16 +418,16 @@ namespace CodeImp.DoomBuilder.ZDoom
if (cnext == '\\') // escape sequence. right now, do nothing if (cnext == '\\') // escape sequence. right now, do nothing
{ {
cnext = reader.ReadChar(); cnext = reader.ReadChar();
s += cnext; SB.Append(cnext);
} }
else if (cnext == c) else if (cnext == c)
{ {
ZScriptToken tok = new ZScriptToken(); ZScriptToken tok = new ZScriptToken();
tok.Type = type; tok.Type = type;
tok.Value = s; tok.Value = SB.ToString();
return tok; return tok;
} }
else s += cnext; else SB.Append(cnext);
} }
} }

View file

@ -29,5 +29,5 @@ using System.Resources;
// Build Number // Build Number
// Revision // Revision
// //
[assembly: AssemblyVersion("2.3.0.2873")] [assembly: AssemblyVersion("2.3.0.2875")]
[assembly: NeutralResourcesLanguageAttribute("en")] [assembly: NeutralResourcesLanguageAttribute("en")]