Skip to content

Commit 5ed2dbe

Browse files
committed
Improve C3 code gen
1 parent 97fdc4b commit 5ed2dbe

15 files changed

+142
-114
lines changed

Src/FastData.Generator.CSharp.Tests/Features/ObjectSupportTest-ArrayStructure_Int32_3.verified.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public class Person
2121

2222
internal static class ArrayStructure_Int32_3
2323
{
24+
private static readonly Person[] _values = {
25+
new Person(1, "Bob", new Person(4, "Anna", null)), new Person(2, "Billy", null), new Person(3, "Bibi", null)
26+
};
2427
private static readonly int[] _keys = new int[] {
2528
1, 2, 3
2629
};
@@ -38,17 +41,14 @@ internal static class ArrayStructure_Int32_3
3841
}
3942
return false;
4043
}
41-
private static readonly Person[] _values = {
42-
new Person(1, "Bob", new Person(4, "Anna", null)), new Person(2, "Billy", null), new Person(3, "Bibi", null)
43-
};
44-
4544

4645
public static bool TryLookup(int key, out Person? value)
4746
{
48-
value = default;
49-
5047
if (key < 1 || key > 3)
48+
{
49+
value = default;
5150
return false;
51+
}
5252

5353
for (uint i = 0; i < 3; i++)
5454
{

Src/FastData.Generator.CSharp.Tests/Features/ObjectSupportTest-BinarySearchStructure_Int32_3.verified.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public class Person
2121

2222
internal static class BinarySearchStructure_Int32_3
2323
{
24+
private static readonly Person[] _values = {
25+
new Person(1, "Bob", new Person(4, "Anna", null)), new Person(2, "Billy", null), new Person(3, "Bibi", null)
26+
};
2427
private static readonly int[] _keys = new int[] {
2528
1, 2, 3
2629
};
@@ -48,17 +51,14 @@ internal static class BinarySearchStructure_Int32_3
4851

4952
return ~lo >= 0;
5053
}
51-
private static readonly Person[] _values = {
52-
new Person(1, "Bob", new Person(4, "Anna", null)), new Person(2, "Billy", null), new Person(3, "Bibi", null)
53-
};
54-
5554

5655
public static bool TryLookup(int key, out Person? value)
5756
{
58-
value = default;
59-
6057
if (key < 1 || key > 3)
58+
{
59+
value = default;
6160
return false;
61+
}
6262

6363
int lo = 0;
6464
int hi = 2;

Src/FastData.Generator.CSharp.Tests/Features/ObjectSupportTest-ConditionalStructure_Int32_3.verified.txt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public class Person
2121

2222
internal static class ConditionalStructure_Int32_3
2323
{
24+
private static readonly Person[] _values = {
25+
new Person(1, "Bob", new Person(4, "Anna", null)), new Person(2, "Billy", null), new Person(3, "Bibi", null)
26+
};
2427

2528
public static bool Contains(int key)
2629
{
@@ -35,12 +38,7 @@ internal static class ConditionalStructure_Int32_3
3538
default:
3639
return false;
3740
}
38-
}
39-
private static readonly Person[] _values = {
40-
new Person(1, "Bob", new Person(4, "Anna", null)), new Person(2, "Billy", null), new Person(3, "Bibi", null)
41-
};
42-
43-
41+
}
4442
public static bool TryLookup(int key, out Person? value)
4543
{
4644
value = default;

Src/FastData.Generator.CSharp.Tests/Features/ObjectSupportTest-HashTablePerfectStructure_Int32_3.verified.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,11 @@ private struct E
6161

6262
public static bool TryLookup(int key, out Person value)
6363
{
64-
value = default;
6564
if (key < 1 || key > 3)
65+
{
66+
value = default;
6667
return false;
68+
}
6769

6870
ulong hash = Hash(key);
6971
uint index = (uint)(hash % 3);

Src/FastData.Generator.CSharp.Tests/Features/ObjectSupportTest-HashTableStructure_Int32_3.verified.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,11 @@ internal static class HashTableStructure_Int32_3
7777

7878
public static bool TryLookup(int key, out Person value)
7979
{
80-
value = default;
8180
if (key < 1 || key > 3)
81+
{
82+
value = default;
8283
return false;
84+
}
8385

8486
ulong hash = Hash(key);
8587
uint index = (uint)(hash % 3);

Src/FastData.Generator.CSharp.Tests/Features/ObjectSupportTest-KeyLengthStructure_String_3.verified.txt

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ internal static class KeyLengthStructure_String_3
2424
private static readonly int[] _offsets = {
2525
0, 1, 2
2626
};
27+
2728
private static readonly Person[] _values = {
2829
new Person(1, "Bob", new Person(4, "Anna", null)), new Person(2, "Billy", null), new Person(3, "Bibi", null)
2930
};
@@ -38,23 +39,25 @@ internal static class KeyLengthStructure_String_3
3839
return false;
3940

4041
return StringComparer.Ordinal.Equals(key, _keys[key.Length - 1]);
41-
}
42-
public static bool TryLookup(string key, out Person value)
43-
{
44-
value = default;
42+
}
43+
public static bool TryLookup(string key, out Person value)
44+
{
4545
if (key.Length < 1u || key.Length > 3u)
46+
{
47+
value = default;
4648
return false;
49+
}
4750

48-
int idx = key.Length - 1;
49-
if (StringComparer.Ordinal.Equals(key, _keys[idx]))
50-
{
51-
value = _values[_offsets[idx]];
52-
return true;
53-
}
51+
int idx = key.Length - 1;
52+
if (StringComparer.Ordinal.Equals(key, _keys[idx]))
53+
{
54+
value = _values[_offsets[idx]];
55+
return true;
56+
}
5457

55-
value = default;
56-
return false;
57-
}
58+
value = default;
59+
return false;
60+
}
5861

5962
public const uint ItemCount = 3;
6063
public const uint MinKeyLength = 1;

Src/FastData.Generator.CSharp.Tests/Features/ObjectSupportTest-SingleValueStructure_Int32_1.verified.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,11 @@ public class Person
2121

2222
internal static class SingleValueStructure_Int32_1
2323
{
24-
24+
private static readonly Person _storedValue = new Person(1, "Bob", new Person(4, "Anna", null));
2525
public static bool Contains(int key)
2626
{
2727
return key == 1;
2828
}
29-
private static readonly Person _storedValue = new Person(1, "Bob", new Person(4, "Anna", null));
30-
3129

3230
public static bool TryLookup(int key, out Person value)
3331
{

Src/FastData.Generator.CSharp/Internal/Framework/CSharpEarlyExitDef.cs

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,42 @@ internal class CSharpEarlyExitDef(TypeMap map, CSharpOptions options) : EarlyExi
1010
{
1111
protected override bool IsEnabled => !options.HasFlag(CSharpOptions.DisableEarlyExits);
1212

13-
protected override string GetMaskEarlyExit(MethodType methodType, ulong bitSet) =>
14-
$"""
15-
if (({bitSet}UL & (1UL << (key.Length - 1))) == 0)
16-
return false;
17-
""";
13+
protected override string GetMaskEarlyExit(MethodType methodType, ulong bitSet) => methodType == MethodType.Contains
14+
? $"""
15+
if (({bitSet}UL & (1UL << (key.Length - 1))) == 0)
16+
return false;
17+
"""
18+
: $$"""
19+
if (({{bitSet}}UL & (1UL << (key.Length - 1))) == 0)
20+
{
21+
value = default;
22+
return false;
23+
}
24+
""";
1825

19-
protected override string GetValueEarlyExits<T>(MethodType methodType, T min, T max) =>
20-
$"""
21-
if ({(min.Equals(max) ? $"key != {map.ToValueLabel(max)}" : $"key < {map.ToValueLabel(min)} || key > {map.ToValueLabel(max)}")})
22-
return false;
23-
""";
26+
protected override string GetValueEarlyExits<T>(MethodType methodType, T min, T max) => methodType == MethodType.Contains
27+
? $"""
28+
if ({(min.Equals(max) ? $"key != {map.ToValueLabel(max)}" : $"key < {map.ToValueLabel(min)} || key > {map.ToValueLabel(max)}")})
29+
return false;
30+
"""
31+
: $$"""
32+
if ({{(min.Equals(max) ? $"key != {map.ToValueLabel(max)}" : $"key < {map.ToValueLabel(min)} || key > {map.ToValueLabel(max)}")}})
33+
{
34+
value = default;
35+
return false;
36+
}
37+
""";
2438

25-
protected override string GetLengthEarlyExits(MethodType methodType, uint min, uint max, uint minByte, uint maxByte) =>
26-
$"""
27-
if ({(min.Equals(max) ? $"key.Length != {map.ToValueLabel(max)}" : $"key.Length < {map.ToValueLabel(min)} || key.Length > {map.ToValueLabel(max)}")})
28-
return false;
29-
""";
39+
protected override string GetLengthEarlyExits(MethodType methodType, uint min, uint max, uint minByte, uint maxByte) => methodType == MethodType.Contains
40+
? $"""
41+
if ({(min.Equals(max) ? $"key.Length != {map.ToValueLabel(max)}" : $"key.Length < {map.ToValueLabel(min)} || key.Length > {map.ToValueLabel(max)}")})
42+
return false;
43+
"""
44+
: $$"""
45+
if ({{(min.Equals(max) ? $"key.Length != {map.ToValueLabel(max)}" : $"key.Length < {map.ToValueLabel(min)} || key.Length > {map.ToValueLabel(max)}")}})
46+
{
47+
value = default;
48+
return false;
49+
}
50+
""";
3051
}

Src/FastData.Generator.CSharp/Internal/Generators/ArrayCode.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ public override string Generate()
1111
{
1212
StringBuilder sb = new StringBuilder();
1313

14+
if (ctx.Values != null)
15+
{
16+
shared.Add(CodePlacement.Before, GetObjectDeclarations<TValue>());
17+
18+
sb.Append($$"""
19+
{{FieldModifier}}{{ValueTypeName}}[] _values = {
20+
{{FormatColumns(ctx.Values, ToValueLabel)}}
21+
};
22+
23+
""");
24+
}
25+
1426
sb.Append($$"""
1527
{{FieldModifier}}{{KeyTypeName}}[] _keys = new {{KeyTypeName}}[] {
1628
{{FormatColumns(ctx.Keys, ToValueLabel)}}
@@ -32,19 +44,11 @@ public override string Generate()
3244

3345
if (ctx.Values != null)
3446
{
35-
shared.Add(CodePlacement.Before, GetObjectDeclarations<TValue>());
36-
3747
sb.Append($$"""
3848
39-
{{FieldModifier}}{{ValueTypeName}}[] _values = {
40-
{{FormatColumns(ctx.Values, ToValueLabel)}}
41-
};
42-
4349
{{MethodAttribute}}
4450
{{MethodModifier}}bool TryLookup({{KeyTypeName}} key, out {{ValueTypeName}}? value)
4551
{
46-
value = default;
47-
4852
{{GetEarlyExits(MethodType.TryLookup)}}
4953
5054
for ({{ArraySizeType}} i = 0; i < {{ctx.Keys.Length.ToStringInvariant()}}; i++)

Src/FastData.Generator.CSharp/Internal/Generators/BinarySearchCode.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ public override string Generate()
1111
{
1212
StringBuilder sb = new StringBuilder();
1313

14+
if (ctx.Values != null)
15+
{
16+
shared.Add(CodePlacement.Before, GetObjectDeclarations<TValue>());
17+
18+
sb.Append($$"""
19+
{{FieldModifier}}{{ValueTypeName}}[] _values = {
20+
{{FormatColumns(ctx.Values, ToValueLabel)}}
21+
};
22+
23+
""");
24+
}
25+
1426
sb.Append($$"""
1527
{{FieldModifier}}{{KeyTypeName}}[] _keys = new {{KeyTypeName}}[] {
1628
{{FormatColumns(ctx.Keys, ToValueLabel)}}
@@ -42,19 +54,11 @@ public override string Generate()
4254

4355
if (ctx.Values != null)
4456
{
45-
shared.Add(CodePlacement.Before, GetObjectDeclarations<TValue>());
46-
4757
sb.Append($$"""
4858
49-
{{FieldModifier}}{{ValueTypeName}}[] _values = {
50-
{{FormatColumns(ctx.Values, ToValueLabel)}}
51-
};
52-
5359
{{MethodAttribute}}
5460
{{MethodModifier}}bool TryLookup({{KeyTypeName}} key, out {{ValueTypeName}}? value)
5561
{
56-
value = default;
57-
5862
{{GetEarlyExits(MethodType.TryLookup)}}
5963
6064
int lo = 0;

0 commit comments

Comments
 (0)