Exemplo n.º 1
0
    internal static void ValidateWriteThrough(
        bool writeThroughSupported,
        ITypeModel model,
        IReadOnlyDictionary <ITypeModel, List <TableFieldContext> > contexts,
        FlatBufferSerializerOptions options)
    {
        if (!contexts.TryGetValue(model, out var fieldsForModel))
        {
            return;
        }

        var firstWriteThrough = fieldsForModel.Where(x => x.WriteThrough).FirstOrDefault();

        if (firstWriteThrough is not null)
        {
            if (options.GreedyDeserialize)
            {
                throw new InvalidFlatBufferDefinitionException($"Field '{firstWriteThrough.FullName}' declares the WriteThrough option. WriteThrough is not supported when using Greedy deserialization.");
            }

            if (!writeThroughSupported)
            {
                throw new InvalidFlatBufferDefinitionException($"Field '{firstWriteThrough.FullName}' declares the WriteThrough option. WriteThrough is only supported for IList vectors.");
            }
        }
    }
Exemplo n.º 2
0
    private string GenerateSerializerForType(
        CompileContext context,
        FlatBufferDeserializationOption deserializationOption)
    {
        Type?type = context.PreviousAssembly?.GetType(this.FullName);

        FlatSharpInternal.Assert(type is not null, $"Flatsharp failed to find expected type '{this.FullName}' in assembly.");

        var options = new FlatBufferSerializerOptions(deserializationOption)
        {
            ConvertProtectedInternalToProtected = false
        };
        var generator = new RoslynSerializerGenerator(options, context.TypeModelContainer);

        MethodInfo method = generator.GetType()
                            .GetMethod(nameof(RoslynSerializerGenerator.GenerateCSharp), BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public) !
                            .MakeGenericMethod(type);

        try
        {
            string code = (string)method.Invoke(generator, new[] { "private" }) !;
            return(code);
        }
        catch (TargetInvocationException ex)
        {
            ExceptionDispatchInfo.Capture(ex.InnerException !).Throw();
            throw;
        }
    }
