Skip to content

Commit

Permalink
Reduce allocations in AbstractFileWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
sharwell committed May 3, 2023
1 parent 87080ad commit 054298d
Showing 1 changed file with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ internal abstract class AbstractFileWriter

private readonly IDictionary<string, Node> _nodeMap;
private readonly IDictionary<string, TreeType> _typeMap;
private readonly Dictionary<string, string> _camelCaseMap = new(StringComparer.Ordinal);
private readonly List<string> _indentMap = new();

private const int INDENT_SIZE = 4;
private int _indentLevel;
Expand Down Expand Up @@ -94,7 +96,12 @@ private void WriteIndentIfNeeded()
{
if (_needIndent)
{
_writer.Write(new string(' ', _indentLevel * INDENT_SIZE));
while (_indentMap.Count <= _indentLevel)
{
_indentMap.Add(new string(' ', _indentMap.Count * INDENT_SIZE));
}

_writer.Write(_indentMap[_indentLevel]);
_needIndent = false;
}
}
Expand Down Expand Up @@ -260,13 +267,23 @@ protected static bool HasErrors(Node n)
return n.Errors == null || string.Compare(n.Errors, "true", true) == 0;
}

protected static string CamelCase(string name)
protected string CamelCase(string name)
{
if (_camelCaseMap.TryGetValue(name, out var camelCase))
return camelCase;

if (char.IsUpper(name[0]))
{
name = char.ToLowerInvariant(name[0]) + name.Substring(1);
camelCase = char.ToLowerInvariant(name[0]) + name.Substring(1);
}
return FixKeyword(name);
else
{
camelCase = name;
}

camelCase = FixKeyword(camelCase);
_camelCaseMap.Add(name, camelCase);
return camelCase;
}

protected static string FixKeyword(string name)
Expand Down

0 comments on commit 054298d

Please sign in to comment.