Skip to content

Commit f574d87

Browse files
committed
Comments and csharpier
1 parent 0966731 commit f574d87

File tree

2 files changed

+34
-22
lines changed

2 files changed

+34
-22
lines changed

crates/bindings-csharp/BSATN.Codegen/Type.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -529,11 +529,14 @@ public override string ToString() =>
529529
"""
530530
);
531531

532+
// Directly allocating the result object here (instead of calling e.g. IStructuralReadWrite.Read<T>, which does the same thing)
533+
// avoids generics; we've found that generics often result in reflective code being generated.
534+
// Using simple code here hopefully helps IL2CPP and Mono do this faster.
532535
read = $$"""
533-
var ___result = new {{FullName}}();
534-
___result.ReadFields(reader);
535-
return ___result;
536-
""";
536+
var ___result = new {{FullName}}();
537+
___result.ReadFields(reader);
538+
return ___result;
539+
""";
537540

538541
write = "value.WriteFields(writer);";
539542

crates/bindings-csharp/BSATN.Runtime.Tests/Tests.cs

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -330,26 +330,38 @@ public void AssertCollisionsLessThan(double fraction)
330330
}
331331

332332
static void TestRoundTrip<T, BSATN>(Gen<T> gen, BSATN serializer)
333-
where BSATN : IReadWrite<T>
333+
where BSATN : IReadWrite<T>
334334
{
335-
gen.Sample((value) =>
336-
{
337-
var stream = new MemoryStream();
338-
var writer = new BinaryWriter(stream);
339-
serializer.Write(writer, value);
340-
stream.Seek(0, SeekOrigin.Begin);
341-
var reader = new BinaryReader(stream);
342-
var result = serializer.Read(reader);
343-
Assert.Equal(value, result);
344-
}, iter: 10_000);
335+
gen.Sample(
336+
(value) =>
337+
{
338+
var stream = new MemoryStream();
339+
var writer = new BinaryWriter(stream);
340+
serializer.Write(writer, value);
341+
stream.Seek(0, SeekOrigin.Begin);
342+
var reader = new BinaryReader(stream);
343+
var result = serializer.Read(reader);
344+
Assert.Equal(value, result);
345+
},
346+
iter: 10_000
347+
);
345348
}
346349

347350
[Fact]
348351
public static void GeneratedProductRoundTrip()
349352
{
350-
TestRoundTrip(GenBasic.Select(value => new BasicDataClass(value)), new BasicDataClass.BSATN());
351-
TestRoundTrip(GenBasic.Select(value => new BasicDataRecord(value)), new BasicDataRecord.BSATN());
352-
TestRoundTrip(GenBasic.Select(value => new BasicDataStruct(value)), new BasicDataStruct.BSATN());
353+
TestRoundTrip(
354+
GenBasic.Select(value => new BasicDataClass(value)),
355+
new BasicDataClass.BSATN()
356+
);
357+
TestRoundTrip(
358+
GenBasic.Select(value => new BasicDataRecord(value)),
359+
new BasicDataRecord.BSATN()
360+
);
361+
TestRoundTrip(
362+
GenBasic.Select(value => new BasicDataStruct(value)),
363+
new BasicDataStruct.BSATN()
364+
);
353365
}
354366

355367
[Fact]
@@ -432,8 +444,7 @@ public partial record BasicEnum
432444
BasicDataClass U,
433445
BasicDataStruct V,
434446
BasicDataRecord W
435-
)>
436-
{ }
447+
)> { }
437448

438449
static readonly Gen<BasicEnum> GenBasicEnum = Gen.SelectMany<int, BasicEnum>(
439450
Gen.Int[0, 7],
@@ -595,7 +606,6 @@ public ContainsNestedList(List<BasicEnum[][]> theList)
595606
.List[0, 2]
596607
.Select(list => new ContainsNestedList(list));
597608

598-
599609
[Fact]
600610
public static void GeneratedNestedListRoundTrip()
601611
{
@@ -646,7 +656,6 @@ public override int GetHashCode([DisallowNull] IEnumerable<T> obj)
646656
}
647657
}
648658

649-
650659
[Fact]
651660
public static void GeneratedNestedListEqualsWorks()
652661
{

0 commit comments

Comments
 (0)