Exemplo n.º 3
0
        // TODO: consider moving to TableOrStructDefinition.
        private static string GenerateSerializerForType(Assembly assembly, TableOrStructDefinition tableOrStruct)
        {
            CSharpHelpers.ConvertProtectedInternalToProtected = false;
            try
            {
                Type type      = assembly.GetType(tableOrStruct.FullName);
                var  options   = new FlatBufferSerializerOptions(tableOrStruct.RequestedSerializer.Value);
                var  generator = new RoslynSerializerGenerator(options, TypeModelContainer.CreateDefault());

                var method = generator
                             .GetType()
                             .GetMethod(nameof(RoslynSerializerGenerator.GenerateCSharp), BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
                             .MakeGenericMethod(type);

                try
                {
                    string code = (string)method.Invoke(generator, new[] { "private" });
                    return(code);
                }
                catch (TargetInvocationException ex)
                {
                    ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
                    throw;
                }
            }
            finally
            {
                CSharpHelpers.ConvertProtectedInternalToProtected = true;
            }
        }
Exemplo n.º 4
0
        private void TestType <T>(bool listCache, Func <T> generator) where T : IEquatable <T>
        {
            var options = new FlatBufferSerializerOptions(listCache ? FlatBufferDeserializationOption.VectorCache : FlatBufferDeserializationOption.Lazy);
            FlatBufferSerializer serializer = new FlatBufferSerializer(options);

            {
                var memoryTable = new RootTable <IList <T> >
                {
                    Vector = Enumerable.Range(0, 10).Select(i => generator()).ToArray()
                };

                Span <byte> memory            = new byte[10240];
                int         offset            = serializer.Serialize(memoryTable, memory);
                var         memoryTableResult = serializer.Parse <RootTable <IList <T> > >(memory.Slice(0, offset).ToArray());

                var resultVector = memoryTableResult.Vector;
                for (int i = 0; i < memoryTableResult.Vector.Count; ++i)
                {
                    Assert.AreEqual <T>(memoryTable.Vector[i], resultVector[i]);

                    // reference equality should correspond to the serializer.
                    Assert.AreEqual(listCache, object.ReferenceEquals(resultVector[i], resultVector[i]));
                }
            }

            {
                var memoryTable = new RootTable <IReadOnlyList <T> >
                {
                    Vector = Enumerable.Range(0, 10).Select(i => generator()).ToArray()
                };

                Span <byte> memory            = new byte[10240];
                int         offset            = serializer.Serialize(memoryTable, memory);
                var         memoryTableResult = serializer.Parse <RootTable <IReadOnlyList <T> > >(memory.Slice(0, offset).ToArray());
                var         resultVector      = memoryTableResult.Vector;
                for (int i = 0; i < memoryTableResult.Vector.Count; ++i)
                {
                    Assert.AreEqual(memoryTable.Vector[i], resultVector[i]);

                    // reference equality should correspond to the serializer.
                    Assert.AreEqual(listCache, object.ReferenceEquals(resultVector[i], resultVector[i]));
                }
            }

            {
                var memoryTable = new RootTable <T[]>
                {
                    Vector = Enumerable.Range(0, 10).Select(i => generator()).ToArray()
                };

                Span <byte> memory            = new byte[10240];
                int         offset            = serializer.Serialize(memoryTable, memory);
                var         memoryTableResult = serializer.Parse <RootTable <T[]> >(memory.Slice(0, offset).ToArray());
                var         resultVector      = memoryTableResult.Vector;
                for (int i = 0; i < memoryTableResult.Vector.Length; ++i)
                {
                    Assert.AreEqual(memoryTable.Vector[i], resultVector[i]);
                }
            }
        }
Exemplo n.º 5
0
        public void TableParse_LazyMutable()
        {
            var options = new FlatBufferSerializerOptions(FlatBufferDeserializationOption.VectorCacheMutable);
            var table   = this.SerializeAndParse(options, out _);

            var newString = Guid.NewGuid().ToString();

            table.String = newString;
            Assert.AreEqual(newString, table.String);

            var newLong = DateTimeOffset.UtcNow.Ticks;

            table.Struct.Long = newLong;
            Assert.AreEqual(newLong, table.Struct.Long);

            var newStruct = new SimpleStruct();

            table.Struct = newStruct;
            Assert.AreEqual(newStruct, table.Struct);

            Assert.AreEqual(typeof(List <SimpleStruct>), table.StructVector.GetType());
            int count = table.StructVector.Count;

            table.StructVector.Add(new SimpleStruct());
            Assert.AreEqual(count + 1, table.StructVector.Count);
        }
Exemplo n.º 6
0
        private SimpleTable SerializeAndParse(FlatBufferSerializerOptions options, out WeakReference <byte[]> buffer)
        {
            SimpleTable table = new SimpleTable
            {
                String = "hi",
                Struct = new SimpleStruct {
                    Byte = 1, Long = 2, Uint = 3
                },
                StructVector = new List <SimpleStruct> {
                    new SimpleStruct {
                        Byte = 4, Long = 5, Uint = 6
                    }
                },
            };

            var serializer = new FlatBufferSerializer(options);

            var rawBuffer = new byte[1024];

            serializer.Serialize(table, rawBuffer);
            buffer = new WeakReference <byte[]>(rawBuffer);

            string csharp = serializer.Compile <SimpleTable>().CSharp;

            return(serializer.Parse <SimpleTable>(rawBuffer));
        }
Exemplo n.º 7
0
        public void MemoryVector_LazyDeserialize()
        {
            RootTable <Memory <byte> > root = new RootTable <Memory <byte> >()
            {
                Vector = new Memory <byte>(new byte[100])
            };

            root.Vector.Span.Fill(1);

            byte[] buffer     = new byte[1024];
            var    options    = new FlatBufferSerializerOptions(FlatBufferDeserializationOption.Lazy);
            var    serializer = new FlatBufferSerializer(options);

            serializer.Serialize(root, buffer.AsSpan());

            var parsed1 = serializer.Parse <RootTable <Memory <byte> > >(buffer);
            var parsed2 = serializer.Parse <RootTable <Memory <byte> > >(buffer);

            Assert.AreEqual((byte)1, parsed1.Vector.Span[0]);
            Assert.AreEqual((byte)1, parsed2.Vector.Span[0]);

            // Asser that this change affects both objects.
            parsed1.Vector.Span[0] = 2;

            Assert.AreEqual((byte)2, parsed1.Vector.Span[0]);
            Assert.AreEqual((byte)2, parsed2.Vector.Span[0]);
        }
Exemplo n.º 8
0
        public void TableParse_NotMutable()
        {
            var options = new FlatBufferSerializerOptions(FlatBufferDeserializationOption.Lazy);
            var table   = this.SerializeAndParse(options, out _);

            Assert.ThrowsException <NotMutableException>(() => table.String       = null);
            Assert.ThrowsException <NotMutableException>(() => table.Struct       = null);
            Assert.ThrowsException <NotMutableException>(() => table.StructVector = new List <SimpleStruct>());
            Assert.ThrowsException <NotMutableException>(() => table.StructVector.Add(null));
        }
    public void TableParse_Progressive()
    {
        var options = new FlatBufferSerializerOptions(FlatBufferDeserializationOption.Progressive);
        var table   = this.SerializeAndParse(options, out _);

        Assert.Throws <NotMutableException>(() => table.String      = string.Empty);
        Assert.Throws <NotMutableException>(() => table.Struct.Long = 0);
        Assert.Throws <NotMutableException>(() => table.Struct      = new());

        Assert.Equal(typeof(FlatBufferProgressiveVector <SimpleStruct, ArrayInputBuffer>), table.StructVector.GetType());
    }
Exemplo n.º 10
0
 protected override string CreateLoop(
     FlatBufferSerializerOptions options,
     string vectorVariableName,
     string numberofItemsVariableName,
     string expectedVariableName,
     string body) => CreateLoopStatic(
     this.ItemTypeModel,
     options,
     vectorVariableName,
     numberofItemsVariableName,
     expectedVariableName,
     body);
Exemplo n.º 11
0
 internal static string CreateLoopStatic(
     FlatBufferSerializerOptions options,
     string vectorVariableName,
     string expectedVariableName,
     string body)
 {
     return($@"
         for (int i = 0; i < {vectorVariableName}.Length; i = unchecked(i + 1))
         {{
             var {expectedVariableName} = {vectorVariableName}[i];
             {body}
         }}");
 }
Exemplo n.º 12
0
 protected override string CreateLoop(
     FlatBufferSerializerOptions options,
     string vectorVariableName,
     string numberofItemsVariableName,
     string expectedVariableName,
     string body)
 {
     return($@"
         foreach (var kvp in {vectorVariableName})
         {{
             var {expectedVariableName} = kvp.Value;
             {body}
         }}");
 }
Exemplo n.º 13
0
        // TODO: consider moving to TableOrStructDefinition.
        private static string GenerateSerializerForType(Assembly assembly, TableOrStructDefinition tableOrStruct)
        {
            Type type      = assembly.GetType(tableOrStruct.FullName);
            var  options   = new FlatBufferSerializerOptions(tableOrStruct.RequestedSerializer.Value);
            var  generator = new RoslynSerializerGenerator(options);

            var method = generator
                         .GetType()
                         .GetMethod(nameof(RoslynSerializerGenerator.GenerateCSharp), BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
                         .MakeGenericMethod(type);

            string code = (string)method.Invoke(generator, new[] { "private" });

            return(code);
        }
Exemplo n.º 14
0
        public void MarshalConfigTests(Type type, bool marshalEnable, bool marshalSerialize, bool marshalParse)
        {
            ITypeModel typeModel = RuntimeTypeModel.CreateFrom(type);
            var        tm        = Assert.IsType <ValueStructTypeModel>(typeModel);

            Assert.Equal(marshalSerialize, tm.CanMarshalOnSerialize);
            Assert.Equal(marshalParse, tm.CanMarshalOnParse);

            var options = new FlatBufferSerializerOptions {
                EnableValueStructMemoryMarshalDeserialization = marshalEnable
            };

            CodeGeneratedMethod serializeMethod = tm.CreateSerializeMethodBody(ContextHelpers.CreateSerializeContext(options));
            CodeGeneratedMethod parseMethod     = tm.CreateParseMethodBody(ContextHelpers.CreateParserContext(options));

            Assert.Equal(parseMethod.MethodBody.Contains("MemoryMarshal.Cast"), marshalEnable && marshalParse);
            Assert.Equal(serializeMethod.MethodBody.Contains("MemoryMarshal.Cast"), marshalEnable && marshalSerialize);
        }
Exemplo n.º 15
0
    internal static string CreateLoopStatic(
        ITypeModel typeModel,
        FlatBufferSerializerOptions options,
        string vectorVariableName,
        string numberofItemsVariableName,
        string expectedVariableName,
        string body)
    {
        string ListBody(string variable)
        {
            return($@"
                int i;
                for (i = 0; i < {numberofItemsVariableName}; i = unchecked(i + 1))
                {{
                    var {expectedVariableName} = {variable}[i];
                    {body}
                }}");
        }

        if (options.Devirtualize)
        {
            return($@"
                if ({vectorVariableName} is {typeModel.GetCompilableTypeName()}[] array)
                {{
                    {ArrayVectorTypeModel.CreateLoopStatic(options, "array", "current", body)}
                }}
                else if ({vectorVariableName} is List<{typeModel.GetCompilableTypeName()}> realList)
                {{
                    {ListBody("realList")}
                }}
                else
                {{
                    {ListBody(vectorVariableName)}
                }}");
        }
        else
        {
            return(ListBody(vectorVariableName));
        }
    }
Exemplo n.º 16
0
        public void StringVector_GreedyDeserialize_NotMutable()
        {
            RootTable <IList <string> > root = new RootTable <IList <string> >
            {
                Vector = new List <string> {
                    "one", "two", "three"
                }
            };

            var options = new FlatBufferSerializerOptions(FlatBufferDeserializationOption.Greedy);
            FlatBufferSerializer serializer = new FlatBufferSerializer(options);

            byte[] buffer = new byte[100];
            serializer.Serialize(root, buffer);

            var parsed = serializer.Parse <RootTable <IList <string> > >(buffer);

            Assert.AreEqual(typeof(ReadOnlyCollection <string>), parsed.Vector.GetType());
            Assert.IsTrue(parsed.Vector.IsReadOnly);

            Assert.ThrowsException <NotSupportedException>(() => parsed.Vector.Add("four"));
        }
Exemplo n.º 17
0
        public void StringVector_GreedyDeserialize_Mutable()
        {
            RootTable <IList <string> > root = new RootTable <IList <string> >
            {
                Vector = new List <string> {
                    "one", "two", "three"
                }
            };

            var options = new FlatBufferSerializerOptions(FlatBufferDeserializationOption.GreedyMutable);
            FlatBufferSerializer serializer = new FlatBufferSerializer(options);

            byte[] buffer = new byte[100];
            serializer.Serialize(root, buffer);

            var parsed = serializer.Parse <RootTable <IList <string> > >(buffer);

            Assert.AreEqual(typeof(List <string>), parsed.Vector.GetType());
            Assert.IsFalse(parsed.Vector.IsReadOnly);

            // Shouldn't throw.
            parsed.Vector.Add("four");
        }
Exemplo n.º 18
0
        public void TableParse_GreedyImmutable()
        {
            var options = new FlatBufferSerializerOptions(FlatBufferDeserializationOption.Greedy);
            var table   = this.SerializeAndParse(options, out var buffer);

            bool reaped = false;

            for (int i = 0; i < 5; ++i)
            {
                GC.Collect(2, GCCollectionMode.Forced);
                GCSettings.LatencyMode = GCLatencyMode.Batch;
                if (!buffer.TryGetTarget(out _))
                {
                    reaped = true;
                    break;
                }
            }

            Assert.IsTrue(reaped, "GC did not reclaim underlying byte buffer.");

            // The buffer has been collected. Now verify that we can read all the data as
            // we expect. This demonstrates that we've copied, as well as that we've
            // released references.
            Assert.IsNotNull(table.String);
            Assert.IsNotNull(table.Struct);
            Assert.IsNotNull(table.StructVector);

            Assert.AreEqual("hi", table.String);
            Assert.AreEqual(1, table.Struct.Byte);
            Assert.AreEqual(2, table.Struct.Long);
            Assert.AreEqual(3u, table.Struct.Uint);

            Assert.AreEqual(4, table.StructVector[0].Byte);
            Assert.AreEqual(5, table.StructVector[0].Long);
            Assert.AreEqual(6u, table.StructVector[0].Uint);
        }
Exemplo n.º 19
0
        public void GlobalSetup()
        {
            FooBar[] fooBars = new FooBar[this.VectorLength];
            for (int i = 0; i < fooBars.Length; i++)
            {
                var foo = new Foo
                {
                    Id     = 0xABADCAFEABADCAFE + (ulong)i,
                    Count  = (short)(10000 + i),
                    Prefix = (sbyte)('@' + i),
                    Length = (uint)(1000000 + i)
                };

                var bar = new Bar
                {
                    Parent = foo,
                    Ratio  = 3.14159f + i,
                    Size   = (ushort)(10000 + i),
                    Time   = 123456 + i
                };

                var fooBar = new FooBar
                {
                    Name    = Guid.NewGuid().ToString(),
                    PostFix = (byte)('!' + i),
                    Rating  = 3.1415432432445543543 + i,
                    Sibling = bar,
                };

                fooBars[i] = fooBar;
            }

            this.defaultContainer = new FooBarListContainer
            {
                Fruit       = 123,
                Initialized = true,
                Location    = "http://google.com/flatbuffers/",
                List        = fooBars,
            };

            {
                this.Google_FlatBuffers_Serialize();
                this.google_ByteBuffer = new FlatBuffers.ByteBuffer(this.google_flatBufferBuilder.SizedByteArray());
            }

            {
                var options = new FlatBufferSerializerOptions(this.DeserializeOption);

                this.fs_serializer = new FlatBufferSerializer(options);
                int offset = this.fs_serializer.Serialize(this.defaultContainer, this.fs_writeMemory);
                this.fs_readMemory = this.fs_writeMemory.AsSpan(0, offset).ToArray();
                this.inputBuffer   = new ArrayInputBuffer(this.fs_readMemory);
                this.FlatSharp_ParseAndTraverse();
            }

            {
                this.PBDN_Serialize();
                this.pbdn_writeBuffer.Position = 0;
                this.pbdn_writeBuffer.CopyTo(this.pbdn_readBuffer);
                this.PBDN_ParseAndTraverse();
            }
        }
Exemplo n.º 20
0
 /// <summary>
 /// Creates a loop that executes the given body.
 /// </summary>
 protected abstract string CreateLoop(
     FlatBufferSerializerOptions options,
     string vectorVariableName,
     string numberofItemsVariableName,
     string expectedVariableName,
     string body);
Exemplo n.º 21
0
        public virtual void GlobalSetup()
        {
            Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.Idle;

            FooBar[]                      fooBars                 = new FooBar[this.VectorLength];
            FooBarNonVirtual[]            fooBarsNV               = new FooBarNonVirtual[this.VectorLength];
            FooBar_ValueType[]            fooBarsValue            = new FooBar_ValueType[this.VectorLength];
            FooBar_ValueType_NonVirtual[] fooBarsValue_NonVirtual = new FooBar_ValueType_NonVirtual[this.VectorLength];

            for (int i = 0; i < fooBars.Length; i++)
            {
                var foo = new Foo
                {
                    Id     = 0xABADCAFEABADCAFE + (ulong)i,
                    Count  = (short)(10000 + i),
                    Prefix = (sbyte)('@' + i),
                    Length = (uint)(1000000 + i)
                };

                var fooNV = new FooNonVirtual
                {
                    Id     = 0xABADCAFEABADCAFE + (ulong)i,
                    Count  = (short)(10000 + i),
                    Prefix = (sbyte)('@' + i),
                    Length = (uint)(1000000 + i)
                };

                var fooValue = new Foo_ValueType
                {
                    Id     = 0xABADCAFEABADCAFE + (ulong)i,
                    Count  = (short)(10000 + i),
                    Prefix = (sbyte)('@' + i),
                    Length = (uint)(1000000 + i)
                };

                var bar = new Bar
                {
                    Parent = foo,
                    Ratio  = 3.14159f + i,
                    Size   = (ushort)(10000 + i),
                    Time   = 123456 + i
                };

                var barNV = new BarNonVirtual
                {
                    Parent = fooNV,
                    Ratio  = 3.14159f + i,
                    Size   = (ushort)(10000 + i),
                    Time   = 123456 + i
                };

                var barValue = new Bar_ValueType
                {
                    Parent = fooValue,
                    Ratio  = 3.14159f + i,
                    Size   = (ushort)(10000 + i),
                    Time   = 123456 + i
                };

                var fooBar = new FooBar
                {
                    Name    = Guid.NewGuid().ToString(),
                    PostFix = (byte)('!' + i),
                    Rating  = 3.1415432432445543543 + i,
                    Sibling = bar,
                };

                var fooBarNV = new FooBarNonVirtual
                {
                    Name    = Guid.NewGuid().ToString(),
                    PostFix = (byte)('!' + i),
                    Rating  = 3.1415432432445543543 + i,
                    Sibling = barNV,
                };

                var fooBarValue = new FooBar_ValueType
                {
                    Name    = Guid.NewGuid().ToString(),
                    PostFix = (byte)('!' + i),
                    Rating  = 3.1415432432445543543 + i,
                    Sibling = barValue,
                };

                var fooBarValueNV = new FooBar_ValueType_NonVirtual
                {
                    Name    = Guid.NewGuid().ToString(),
                    PostFix = (byte)('!' + i),
                    Rating  = 3.1415432432445543543 + i,
                    Sibling = barValue,
                };

                fooBars[i]                 = fooBar;
                fooBarsNV[i]               = fooBarNV;
                fooBarsValue[i]            = fooBarValue;
                fooBarsValue_NonVirtual[i] = fooBarValueNV;
            }

            this.defaultContainer = new FooBarListContainer
            {
                Fruit       = 123,
                Initialized = true,
                Location    = "http://google.com/flatbuffers/",
                List        = fooBars,
            };

            this.defaultContainerNonVirtual = new FooBarListContainerNonVirtual
            {
                Fruit       = 123,
                Initialized = true,
                Location    = "http://google.com/flatbuffers/",
                List        = fooBarsNV,
            };

            this.defaultContainerWithValueStructs = new FooBarListContainer_ValueType
            {
                Fruit       = 123,
                Initialized = true,
                Location    = "http://google.com/flatbuffers/",
                List        = fooBarsValue,
            };

            this.defaultContainerWithValueStructsNonVirtual = new FooBarListContainer_ValueType_NonVirtual
            {
                Fruit       = 123,
                Initialized = true,
                Location    = "http://google.com/flatbuffers/",
                List        = fooBarsValue_NonVirtual,
            };

            Random rng = new Random();

            this.sortedIntContainer = new SortedVectorTable <int> {
                Vector = new List <SortedVectorTableItem <int> >()
            };
            this.sortedStringContainer = new SortedVectorTable <string> {
                Vector = new List <SortedVectorTableItem <string> >()
            };
            this.unsortedIntContainer = new UnsortedVectorTable <int> {
                Vector = new List <UnsortedVectorTableItem <int> >()
            };
            this.unsortedStringContainer = new UnsortedVectorTable <string> {
                Vector = new List <UnsortedVectorTableItem <string> >()
            };

            for (int i = 0; i < this.VectorLength; ++i)
            {
                this.sortedIntContainer.Vector.Add(new SortedVectorTableItem <int> {
                    Key = rng.Next()
                });
                this.sortedStringContainer.Vector.Add(new SortedVectorTableItem <string> {
                    Key = Guid.NewGuid().ToString()
                });
                this.unsortedIntContainer.Vector.Add(new UnsortedVectorTableItem <int> {
                    Key = rng.Next()
                });
                this.unsortedStringContainer.Vector.Add(new UnsortedVectorTableItem <string> {
                    Key = Guid.NewGuid().ToString()
                });
            }

            this.valueTableVector = new ValueTableVector
            {
                ValueTables = Enumerable.Range(0, this.VectorLength).Select(x => new ValueTable
                {
                    A = 1,
                    B = 2,
                    C = 3,
                    D = 4,
                    E = 5,
                    F = 6,
                    G = 7,
                    H = 8,
                    I = 9,
                    J = 10,
                    K = true,
                }).ToArray()
            };

            {
                var options = new FlatBufferSerializerOptions(this.DeserializeOption);

                this.fs_serializer = new FlatBufferSerializer(options);

                int offset = this.fs_serializer.Serialize(this.defaultContainer, this.fs_writeMemory);
                this.fs_readMemory = this.fs_writeMemory.AsSpan(0, offset).ToArray();
                this.inputBuffer   = new InputBufferKind(this.fs_readMemory);
                this.FlatSharp_ParseAndTraverse();
            }

            int googleLength = 0;

            {
                this.google_ByteBuffer       = new FlatBuffers.ByteBuffer(this.fs_readMemory.ToArray());
                this.google_defaultContainer = Google.FooBarContainer.GetRootAsFooBarContainer(this.google_ByteBuffer).UnPack();
                googleLength = this.Google_FlatBuffers_Serialize_ObjectApi();
            }

            {
                this.PBDN_Serialize();
                this.pbdn_writeBuffer.Position = 0;
                this.pbdn_writeBuffer.CopyTo(this.pbdn_readBuffer);
                this.PBDN_ParseAndTraverse();
            }

            {
                this.MsgPack_Serialize_NonVirtual();
            }

            Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.RealTime;

            Console.WriteLine($"Sizes: MsgPack: {this.msgPackWriteData.Length}");
            Console.WriteLine($"Sizes: FlatSharp: {this.fs_readMemory.Length}");
            Console.WriteLine($"Sizes: Google: {googleLength}");
            Console.WriteLine($"Sizes: Pbdn: {this.pbdn_writeBuffer.Length}");
        }
Exemplo n.º 22
0
 protected override string CreateLoop(FlatBufferSerializerOptions options, string vectorVariableName, string numberOfItemsVariableName, string expectedVariableName, string body)
 {
     FlatSharpInternal.Assert(false, "Not expecting to do loop get max size for memory vector");
     throw new Exception();
 }
Exemplo n.º 23
0
        public virtual void GlobalSetup()
        {
            FooBar[] fooBars = new FooBar[this.VectorLength];
            for (int i = 0; i < fooBars.Length; i++)
            {
                var foo = new Foo
                {
                    Id     = 0xABADCAFEABADCAFE + (ulong)i,
                    Count  = (short)(10000 + i),
                    Prefix = (sbyte)('@' + i),
                    Length = (uint)(1000000 + i)
                };

                var bar = new Bar
                {
                    Parent = foo,
                    Ratio  = 3.14159f + i,
                    Size   = (ushort)(10000 + i),
                    Time   = 123456 + i
                };

                var fooBar = new FooBar
                {
                    Name    = Guid.NewGuid().ToString(),
                    PostFix = (byte)('!' + i),
                    Rating  = 3.1415432432445543543 + i,
                    Sibling = bar,
                };

                fooBars[i] = fooBar;
            }

            this.defaultContainer = new FooBarListContainer
            {
                Fruit       = 123,
                Initialized = true,
                Location    = "http://google.com/flatbuffers/",
                List        = fooBars,
            };

            Random rng = new Random();

            this.sortedIntContainer = new SortedVectorTable <int> {
                Vector = new List <SortedVectorTableItem <int> >()
            };
            this.sortedStringContainer = new SortedVectorTable <string> {
                Vector = new List <SortedVectorTableItem <string> >()
            };
            this.unsortedIntContainer = new UnsortedVectorTable <int> {
                Vector = new List <UnsortedVectorTableItem <int> >()
            };
            this.unsortedStringContainer = new UnsortedVectorTable <string> {
                Vector = new List <UnsortedVectorTableItem <string> >()
            };

            for (int i = 0; i < this.VectorLength; ++i)
            {
                this.sortedIntContainer.Vector.Add(new SortedVectorTableItem <int> {
                    Key = rng.Next()
                });
                this.sortedStringContainer.Vector.Add(new SortedVectorTableItem <string> {
                    Key = Guid.NewGuid().ToString()
                });
                this.unsortedIntContainer.Vector.Add(new UnsortedVectorTableItem <int> {
                    Key = rng.Next()
                });
                this.unsortedStringContainer.Vector.Add(new UnsortedVectorTableItem <string> {
                    Key = Guid.NewGuid().ToString()
                });
            }

            {
                var options = new FlatBufferSerializerOptions(this.DeserializeOption);

                this.fs_serializer = new FlatBufferSerializer(options);

                int offset = this.fs_serializer.Serialize(this.defaultContainer, this.fs_writeMemory);
                this.fs_readMemory = this.fs_writeMemory.AsSpan(0, offset).ToArray();
                this.inputBuffer   = new ArrayInputBuffer(this.fs_readMemory);
                this.FlatSharp_ParseAndTraverse();
            }

            {
                this.google_ByteBuffer       = new FlatBuffers.ByteBuffer(this.fs_readMemory.ToArray());
                this.google_defaultContainer = Google.FooBarContainer.GetRootAsFooBarContainer(this.google_ByteBuffer).UnPack();
            }

            {
                this.PBDN_Serialize();
                this.pbdn_writeBuffer.Position = 0;
                this.pbdn_writeBuffer.CopyTo(this.pbdn_readBuffer);
                this.PBDN_ParseAndTraverse();
            }
        }