Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance decrease when called from anonymous function #347

Open
v-kuropatka opened this issue Mar 10, 2021 · 0 comments
Open

Performance decrease when called from anonymous function #347

v-kuropatka opened this issue Mar 10, 2021 · 0 comments

Comments

@v-kuropatka
Copy link

I noticed some strange behavior when JSON.Serialize is called via anonymous function. Consider following benchmark:

    [MemoryDiagnoser]
    public class SerializersBenchmark
    {
        private Action<object> jilSerializer = o => JSON.Serialize(o, Options.ISO8601);
        private static readonly SomePerson Person = new SomePerson
        {
            FirstName =  "John",
            LastName = "Doe"
        };

        [Benchmark]
        public void JilSerializer()
        {
            JSON.Serialize(Person, Options.ISO8601);
        }

        [Benchmark]
        public void JilSerializerThroughAction()
        {
            jilSerializer(Person);
        }

        public class SomePerson
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
        }
    }

Here are results:

Method Mean Error StdDev Gen 0 Gen 1 Gen 2 Allocated
JilSerializer 260.8 ns 1.61 ns 1.43 ns 0.0525 - - 440 B
JilSerializerThroughAction 447.5 ns 2.87 ns 2.69 ns 0.0677 - - 568 B

You can see significant performance reduction and increase of memory allocation. Could you please advise how to avoid it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant