From a0d2466057d559f06ad5ab4b1a22648fbde2ecad Mon Sep 17 00:00:00 2001 From: James Courtney Date: Sun, 10 Nov 2024 14:07:45 -0800 Subject: [PATCH] Cleanup --- src/FlatSharp.Runtime/IO/ISpanWriter.cs | 88 --------- ...latBufferSerializationTargetExtensions.cs} | 175 +++++++++++++++--- .../IFlatBufferSerializationTarget.cs | 138 -------------- src/FlatSharp.Runtime/IO/SpanWriter.cs | 119 ------------ .../ISerializerExtensions.cs | 24 +++ src/FlatSharp/TypeModel/StringTypeModel.cs | 4 +- src/FlatSharp/TypeModel/TableTypeModel.cs | 2 +- src/FlatSharp/TypeModel/UnionTypeModel.cs | 2 +- .../TypeModel/Vectors/BaseVectorTypeModel.cs | 2 +- .../Vectors/MemoryVectorTypeModel.cs | 2 +- .../BaseVectorOfUnionTypeModel.cs | 4 +- .../FlatSharpEndToEndTests.csproj | 2 +- .../Oracle/AlignmentTests.cs | 4 +- .../Oracle/OracleSerializeTests.cs | 54 +++--- 14 files changed, 208 insertions(+), 412 deletions(-) delete mode 100644 src/FlatSharp.Runtime/IO/ISpanWriter.cs rename src/FlatSharp.Runtime/IO/{SpanWriterExtensions.cs => SerializationTarget/FlatBufferSerializationTargetExtensions.cs} (53%) delete mode 100644 src/FlatSharp.Runtime/IO/SpanWriter.cs diff --git a/src/FlatSharp.Runtime/IO/ISpanWriter.cs b/src/FlatSharp.Runtime/IO/ISpanWriter.cs deleted file mode 100644 index 6bc398e2..00000000 --- a/src/FlatSharp.Runtime/IO/ISpanWriter.cs +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright 2020 James Courtney - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -using System.Text; - -namespace FlatSharp; - -/// -/// Defines a span writer. -/// -public interface ISpanWriter -{ - /// - /// Writes the given byte to the span at the given offset. - /// - void WriteByte(Span span, byte value, int offset); - - /// - /// Writes the given double to the span at the given offset. - /// - void WriteDouble(Span span, double value, int offset); - - /// - /// Writes the given float to the span at the given offset. - /// - void WriteFloat(Span span, float value, int offset); - - /// - /// Writes the given int to the span at the given offset. - /// - void WriteInt(Span span, int value, int offset); - - /// - /// Writes the given long to the span at the given offset. - /// - void WriteLong(Span span, long value, int offset); - - /// - /// Writes the given sbyte to the span at the given offset. - /// - void WriteSByte(Span span, sbyte value, int offset); - - /// - /// Writes the given short to the span at the given offset. - /// - void WriteShort(Span span, short value, int offset); - - /// - /// Writes the given uint to the span at the given offset. - /// - void WriteUInt(Span span, uint value, int offset); - - /// - /// Writes the given ulong to the span at the given offset. - /// - void WriteULong(Span span, ulong value, int offset); - - /// - /// Writes the given ushort to the span at the given offset. - /// - void WriteUShort(Span span, ushort value, int offset); - - /// - /// Writes the bytes of the given string to the destination span according to the given encoding. - /// - int GetStringBytes(Span destination, string value, Encoding encoding); - - /// - /// Invokes the method. - /// - void FlushSharedStrings( - ISharedStringWriter writer, - Span destination, - SerializationContext context); -} diff --git a/src/FlatSharp.Runtime/IO/SpanWriterExtensions.cs b/src/FlatSharp.Runtime/IO/SerializationTarget/FlatBufferSerializationTargetExtensions.cs similarity index 53% rename from src/FlatSharp.Runtime/IO/SpanWriterExtensions.cs rename to src/FlatSharp.Runtime/IO/SerializationTarget/FlatBufferSerializationTargetExtensions.cs index a0b83b1f..0a77ba97 100644 --- a/src/FlatSharp.Runtime/IO/SpanWriterExtensions.cs +++ b/src/FlatSharp.Runtime/IO/SerializationTarget/FlatBufferSerializationTargetExtensions.cs @@ -1,29 +1,153 @@ -/* - * Copyright 2024 James Courtney - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - using System.Buffers; +using System.Buffers.Binary; using System.Runtime.InteropServices; -namespace FlatSharp.Internal; +namespace FlatSharp; /// -/// Extension methods that apply to all implementations. +/// Extensions for IFlatBufferSerializationTarget /// -public static class SpanWriterExtensions +public static class FlatBufferSerializationTargetExtensions { + public static void WriteBool(this T target, long offset, bool value) + where T : IFlatBufferSerializationTarget +#if NET9_0_OR_GREATER + , allows ref struct +#endif + { + target.WriteUInt8(offset, value ? SerializationHelpers.True : SerializationHelpers.False); + } + + public static void WriteUInt8(this T target, long offset, byte value) + where T : IFlatBufferSerializationTarget +#if NET9_0_OR_GREATER + , allows ref struct +#endif + { + target[offset] = value; + } + + public static void WriteInt8(this T target, long offset, sbyte value) + where T : IFlatBufferSerializationTarget +#if NET9_0_OR_GREATER + , allows ref struct +#endif + { + target[offset] = unchecked((byte)value); + } + + public static void WriteUInt16(this T target, long offset, ushort value) + where T : IFlatBufferSerializationTarget +#if NET9_0_OR_GREATER + , allows ref struct +#endif + { + CheckAlignment(offset, sizeof(ushort)); + BinaryPrimitives.WriteUInt16LittleEndian( + target.AsSpan(offset, sizeof(ushort)), + value); + } + + public static void WriteInt16(this T target, long offset, short value) + where T : IFlatBufferSerializationTarget +#if NET9_0_OR_GREATER + , allows ref struct +#endif + { + CheckAlignment(offset, sizeof(short)); + BinaryPrimitives.WriteInt16LittleEndian( + target.AsSpan(offset, sizeof(short)), + value); + } + + public static void WriteUInt32(this T target, long offset, uint value) + where T : IFlatBufferSerializationTarget +#if NET9_0_OR_GREATER + , allows ref struct +#endif + { + CheckAlignment(offset, sizeof(uint)); + BinaryPrimitives.WriteUInt32LittleEndian( + target.AsSpan(offset, sizeof(uint)), + value); + } + + public static void WriteInt32(this T target, long offset, int value) + where T : IFlatBufferSerializationTarget +#if NET9_0_OR_GREATER + , allows ref struct +#endif + { + CheckAlignment(offset, sizeof(int)); + BinaryPrimitives.WriteInt32LittleEndian( + target.AsSpan(offset, sizeof(int)), + value); + } + + public static void WriteUInt64(this T target, long offset, ulong value) + where T : IFlatBufferSerializationTarget +#if NET9_0_OR_GREATER + , allows ref struct +#endif + { + CheckAlignment(offset, sizeof(ulong)); + BinaryPrimitives.WriteUInt64LittleEndian( + target.AsSpan(offset, sizeof(ulong)), + value); + } + + public static void WriteInt64(this T target, long offset, long value) + where T : IFlatBufferSerializationTarget +#if NET9_0_OR_GREATER + , allows ref struct +#endif + { + CheckAlignment(offset, sizeof(long)); + BinaryPrimitives.WriteInt64LittleEndian( + target.AsSpan(offset, sizeof(long)), + value); + } + + + public static void WriteFloat32(this T target, long offset, float value) + where T : IFlatBufferSerializationTarget +#if NET9_0_OR_GREATER + , allows ref struct +#endif + { + CheckAlignment(offset, sizeof(float)); + +#if NETSTANDARD + ScalarSpanReader.FloatLayout floatLayout = new ScalarSpanReader.FloatLayout + { + value = value + }; + + target.WriteUInt32(offset, floatLayout.bytes); +#else + BinaryPrimitives.WriteSingleLittleEndian( + target.AsSpan(offset, sizeof(float)), + value); +#endif + } + + public static void WriteFloat64(this T target, long offset, double value) + where T : IFlatBufferSerializationTarget +#if NET9_0_OR_GREATER + , allows ref struct +#endif + { + CheckAlignment(offset, sizeof(double)); + +#if NETSTANDARD + target.WriteInt64(offset, BitConverter.DoubleToInt64Bits(value)); +#else + BinaryPrimitives.WriteDoubleLittleEndian( + target.AsSpan(offset, sizeof(double)), + value); +#endif + } + public static void WriteReadOnlyByteMemoryBlock( this TTarget target, ReadOnlyMemory memory, @@ -50,11 +174,11 @@ public static void UnsafeWriteSpan( long offset, int alignment, SerializationContext ctx) + where TElement : unmanaged where TSerializationTarget : IFlatBufferSerializationTarget #if NET9_0_OR_GREATER , allows ref struct #endif - where TElement : unmanaged { // Since we are copying bytes here, only LE is supported. FlatSharpInternal.AssertLittleEndian(); @@ -154,16 +278,9 @@ public static void WriteUOffset( } } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void WriteBool(this TSpanWriter spanWriter, Span span, bool b, int offset) - where TSpanWriter : ISpanWriter - { - spanWriter.WriteByte(span, b ? SerializationHelpers.True : SerializationHelpers.False, offset); - } - [ExcludeFromCodeCoverage] [Conditional("DEBUG")] - public static void CheckAlignment(this TSpanWriter spanWriter, long offset, int size) where TSpanWriter : ISpanWriter + private static void CheckAlignment(long offset, int size) { #if DEBUG if (offset % size != 0) @@ -172,4 +289,4 @@ public static void CheckAlignment(this TSpanWriter spanWriter, long } #endif } -} +} \ No newline at end of file diff --git a/src/FlatSharp.Runtime/IO/SerializationTarget/IFlatBufferSerializationTarget.cs b/src/FlatSharp.Runtime/IO/SerializationTarget/IFlatBufferSerializationTarget.cs index 1a7e1acd..6cc1df32 100644 --- a/src/FlatSharp.Runtime/IO/SerializationTarget/IFlatBufferSerializationTarget.cs +++ b/src/FlatSharp.Runtime/IO/SerializationTarget/IFlatBufferSerializationTarget.cs @@ -14,8 +14,6 @@ * limitations under the License. */ -using System.Buffers.Binary; - namespace FlatSharp; /// @@ -51,140 +49,4 @@ public interface IFlatBufferSerializationTarget /// Returns a Span{byte} over the given range. /// Span AsSpan(long start, int length); -} - -/// -/// Extensions for IFlatBufferSerializationTarget -/// -public static class FlatBufferSerializationTargetExtensions -{ - public static void WriteBool(this T target, long offset, bool value) - where T : IFlatBufferSerializationTarget -#if NET9_0_OR_GREATER - , allows ref struct -#endif - { - target.WriteUInt8(offset, value ? SerializationHelpers.True : SerializationHelpers.False); - } - - public static void WriteUInt8(this T target, long offset, byte value) - where T : IFlatBufferSerializationTarget - #if NET9_0_OR_GREATER - , allows ref struct - #endif - { - target[offset] = value; - } - - public static void WriteInt8(this T target, long offset, sbyte value) - where T : IFlatBufferSerializationTarget -#if NET9_0_OR_GREATER - , allows ref struct -#endif - { - target[offset] = unchecked((byte)value); - } - - public static void WriteUInt16(this T target, long offset, ushort value) - where T : IFlatBufferSerializationTarget -#if NET9_0_OR_GREATER - , allows ref struct -#endif - { - BinaryPrimitives.WriteUInt16LittleEndian( - target.AsSpan(offset, sizeof(ushort)), - value); - } - - public static void WriteInt16(this T target, long offset, short value) - where T : IFlatBufferSerializationTarget -#if NET9_0_OR_GREATER - , allows ref struct -#endif - { - BinaryPrimitives.WriteInt16LittleEndian( - target.AsSpan(offset, sizeof(short)), - value); - } - - public static void WriteUInt32(this T target, long offset, uint value) - where T : IFlatBufferSerializationTarget -#if NET9_0_OR_GREATER - , allows ref struct -#endif - { - BinaryPrimitives.WriteUInt32LittleEndian( - target.AsSpan(offset, sizeof(uint)), - value); - } - - public static void WriteInt32(this T target, long offset, int value) - where T : IFlatBufferSerializationTarget -#if NET9_0_OR_GREATER - , allows ref struct -#endif - { - BinaryPrimitives.WriteInt32LittleEndian( - target.AsSpan(offset, sizeof(int)), - value); - } - - public static void WriteUInt64(this T target, long offset, ulong value) - where T : IFlatBufferSerializationTarget -#if NET9_0_OR_GREATER - , allows ref struct -#endif - { - BinaryPrimitives.WriteUInt64LittleEndian( - target.AsSpan(offset, sizeof(ulong)), - value); - } - - public static void WriteInt64(this T target, long offset, long value) - where T : IFlatBufferSerializationTarget -#if NET9_0_OR_GREATER - , allows ref struct -#endif - { - BinaryPrimitives.WriteInt64LittleEndian( - target.AsSpan(offset, sizeof(long)), - value); - } - - - public static void WriteFloat32(this T target, long offset, float value) - where T : IFlatBufferSerializationTarget -#if NET9_0_OR_GREATER - , allows ref struct -#endif - { - -#if NETSTANDARD - ScalarSpanReader.FloatLayout floatLayout = new ScalarSpanReader.FloatLayout - { - value = value - }; - - target.WriteUInt32(offset, floatLayout.bytes); -#else - BinaryPrimitives.WriteSingleLittleEndian( - target.AsSpan(offset, sizeof(float)), - value); -#endif - } - - public static void WriteFloat64(this T target, long offset, double value) - where T : IFlatBufferSerializationTarget -#if NET9_0_OR_GREATER - , allows ref struct -#endif - { -#if NETSTANDARD - target.WriteInt64(offset, BitConverter.DoubleToInt64Bits(value)); -#else - BinaryPrimitives.WriteDoubleLittleEndian( - target.AsSpan(offset, sizeof(double)), - value); -#endif - } } \ No newline at end of file diff --git a/src/FlatSharp.Runtime/IO/SpanWriter.cs b/src/FlatSharp.Runtime/IO/SpanWriter.cs deleted file mode 100644 index 16bb6dc1..00000000 --- a/src/FlatSharp.Runtime/IO/SpanWriter.cs +++ /dev/null @@ -1,119 +0,0 @@ -/* - * Copyright 2020 James Courtney - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -using System.Buffers; -using System.Buffers.Binary; -using System.Text; - -namespace FlatSharp; - -/// -/// Utility class for writing items to spans. -/// -public struct SpanWriter : ISpanWriter -{ - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void WriteByte(Span span, byte value, int offset) - { - span[offset] = value; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void WriteSByte(Span span, sbyte value, int offset) - { - span[offset] = (byte)value; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void WriteUShort(Span span, ushort value, int offset) - { - this.CheckAlignment(offset, sizeof(ushort)); - BinaryPrimitives.WriteUInt16LittleEndian(span.Slice(offset), value); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void WriteShort(Span span, short value, int offset) - { - this.CheckAlignment(offset, sizeof(short)); - BinaryPrimitives.WriteInt16LittleEndian(span.Slice(offset), value); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void WriteUInt(Span span, uint value, int offset) - { - this.CheckAlignment(offset, sizeof(uint)); - BinaryPrimitives.WriteUInt32LittleEndian(span.Slice(offset), value); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void WriteInt(Span span, int value, int offset) - { - this.CheckAlignment(offset, sizeof(int)); - BinaryPrimitives.WriteInt32LittleEndian(span.Slice(offset), value); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void WriteULong(Span span, ulong value, int offset) - { - this.CheckAlignment(offset, sizeof(ulong)); - BinaryPrimitives.WriteUInt64LittleEndian(span.Slice(offset), value); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void WriteLong(Span span, long value, int offset) - { - this.CheckAlignment(offset, sizeof(long)); - BinaryPrimitives.WriteInt64LittleEndian(span.Slice(offset), value); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void WriteFloat(Span span, float value, int offset) - { - ScalarSpanReader.FloatLayout floatLayout = new ScalarSpanReader.FloatLayout - { - value = value - }; - - this.WriteUInt(span, floatLayout.bytes, offset); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void WriteDouble(Span span, double value, int offset) - { - this.WriteLong(span, BitConverter.DoubleToInt64Bits(value), offset); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public int GetStringBytes(Span destination, string value, Encoding encoding) - { -#if NETSTANDARD2_0 - int length = value.Length; - byte[] buffer = ArrayPool.Shared.Rent(encoding.GetMaxByteCount(length)); - int bytesWritten = encoding.GetBytes(value, 0, length, buffer, 0); - buffer.AsSpan().Slice(0, bytesWritten).CopyTo(destination); - ArrayPool.Shared.Return(buffer); -#else - int bytesWritten = encoding.GetBytes(value, destination); -#endif - - return bytesWritten; - } - - public void FlushSharedStrings(ISharedStringWriter writer, Span destination, SerializationContext context) - { - throw new NotSupportedException(); - } -} diff --git a/src/FlatSharp.Runtime/ISerializerExtensions.cs b/src/FlatSharp.Runtime/ISerializerExtensions.cs index 9ca97d3e..5b7f80dc 100644 --- a/src/FlatSharp.Runtime/ISerializerExtensions.cs +++ b/src/FlatSharp.Runtime/ISerializerExtensions.cs @@ -118,7 +118,11 @@ public static object Parse(this ISerializer serializer, ReadOnlyMemory dat [ExcludeFromCodeCoverage] // Just a helper public static long Write(this ISerializer serializer, byte[] buffer, T item) where T : class { +#if NET9_0_OR_GREATER + return serializer.Write(buffer.AsSpan(), item); +#else return serializer.Write(new ArraySerializationTarget(buffer), item); +#endif } /// @@ -128,7 +132,11 @@ public static long Write(this ISerializer serializer, byte[] buffer, T ite [ExcludeFromCodeCoverage] // Just a helper public static long Write(this ISerializer serializer, byte[] buffer, object item) { +#if NET9_0_OR_GREATER + return serializer.Write(buffer.AsSpan(), item); +#else return serializer.Write(new ArraySerializationTarget(buffer), item); +#endif } /// @@ -138,6 +146,9 @@ public static long Write(this ISerializer serializer, byte[] buffer, object item [ExcludeFromCodeCoverage] // Just a helper public static long Write(this ISerializer serializer, ArraySegment buffer, T item) where T : class { +#if NET9_0_OR_GREATER + return serializer.Write(buffer.AsSpan(), item); +#else byte[]? array = buffer.Array; if (array is null) { @@ -147,6 +158,7 @@ public static long Write(this ISerializer serializer, ArraySegment b return serializer.Write( new ArraySerializationTarget(array, buffer.Offset, buffer.Count), item); +#endif } /// @@ -156,6 +168,9 @@ public static long Write(this ISerializer serializer, ArraySegment b [ExcludeFromCodeCoverage] // Just a helper public static long Write(this ISerializer serializer, ArraySegment buffer, object item) { +#if NET9_0_OR_GREATER + return serializer.Write(buffer.AsSpan(), item); +#else byte[]? array = buffer.Array; if (array is null) { @@ -165,6 +180,7 @@ public static long Write(this ISerializer serializer, ArraySegment buffer, return serializer.Write( new ArraySerializationTarget(array, buffer.Offset, buffer.Count), item); +#endif } /// @@ -174,7 +190,11 @@ public static long Write(this ISerializer serializer, ArraySegment buffer, [ExcludeFromCodeCoverage] // Just a helper public static long Write(this ISerializer serializer, Memory buffer, T item) where T : class { +#if NET9_0_OR_GREATER + return serializer.Write(buffer.Span, item); +#else return serializer.Write(new MemorySerializationTarget(buffer), item); +#endif } /// @@ -184,7 +204,11 @@ public static long Write(this ISerializer serializer, Memory buffer, [ExcludeFromCodeCoverage] // Just a helper public static long Write(this ISerializer serializer, Memory buffer, object item) { +#if NET9_0_OR_GREATER + return serializer.Write(buffer.Span, item); +#else return serializer.Write(new MemorySerializationTarget(buffer), item); +#endif } #if NET9_0_OR_GREATER diff --git a/src/FlatSharp/TypeModel/StringTypeModel.cs b/src/FlatSharp/TypeModel/StringTypeModel.cs index a3a282dd..7a85891e 100644 --- a/src/FlatSharp/TypeModel/StringTypeModel.cs +++ b/src/FlatSharp/TypeModel/StringTypeModel.cs @@ -122,7 +122,7 @@ public override CodeGeneratedMethod CreateSerializeMethodBody(SerializationCodeG }} }} - {context.TargetVariableName}.{nameof(SpanWriterExtensions.WriteString)}( + {context.TargetVariableName}.WriteString( {context.ValueVariableName}, {context.OffsetVariableName}, {context.SerializationContextVariableName}); @@ -132,7 +132,7 @@ public override CodeGeneratedMethod CreateSerializeMethodBody(SerializationCodeG { // otherwise, we can omit that code entirely. body = $@" - {context.TargetVariableName}.{nameof(SpanWriterExtensions.WriteString)}( + {context.TargetVariableName}.WriteString( {context.ValueVariableName}, {context.OffsetVariableName}, {context.SerializationContextVariableName}); diff --git a/src/FlatSharp/TypeModel/TableTypeModel.cs b/src/FlatSharp/TypeModel/TableTypeModel.cs index 09a9a889..9a630940 100644 --- a/src/FlatSharp/TypeModel/TableTypeModel.cs +++ b/src/FlatSharp/TypeModel/TableTypeModel.cs @@ -473,7 +473,7 @@ public override CodeGeneratedMethod CreateSerializeMethodBody(SerializationCodeG string methodStart = $@" long tableStart = {context.SerializationContextVariableName}.{nameof(SerializationContext.AllocateSpace)}({maxInlineSize}, sizeof(int)); - {context.TargetVariableName}.{nameof(SpanWriterExtensions.WriteUOffset)}({context.OffsetVariableName}, tableStart); + {context.TargetVariableName}.WriteUOffset({context.OffsetVariableName}, tableStart); long currentOffset = tableStart + sizeof(int); // skip past vtable soffset_t. int vtableLength = {minVtableLength}; diff --git a/src/FlatSharp/TypeModel/UnionTypeModel.cs b/src/FlatSharp/TypeModel/UnionTypeModel.cs index c5699eaf..7102e457 100644 --- a/src/FlatSharp/TypeModel/UnionTypeModel.cs +++ b/src/FlatSharp/TypeModel/UnionTypeModel.cs @@ -182,7 +182,7 @@ public override CodeGeneratedMethod CreateSerializeMethodBody(SerializationCodeG // a pointer to a struct. inlineAdjustment =$@" var writeOffset = context.{nameof(SerializationContext.AllocateSpace)}({elementModel.PhysicalLayout.Single().InlineSize}, {elementModel.PhysicalLayout.Single().Alignment}); - {context.TargetVariableName}.{nameof(SpanWriterExtensions.WriteUOffset)}({context.OffsetVariableName}.offset1, writeOffset);"; + {context.TargetVariableName}.WriteUOffset({context.OffsetVariableName}.offset1, writeOffset);"; } else { diff --git a/src/FlatSharp/TypeModel/Vectors/BaseVectorTypeModel.cs b/src/FlatSharp/TypeModel/Vectors/BaseVectorTypeModel.cs index a2cf0a30..8548307d 100644 --- a/src/FlatSharp/TypeModel/Vectors/BaseVectorTypeModel.cs +++ b/src/FlatSharp/TypeModel/Vectors/BaseVectorTypeModel.cs @@ -147,7 +147,7 @@ public override CodeGeneratedMethod CreateSerializeMethodBody(SerializationCodeG string body = $@" int count = {context.ValueVariableName}.{this.LengthPropertyName}; long vectorOffset = {context.SerializationContextVariableName}.{nameof(SerializationContext.AllocateVector)}({itemTypeModel.PhysicalLayout[0].Alignment}, count, {this.PaddedMemberInlineSize}); - {context.TargetVariableName}.{nameof(SpanWriterExtensions.WriteUOffset)}({context.OffsetVariableName}, vectorOffset); + {context.TargetVariableName}.WriteUOffset({context.OffsetVariableName}, vectorOffset); {context.TargetVariableName}.WriteInt32(vectorOffset, count); vectorOffset += sizeof(int); diff --git a/src/FlatSharp/TypeModel/Vectors/MemoryVectorTypeModel.cs b/src/FlatSharp/TypeModel/Vectors/MemoryVectorTypeModel.cs index 41c304f2..5e28c248 100644 --- a/src/FlatSharp/TypeModel/Vectors/MemoryVectorTypeModel.cs +++ b/src/FlatSharp/TypeModel/Vectors/MemoryVectorTypeModel.cs @@ -78,7 +78,7 @@ public override CodeGeneratedMethod CreateParseMethodBody(ParserCodeGenContext c public override CodeGeneratedMethod CreateSerializeMethodBody(SerializationCodeGenContext context) { string body = - $"{context.TargetVariableName}.{nameof(SpanWriterExtensions.WriteReadOnlyByteMemoryBlock)}({context.ValueVariableName}, {context.OffsetVariableName}, {context.SerializationContextVariableName});"; + $"{context.TargetVariableName}.WriteReadOnlyByteMemoryBlock({context.ValueVariableName}, {context.OffsetVariableName}, {context.SerializationContextVariableName});"; return new CodeGeneratedMethod(body); } diff --git a/src/FlatSharp/TypeModel/VectorsOfUnion/BaseVectorOfUnionTypeModel.cs b/src/FlatSharp/TypeModel/VectorsOfUnion/BaseVectorOfUnionTypeModel.cs index 0cfb80bb..e5f3e1fe 100644 --- a/src/FlatSharp/TypeModel/VectorsOfUnion/BaseVectorOfUnionTypeModel.cs +++ b/src/FlatSharp/TypeModel/VectorsOfUnion/BaseVectorOfUnionTypeModel.cs @@ -123,12 +123,12 @@ public override CodeGeneratedMethod CreateSerializeMethodBody(SerializationCodeG string body = $@" int count = {context.ValueVariableName}.{this.LengthPropertyName}; long discriminatorVectorOffset = {context.SerializationContextVariableName}.{nameof(SerializationContext.AllocateVector)}(sizeof(byte), count, sizeof(byte)); - {context.TargetVariableName}.{nameof(SpanWriterExtensions.WriteUOffset)}({context.OffsetVariableName}.offset0, discriminatorVectorOffset); + {context.TargetVariableName}.WriteUOffset({context.OffsetVariableName}.offset0, discriminatorVectorOffset); {context.TargetVariableName}.WriteInt32(discriminatorVectorOffset, count); discriminatorVectorOffset += sizeof(int); long offsetVectorOffset = {context.SerializationContextVariableName}.{nameof(SerializationContext.AllocateVector)}(sizeof(int), count, sizeof(int)); - {context.TargetVariableName}.{nameof(SpanWriterExtensions.WriteUOffset)}({context.OffsetVariableName}.offset1, offsetVectorOffset); + {context.TargetVariableName}.WriteUOffset({context.OffsetVariableName}.offset1, offsetVectorOffset); {context.TargetVariableName}.WriteInt32(offsetVectorOffset, count); offsetVectorOffset += sizeof(int); diff --git a/src/Tests/FlatSharpEndToEndTests/FlatSharpEndToEndTests.csproj b/src/Tests/FlatSharpEndToEndTests/FlatSharpEndToEndTests.csproj index 876746ca..1aedb35b 100644 --- a/src/Tests/FlatSharpEndToEndTests/FlatSharpEndToEndTests.csproj +++ b/src/Tests/FlatSharpEndToEndTests/FlatSharpEndToEndTests.csproj @@ -1,7 +1,7 @@  true - net9.0 + net9.0;net8.0 net472;net6.0;net7.0;net8.0;net9.0 net6.0;net7.0;net8.0;net9.0 false diff --git a/src/Tests/FlatSharpEndToEndTests/Oracle/AlignmentTests.cs b/src/Tests/FlatSharpEndToEndTests/Oracle/AlignmentTests.cs index 3fc4e4dd..13f30ddb 100644 --- a/src/Tests/FlatSharpEndToEndTests/Oracle/AlignmentTests.cs +++ b/src/Tests/FlatSharpEndToEndTests/Oracle/AlignmentTests.cs @@ -74,10 +74,10 @@ public void NestedStructWithOddAlignment_Serialize() } }; - Span memory = new byte[10240]; + byte[] memory = new byte[10240]; long offset = FS.AlignmentTestOuterHoder.Serializer.Write(memory, holder); - var parsed = Flatc.AlignmentTestOuterHoder.GetRootAsAlignmentTestOuterHoder(new ByteBuffer(memory.Slice(0, (int)offset).ToArray())); + var parsed = Flatc.AlignmentTestOuterHoder.GetRootAsAlignmentTestOuterHoder(new ByteBuffer(memory.AsSpan().Slice(0, (int)offset).ToArray())); var outer = parsed.Value.Value; Assert.AreEqual(1, outer.A); diff --git a/src/Tests/FlatSharpEndToEndTests/Oracle/OracleSerializeTests.cs b/src/Tests/FlatSharpEndToEndTests/Oracle/OracleSerializeTests.cs index b5aea4be..5d69ec57 100644 --- a/src/Tests/FlatSharpEndToEndTests/Oracle/OracleSerializeTests.cs +++ b/src/Tests/FlatSharpEndToEndTests/Oracle/OracleSerializeTests.cs @@ -45,10 +45,10 @@ public void SimpleTypes_WithValues() UShort = GetRandom(), }; - Span memory = new byte[10240]; + byte[] memory = new byte[10240]; long size = FS.BasicTypes.Serializer.Write(memory, simple); - var bb = new ByteBuffer(memory.Slice(0, (int)size).ToArray()); + var bb = new ByteBuffer(memory.AsSpan().Slice(0, (int)size).ToArray()); Assert.IsTrue(Flatc.BasicTypesVerify.Verify(new(bb), 4)); var oracle = Flatc.BasicTypes.GetRootAsBasicTypes(bb); @@ -74,10 +74,10 @@ public void SimpleTypes_Defaults() { var simple = new FS.BasicTypes(); - Span memory = new byte[10240]; + byte[] memory = new byte[10240]; long size = FS.BasicTypes.Serializer.Write(memory, simple); - var bb = new ByteBuffer(memory.Slice(0, (int)size).ToArray()); + var bb = new ByteBuffer(memory.AsSpan().Slice(0, (int)size).ToArray()); Assert.IsTrue(Flatc.BasicTypesVerify.Verify(new(bb), 4)); var oracle = Flatc.BasicTypes.GetRootAsBasicTypes(bb); @@ -111,10 +111,10 @@ public void LinkedList() } }; - Span memory = new byte[10240]; + byte[] memory = new byte[10240]; long size = FS.LinkedListNode.Serializer.Write(memory, simple); - var bb = new ByteBuffer(memory.Slice(0, (int)size).ToArray()); + var bb = new ByteBuffer(memory.AsSpan().Slice(0, (int)size).ToArray()); Assert.IsTrue(Flatc.LinkedListNodeVerify.Verify(new(bb), 4)); var oracle = Flatc.LinkedListNode.GetRootAsLinkedListNode(bb); @@ -138,10 +138,10 @@ public void StructWithinTable() }, }; - Span memory = new byte[10240]; + byte[] memory = new byte[10240]; long size = FS.LocationHolder.Serializer.Write(memory, holder); - var bb = new ByteBuffer(memory.Slice(0, (int)size).ToArray()); + var bb = new ByteBuffer(memory.AsSpan().Slice(0, (int)size).ToArray()); Assert.IsTrue(Flatc.LocationHolderVerify.Verify(new(bb), 4)); var oracle = Flatc.LocationHolder.GetRootAsLocationHolder(bb); @@ -176,10 +176,10 @@ public void ScalarVectors() ByteVector3 = new byte[0], }; - Span memory = new byte[10240]; + byte[] memory = new byte[10240]; long size = FS.Vectors.Serializer.Write(memory, table); - var bb = new ByteBuffer(memory.Slice(0, (int)size).ToArray()); + var bb = new ByteBuffer(memory.AsSpan().Slice(0, (int)size).ToArray()); Assert.IsTrue(Flatc.VectorsVerify.Verify(new(bb), 4)); var oracle = Flatc.Vectors.GetRootAsVectors(bb); @@ -223,11 +223,11 @@ public void FiveByteStructVector() } }; - Span buffer = new byte[1024]; + byte[] buffer = new byte[1024]; var bytecount = FS.FiveByteStructTable.Serializer.Write(buffer, table); - buffer = buffer.Slice(0, (int)bytecount); + var span = buffer.AsSpan().Slice(0, (int)bytecount); - var bb = new ByteBuffer(buffer.ToArray()); + var bb = new ByteBuffer(span.ToArray()); Assert.IsTrue(Flatc.FiveByteStructTableVerify.Verify(new Verifier(bb), 4)); var oracle = Flatc.FiveByteStructTable.GetRootAsFiveByteStructTable(bb); @@ -267,10 +267,10 @@ public void Union_Table_BasicTypes() Value = new FS.Union(simple) }; - Span memory = new byte[10240]; + byte[] memory = new byte[10240]; long size = FS.UnionTable.Serializer.Write(memory, table); - var bb = new ByteBuffer(memory.Slice(0, (int)size).ToArray()); + var bb = new ByteBuffer(memory.AsSpan().Slice(0, (int)size).ToArray()); Assert.IsTrue(Flatc.UnionTableVerify.Verify(new(bb), 4)); var oracle = Flatc.UnionTable.GetRootAsUnionTable(bb); @@ -303,10 +303,10 @@ public void Union_String() Value = new("foobar") }; - Span memory = new byte[10240]; + byte[] memory = new byte[10240]; long size = FS.UnionTable.Serializer.Write(memory, table); - var bb = new ByteBuffer(memory.Slice(0, (int)size).ToArray()); + var bb = new ByteBuffer(memory.AsSpan().Slice(0, (int)size).ToArray()); Assert.IsTrue(Flatc.UnionTableVerify.Verify(new(bb), 4)); var oracle = Flatc.UnionTable.GetRootAsUnionTable(bb); @@ -322,10 +322,10 @@ public void Union_NotSet() Value = null, }; - Span memory = new byte[10240]; + byte[] memory = new byte[10240]; long size = FS.UnionTable.Serializer.Write(memory, table); - var bb = new ByteBuffer(memory.Slice(0, (int)size).ToArray()); + var bb = new ByteBuffer(memory.AsSpan().Slice(0, (int)size).ToArray()); Assert.IsTrue(Flatc.UnionTableVerify.Verify(new(bb), 4)); var oracle = Flatc.UnionTable.GetRootAsUnionTable(bb); @@ -347,10 +347,10 @@ public void Union_Struct_Location() Value = new(location) }; - Span memory = new byte[10240]; + byte[] memory = new byte[10240]; long size = FS.UnionTable.Serializer.Write(memory, table); - var bb = new ByteBuffer(memory.Slice(0, (int)size).ToArray()); + var bb = new ByteBuffer(memory.AsSpan().Slice(0, (int)size).ToArray()); Assert.IsTrue(Flatc.UnionTableVerify.Verify(new(bb), 4)); var oracle = Flatc.UnionTable.GetRootAsUnionTable(bb); @@ -374,10 +374,10 @@ public void NestedStruct_InnerStructSet() } }; - Span memory = new byte[10240]; + byte[] memory = new byte[10240]; long size = FS.NestedStructs.Serializer.Write(memory, nested); - var bb = new ByteBuffer(memory.Slice(0, (int)size).ToArray()); + var bb = new ByteBuffer(memory.AsSpan().Slice(0, (int)size).ToArray()); Assert.IsTrue(Flatc.NestedStructsVerify.Verify(new(bb), 4)); var oracle = Flatc.NestedStructs.GetRootAsNestedStructs(bb); @@ -400,10 +400,10 @@ public void NestedStruct_InnerStructNotSet() } }; - Span memory = new byte[10240]; + byte[] memory = new byte[10240]; long size = FS.NestedStructs.Serializer.Write(memory, nested); - var bb = new ByteBuffer(memory.Slice(0, (int)size).ToArray()); + var bb = new ByteBuffer(memory.AsSpan().Slice(0, (int)size).ToArray()); Assert.IsTrue(Flatc.NestedStructsVerify.Verify(new(bb), 4)); var oracle = Flatc.NestedStructs.GetRootAsNestedStructs(bb); @@ -427,10 +427,10 @@ public void VectorOfUnion() } }; - Span memory = new byte[10240]; + byte[] memory = new byte[10240]; long size = FS.VectorOfUnionTable.Serializer.Write(memory, table); - var bb = new ByteBuffer(memory.Slice(0, (int)size).ToArray()); + var bb = new ByteBuffer(memory.AsSpan().Slice(0, (int)size).ToArray()); Assert.IsTrue(Flatc.UnionVectorTableVerify.Verify(new(bb), 4)); var oracle = Flatc.UnionVectorTable.GetRootAsUnionVectorTable(bb);