Skip to content

Commit 34ad795

Browse files
ddobrevtritao
authored andcommitted
Optimized the generation of C# by avoiding splitting.
Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent 37409c7 commit 34ad795

File tree

3 files changed

+25
-40
lines changed

3 files changed

+25
-40
lines changed

src/Generator/Generators/CSharp/CSharpSources.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ private void GenerateClassInternalsField(LayoutField field, Class @class)
727727
var fieldValuePrinted = field.Expression.CSharpValue(ExpressionPrinter);
728728
Write($" = {fieldValuePrinted}");
729729
}
730-
Write(";");
730+
WriteLine(";");
731731

732732
PopBlock(NewLineKind.BeforeNextBlock);
733733
}
@@ -757,7 +757,7 @@ private void GeneratePropertySetter<T>(T decl,
757757
{
758758
if (isAbstract)
759759
{
760-
Write(";");
760+
WriteLine(";");
761761
PopBlock(NewLineKind.BeforeNextBlock);
762762
return;
763763
}
@@ -1044,7 +1044,7 @@ private void GeneratePropertyGetter<T>(T decl, Class @class,
10441044
{
10451045
if (isAbstract)
10461046
{
1047-
Write(";");
1047+
WriteLine(";");
10481048
PopBlock(NewLineKind.BeforeNextBlock);
10491049
return;
10501050
}
@@ -1707,7 +1707,7 @@ private void GenerateVTableManagedCall(Method method)
17071707
}
17081708
else
17091709
{
1710-
Write($"{property.Name}");
1710+
Write(property.Name);
17111711
if (isSetter)
17121712
Write($" = {marshalsCode}");
17131713
}
@@ -2329,7 +2329,7 @@ public void GenerateMethod(Method method, Class @class)
23292329

23302330
if (method.IsPure)
23312331
{
2332-
Write(";");
2332+
WriteLine(";");
23332333
PopBlock(NewLineKind.BeforeNextBlock);
23342334
return;
23352335
}

src/Generator/Utils/BlockGenerator.cs

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,15 @@ public IEnumerable<Block> FindBlocks(BlockKind kind)
110110
}
111111
}
112112

113-
public virtual string Generate()
113+
public virtual StringBuilder Generate()
114114
{
115115
if (CheckGenerate != null && !CheckGenerate())
116-
return "";
116+
return new StringBuilder();
117117

118118
if (Blocks.Count == 0)
119-
return Text.ToString();
119+
return Text.StringBuilder;
120120

121121
var builder = new StringBuilder();
122-
uint totalIndent = 0;
123122
Block previousBlock = null;
124123

125124
var blockIndex = 0;
@@ -135,52 +134,33 @@ public virtual string Generate()
135134
if (nextBlock != null)
136135
{
137136
var nextText = nextBlock.Generate();
138-
if (string.IsNullOrEmpty(nextText) &&
137+
if (nextText.Length == 0 &&
139138
childBlock.NewLineKind == NewLineKind.IfNotEmpty)
140139
skipBlock = true;
141140
}
142141

143142
if (skipBlock)
144143
continue;
145144

146-
if (string.IsNullOrEmpty(childText))
145+
if (childText.Length == 0)
147146
continue;
148147

149-
var lines = childText.SplitAndKeep(Environment.NewLine).ToList();
150-
151148
if (previousBlock != null &&
152149
previousBlock.NewLineKind == NewLineKind.BeforeNextBlock)
153150
builder.AppendLine();
154151

155-
if (childBlock.isSubBlock)
156-
totalIndent = 0;
157-
158-
foreach (var line in lines)
159-
{
160-
if (string.IsNullOrEmpty(line))
161-
continue;
162-
163-
if (!string.IsNullOrWhiteSpace(line))
164-
builder.Append(new string(' ', (int)totalIndent));
165-
166-
builder.Append(line);
167-
168-
if (!line.EndsWith(Environment.NewLine, StringComparison.Ordinal))
169-
builder.AppendLine();
170-
}
152+
builder.Append(childText);
171153

172154
if (childBlock.NewLineKind == NewLineKind.Always)
173155
builder.AppendLine();
174156

175-
totalIndent += childBlock.Text.Indent;
176-
177157
previousBlock = childBlock;
178158
}
179159

180160
if (Text.StringBuilder.Length != 0)
181161
builder.Append(Text.StringBuilder);
182162

183-
return builder.ToString();
163+
return builder;
184164
}
185165

186166
public bool IsEmpty
@@ -271,7 +251,7 @@ protected BlockGenerator()
271251

272252
public virtual string Generate()
273253
{
274-
return RootBlock.Generate();
254+
return RootBlock.Generate().ToString();
275255
}
276256

277257
#region Block helpers
@@ -284,6 +264,14 @@ public void AddBlock(Block block)
284264
public void PushBlock(BlockKind kind = BlockKind.Unknown, object obj = null)
285265
{
286266
var block = new Block { Kind = kind, Object = obj };
267+
var array = new uint[ActiveBlock.Text.CurrentIndent.Count];
268+
ActiveBlock.Text.CurrentIndent.CopyTo(array, 0);
269+
foreach (var indent in array.Reverse())
270+
{
271+
block.Text.CurrentIndent.Push(indent);
272+
}
273+
block.Text.IsStartOfLine = ActiveBlock.Text.IsStartOfLine;
274+
block.Text.NeedsNewLine = ActiveBlock.Text.NeedsNewLine;
287275
PushBlock(block);
288276
}
289277

src/Generator/Utils/TextGenerator.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ public class TextGenerator : ITextGenerator
2525
{
2626
public const uint DefaultIndent = 4;
2727

28-
public StringBuilder StringBuilder;
29-
protected bool IsStartOfLine;
30-
protected bool NeedsNewLine;
31-
protected readonly Stack<uint> CurrentIndent;
28+
public StringBuilder StringBuilder = new StringBuilder();
29+
public bool IsStartOfLine { get; set; }
30+
public bool NeedsNewLine { get; set; }
31+
public Stack<uint> CurrentIndent { get; } = new Stack<uint>();
3232

3333
public uint Indent
3434
{
@@ -37,9 +37,6 @@ public uint Indent
3737

3838
public TextGenerator()
3939
{
40-
StringBuilder = new StringBuilder();
41-
IsStartOfLine = false;
42-
CurrentIndent = new Stack<uint>();
4340
}
4441

4542
public TextGenerator(TextGenerator generator)

0 commit comments

Comments
 (0)