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

Remove a couple LINQ usages in Microsoft.Extensions #47873

Merged
2 commits merged into from
Feb 5, 2021

Conversation

eerhardt
Copy link
Member

@eerhardt eerhardt commented Feb 4, 2021

Note that this was the only usage of Enumerable.Cast in a Blazor WASM default app. So this allows for that method to be trimmed. It is also faster and allocates less according to this benchmark:

    [MemoryDiagnoser]
    public class JoinBenchmark
    {
        private const string NullValue = "(null)";
        private static IEnumerable s_Enumerable = Enumerable.Range(1, 10);

        [Benchmark]
        public string OldWay()
        {
            return string.Join(", ", s_Enumerable.Cast<object>().Select(o => o ?? NullValue));
        }

        [Benchmark]
        public string NewWay()
        {
            var vsb = new ValueStringBuilder(stackalloc char[256]);
            bool first = true;
            foreach (object e in s_Enumerable)
            {
                if (!first)
                {
                    vsb.Append(", ");
                }

                vsb.Append(e != null ? e.ToString() : NullValue);
                first = false;
            }
            return vsb.ToString();
        }
    }
Method Mean Error StdDev Gen 0 Gen 1 Gen 2 Allocated
OldWay 468.1 ns 3.29 ns 2.92 ns 0.0798 - - 504 B
NewWay 222.2 ns 2.72 ns 2.27 ns 0.0621 - - 392 B

@ghost
Copy link

ghost commented Feb 4, 2021

Tagging subscribers to this area: @maryamariyan
See info in area-owners.md if you want to be subscribed.

Issue Details

Note that this was the only usage of Enumerable.Cast in a Blazor WASM default app. So this allows for that method to be trimmed. It is also faster and allocates less according to this benchmark:

    [MemoryDiagnoser]
    public class JoinBenchmark
    {
        private const string NullValue = "(null)";
        private static IEnumerable s_Enumerable = Enumerable.Range(1, 10);

        [Benchmark]
        public string OldWay()
        {
            return string.Join(", ", s_Enumerable.Cast<object>().Select(o => o ?? NullValue));
        }

        [Benchmark]
        public string NewWay()
        {
            var vsb = new ValueStringBuilder(stackalloc char[256]);
            bool first = true;
            foreach (object e in s_Enumerable)
            {
                if (!first)
                {
                    vsb.Append(", ");
                }

                vsb.Append(e != null ? e.ToString() : NullValue);
                first = false;
            }
            return vsb.ToString();
        }
    }
Method Mean Error StdDev Gen 0 Gen 1 Gen 2 Allocated
OldWay 468.1 ns 3.29 ns 2.92 ns 0.0798 - - 504 B
NewWay 222.2 ns 2.72 ns 2.27 ns 0.0621 - - 392 B
Author: eerhardt
Assignees: -
Labels:

area-Extensions-Configuration

Milestone: -

Copy link
Member

@maryamariyan maryamariyan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, Aside from the error:

src/libraries/Microsoft.Extensions.Configuration.Xml/src/XmlStreamConfigurationProvider.cs(76,88): error CS1061: (NETCORE_ENGINEERING_TELEMETRY=Build) 'Stack<string>' does not contain a definition for 'Reverse' and no accessible extension method 'Reverse' accepting a first argument of type 'Stack<string>' could be found (are you missing a using directive or an assembly reference?)

@ghost
Copy link

ghost commented Feb 5, 2021

Hello @eerhardt!

Because this pull request has the auto-merge label, I will be glad to assist with helping to merge this pull request once all check-in policies pass.

p.s. you can customize the way I help with merging this pull request, such as holding this pull request until a specific person approves. Simply @mention me (@msftbot) and give me an instruction to get started! Learn more here.

@ghost ghost merged commit df2b4f2 into dotnet:master Feb 5, 2021
@eerhardt eerhardt deleted the RemoveLinqLoggingValues branch February 5, 2021 20:08
@ghost ghost locked as resolved and limited conversation to collaborators Mar 7, 2021
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants