Skip to content

Commit 9f263cd

Browse files
zhangskzThadHouse
andauthored
Use generic Marshal.SizeOf when possible (#21964) (#24673)
85507b9 introduced a new call to Marshal.Sizeof(Type). This call is not AOT compatible, which is a regression as prior versions were AOT compatible, even if not directly supported. This conditionally uses the generic version of Marshal.SizeOf, which is AOT compatible to fix the issue. Closes #21824 Closes #21964 COPYBARA_INTEGRATE_REVIEW=#21964 from ThadHouse:genericsizeof a12294e PiperOrigin-RevId: 818694739 Co-authored-by: Thad House <[email protected]>
1 parent 06d6548 commit 9f263cd

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

csharp/src/Google.Protobuf/Collections/RepeatedField.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,13 @@ private unsafe bool TryGetArrayAsSpanPinnedUnsafe(FieldCodec<T> codec, out Span<
711711
// 1. protobuf wire bytes is LittleEndian only
712712
// 2. validate that size of csharp element T is matching the size of protobuf wire size
713713
// NOTE: cannot use bool with this span because csharp marshal it as 4 bytes
714-
if (BitConverter.IsLittleEndian && (codec.FixedSize > 0 && Marshal.SizeOf(typeof(T)) == codec.FixedSize))
714+
if (BitConverter.IsLittleEndian && (codec.FixedSize > 0 &&
715+
#if GOOGLE_PROTOBUF_SUPPORT_GENERIC_SIZEOF
716+
Marshal.SizeOf<T>()
717+
#else
718+
Marshal.SizeOf(typeof(T))
719+
#endif
720+
== codec.FixedSize))
715721
{
716722
handle = GCHandle.Alloc(array, GCHandleType.Pinned);
717723
span = new Span<byte>(handle.AddrOfPinnedObject().ToPointer(), array.Length * codec.FixedSize);
@@ -772,4 +778,4 @@ public RepeatedFieldDebugView(RepeatedField<T> list)
772778
public T[] Items => list.ToArray();
773779
}
774780
}
775-
}
781+
}

csharp/src/Google.Protobuf/Google.Protobuf.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@
2525
<IsTrimmable>true</IsTrimmable>
2626
<!-- Disable warnings about AOT and trimming features on older targets, e.g. netstandard2.0.
2727
For now, continue to apply these features to these targets because some packages don't have a target that supports them -->
28-
<NoWarn>$(NoWarn);NETSDK1195;NETSDK1210</NoWarn>
28+
<NoWarn>$(NoWarn);NETSDK1195;NETSDK1210</NoWarn>
2929
</PropertyGroup>
3030

3131
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
32-
<DefineConstants>$(DefineConstants);GOOGLE_PROTOBUF_SUPPORT_FAST_STRING</DefineConstants>
32+
<DefineConstants>$(DefineConstants);GOOGLE_PROTOBUF_SUPPORT_FAST_STRING;GOOGLE_PROTOBUF_SUPPORT_GENERIC_SIZEOF</DefineConstants>
3333
</PropertyGroup>
3434

3535
<PropertyGroup Condition=" '$(TargetFramework)' == 'net50' ">
36-
<DefineConstants>$(DefineConstants);GOOGLE_PROTOBUF_SUPPORT_FAST_STRING;GOOGLE_PROTOBUF_SIMD</DefineConstants>
36+
<DefineConstants>$(DefineConstants);GOOGLE_PROTOBUF_SUPPORT_FAST_STRING;GOOGLE_PROTOBUF_SUPPORT_GENERIC_SIZEOF;GOOGLE_PROTOBUF_SIMD</DefineConstants>
3737
</PropertyGroup>
3838

3939
<ItemGroup>

0 commit comments

Comments
 (0)