Skip to content

Commit

Permalink
fix IFixedSizeMemoryPackable optimization does not work
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Dec 14, 2022
1 parent 6162c97 commit 76a1381
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 20 deletions.
2 changes: 1 addition & 1 deletion sandbox/Net6VsNet7/Net6VsNet7.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>net7.0;net6.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
Expand Down
55 changes: 38 additions & 17 deletions sandbox/Net6VsNet7/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
using MemoryPack;
using System.Buffers;
using System.Reflection;

var config = ManualConfig.CreateMinimumViable()
.AddDiagnoser(MemoryDiagnoser.Default)
Expand All @@ -15,17 +17,24 @@
.AddJob(Job.Default.WithWarmupCount(1).WithIterationCount(1).WithRuntime(CoreRuntime.Core60))
.AddJob(Job.Default.WithWarmupCount(1).WithIterationCount(1).WithRuntime(CoreRuntime.Core70));

BenchmarkRunner.Run<Net6Net7>(config);
#if DEBUG

#else
BenchmarkSwitcher.FromTypes(new[] { typeof(Net6Net7<>) }).RunAllJoined(config);
#endif

[GenericTypeArguments(typeof(Sample))]
[GenericTypeArguments(typeof(Sample2))]
[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByMethod)]
public class Net6Net7
public class Net6Net7<T>
where T : class, new()
{
private Sample s = new();

private T s;
byte[] bin;

public Net6Net7()
{
s = new();
bin = MemoryPackSerializer.Serialize(s);
}

Expand All @@ -41,35 +50,47 @@ public byte[] Serialize()
return MemoryPackSerializer.Serialize(s);
}

[Benchmark]
public Sample? Deserialize()
{
return MemoryPackSerializer.Deserialize<Sample>(bin);
}
//[Benchmark]
//public Sample? Deserialize()
//{
// return MemoryPackSerializer.Deserialize<Sample>(bin);
//}
}

[MemoryPackable]
public partial class Sample
{
// these types are serialized by default
public int PublicField;
public readonly int PublicReadOnlyField;
public int PublicProperty { get; set; }
public int PrivateSetPublicProperty { get; private set; }
public int ReadOnlyPublicProperty { get; }
public int InitProperty { get; init; }
// public required int RequiredInitProperty { get; init; }

// these types are not serialized by default
int privateProperty { get; set; }
int privateField;
readonly int privateReadOnlyField;
[MemoryPackIgnore]
public int PublicProperty2 => PublicProperty + PublicField;

[MemoryPackInclude]
int privateField2;
[MemoryPackInclude]
int privateProperty2 { get; set; }
}

[MemoryPackable]
public partial class Sample2
{
public int PublicField;
public readonly int PublicReadOnlyField;
public int PublicProperty { get; set; }
public int PrivateSetPublicProperty { get; private set; }
public int ReadOnlyPublicProperty { get; }
public int InitProperty { get; init; }

public int[]? ArrayProp { get; set; }

// use [MemoryPackIgnore] to remove target of a public member
[MemoryPackIgnore]
public int PublicProperty2 => PublicProperty + PublicField;

// use [MemoryPackInclude] to promote a private member to serialization target
[MemoryPackInclude]
int privateField2;
[MemoryPackInclude]
Expand Down
2 changes: 1 addition & 1 deletion src/MemoryPack.Core/Internal/TypeHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ static Cache()
{
if (typeof(IFixedSizeMemoryPackable).IsAssignableFrom(type))
{
var prop = type.GetProperty("MemoryPack.IFixedSizeMemoryPackable.Size", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
var prop = type.GetProperty("global::MemoryPack.IFixedSizeMemoryPackable.Size", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
if (prop != null)
{
IsFixedSizeMemoryPackable = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ static Cache()
{
if (typeof(IFixedSizeMemoryPackable).IsAssignableFrom(type))
{
var prop = type.GetProperty("MemoryPack.IFixedSizeMemoryPackable.Size", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
var prop = type.GetProperty("global::MemoryPack.IFixedSizeMemoryPackable.Size", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
if (prop != null)
{
IsFixedSizeMemoryPackable = true;
Expand Down
32 changes: 32 additions & 0 deletions tests/MemoryPack.Tests/ReflectionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace MemoryPack.Tests;

public class ReflectionTest
{
[Fact]
public void InvokeExplicitInterface()
{
var type = typeof(ReflecCheck);

var m = type.GetMethod("MemoryPack.IMemoryPackFormatterRegister.RegisterFormatter", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
m.Should().NotBeNull();

#if NET7_0_OR_GREATER
var p = type.GetProperty("global::MemoryPack.IFixedSizeMemoryPackable.Size", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
p.Should().NotBeNull();
#endif
}
}

[MemoryPackable]
public partial class ReflecCheck
{
public int MyProperty1 { get; set; }
public int MyProperty2 { get; set; }
}

0 comments on commit 76a1381

Please sign in to comment.