コード例 #1
0
    public static void Run()
    {
        // These types are generated from SchemaFilesExample.fbs
        FooBarContainer container = new FooBarContainer
        {
            fruit       = Fruit.Pears,
            initialized = true,
            location    = "location",
            list        = new List <FooBar>
            {
                new FooBar
                {
                    name    = "name",
                    postfix = 1,
                    rating  = 3,
                    sibling = new Bar
                    {
                        ratio  = 3.14f,
                        size   = ushort.MaxValue,
                        time   = int.MinValue,
                        parent = new Foo {
                            count = 3, id = 4, length = 8, prefix = 10
                        },
                    }
                }
            },
        };

        // Serializer is pregenerated, so no first-run penalty for FlatBufferSerializer.
        byte[] destination = new byte[1024];

        int maxBytes     = FooBarContainer.Serializer.GetMaxSize(container);
        int bytesWritten = FooBarContainer.Serializer.Write(destination, container);
        var parsed       = FooBarContainer.Serializer.Parse(destination);
    }
コード例 #2
0
    public static void Run()
    {
        // These types are generated from SchemaFilesExample.fbs
        FooBarContainer container = new FooBarContainer
        {
            fruit       = Fruit.Pears,
            initialized = true,
            location    = "location",
            list        = new List <FooBar>
            {
                new FooBar
                {
                    name    = "name",
                    postfix = 1,
                    rating  = 3,
                    sibling = new Bar
                    {
                        ratio  = 3.14f,
                        size   = ushort.MaxValue,
                        time   = int.MinValue,
                        parent = new Foo(),     // structs are nullable types, but are not meant to be null.
                    }
                }
            },
        };

        byte[] destination = new byte[1024];

        // Generated types can still use a runtime-generated serializer
        int maxBytes     = FlatBufferSerializer.Default.GetMaxSize(container);
        int bytesWritten = FlatBufferSerializer.Default.Serialize(container, destination);
        var parsed       = FlatBufferSerializer.Default.Parse <FooBarContainer>(destination);
    }
コード例 #3
0
        private static void RunBenchmarkSet(
            string runName,
            ISerializer <FooBarContainer> serializer,
            FooBarContainer container)
        {
            int traversalCount = 5;

            byte[] destination = new byte[serializer.GetMaxSize(container)];

            SpanWriter  spanWriter  = new SpanWriter();
            InputBuffer inputBuffer = new ArrayInputBuffer(destination);

            serializer.Write(spanWriter, destination, container);

            (string, Action)[] items = new (string, Action)[]
コード例 #4
0
        public static void Run()
        {
            FooBarContainer container = new FooBarContainer
            {
                fruit       = Fruit.Pears,
                initialized = true,
                location    = "location",
                list        = new List <FooBar>
                {
                    new FooBar
                    {
                        name    = "name",
                        postfix = 1,
                        rating  = 3,
                        sibling = new Bar
                        {
                            ratio = 3.14f,
                            size  = ushort.MaxValue,
                            time  = int.MinValue,

                            // Nested structs are not intended to have null values,
                            // but it is possible due to FlatSharp modeling
                            // them as reference types. However, null structs
                            // do not cause a problem when serializing or parsing.
                            parent = null !,
                        }
                    }
                },
            };

            // Simple use case: make a deep copy of an object you're using.
            var copy = new FooBarContainer(container);

            Debug.Assert(!object.ReferenceEquals(copy.list, container.list), "A new list is created");
            for (int i = 0; i < container.list.Count; ++i)
            {
                var originalItem = container.list[i];

                Debug.Assert(copy.list is not null);
                var copyItem = copy.list[i];
                Debug.Assert(!object.ReferenceEquals(copyItem, originalItem));
            }

            // Now let's look at how this can be useful when operating on deserialized objects.
            var serializer = new FlatBufferSerializer(FlatBufferDeserializationOption.Lazy);

            byte[] data = new byte[1024];
            serializer.Serialize(container, data);
            var deserialized = serializer.Parse <FooBarContainer>(data);

            // Take a deserialized item and "upcast" it back to the original type.
            // This performs a full traversal of the object and allows the underlying buffer to be reused.
            Debug.Assert(deserialized.GetType() != container.GetType(), "The deserialized type is a subclass of the FooBarContainer type");
            copy = new FooBarContainer(deserialized);
            Debug.Assert(copy.GetType() == container.GetType(), "By using the copy constructor, we can get an instance of the original type.");

            // Next: Some deserialization modes, such as Lazy, don't permit mutation of the object.
            // Using the copy constructor can convert this to an object that we can mutate!
            try
            {
                // will throw
                deserialized.fruit = Fruit.Apples;
                Debug.Assert(false);
            }
            catch
            {
            }

            // Modifying the copy is just fine, though.
            copy.fruit = Fruit.Apples;
        }
コード例 #5
0
        public static void Main(string[] args)
        {
            Console.WriteLine($"x64={Environment.Is64BitProcess}");

            FooBar[] fooBars = new FooBar[5];
            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;
            }

            var defaultContainer = new FooBarContainer
            {
                fruit       = benchfb.Enum.Bananas,
                initialized = true,
                location    = "http://google.com/flatbuffers/",
                list        = fooBars,
            };

            var serializer = FooBarContainer.Serializer;

            RunBenchmarkSet(
                "GreedyBuildTime",
                serializer,
                defaultContainer);

            RunBenchmarkSet(
                "GreedyRuntime",
                new FlatBufferSerializer(new FlatBufferSerializerOptions(FlatBufferDeserializationOption.GreedyMutable)).Compile <FooBarContainer>(),
                defaultContainer);

            /*
             *
             * RunBenchmarkSet(
             *  "Lazy",
             *  new FlatBufferSerializer(new FlatBufferSerializerOptions(FlatBufferSerializerFlags.GenerateMutableObjects)).Compile<FooBarContainer>(),
             *  defaultContainer);
             *
             * RunBenchmarkSet(
             *  "Greedy",
             *  new FlatBufferSerializer(new FlatBufferSerializerOptions(FlatBufferSerializerFlags.GreedyDeserialize | FlatBufferSerializerFlags.GenerateMutableObjects)).Compile<FooBarContainer>(),
             *  defaultContainer);*/
        }