Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Avoid array cloning in Path.Get*Chars - #11338

Merged
stephentoub merged 1 commit into
dotnet:masterfrom
stephentoub:path_array_clone
Sep 1, 2016
Merged

Avoid array cloning in Path.Get*Chars#11338
stephentoub merged 1 commit into
dotnet:masterfrom
stephentoub:path_array_clone

Conversation

@stephentoub

Copy link
Copy Markdown
Member

It's faster to just allocate a new array.
Per comment at #11293 (comment)
cc: @jkotas

@jkotas

jkotas commented Sep 1, 2016

Copy link
Copy Markdown
Member

👍

It's faster to just allocate a new array.
@stephentoub

Copy link
Copy Markdown
Member Author

Test Innerloop Windows_NT Debug Build and Test please (known AV in coreclr)

return (char[])PathInternal.InvalidPathChars.Clone();
}

public static char[] GetInvalidFileNameChars()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Did you mean to remove this?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes. It's now in the other partial files.

@drieseng

drieseng commented Sep 1, 2016

Copy link
Copy Markdown
Contributor

I'm waiting for someone from MS to treath this like it's a community contribution, and ask you to show the numbers ;-)

@stephentoub

stephentoub commented Sep 1, 2016

Copy link
Copy Markdown
Member Author

I'm waiting for someone from MS to treath this like its a community contribution, and ask you to show the numbers ;-)

Code:

using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;

class Program
{
    static void Main()
    {
        const int Iters = 10000000;
        var sw = new Stopwatch();
        while (true)
        {
            sw.Restart();
            for (int i = 0; i < Iters; i++) GetClone();
            sw.Stop();
            Console.WriteLine($"Clone: {sw.Elapsed.TotalSeconds}");

            sw.Restart();
            for (int i = 0; i < Iters; i++) GetNew();
            sw.Stop();
            Console.WriteLine($"New  : {sw.Elapsed.TotalSeconds}");

            Console.WriteLine();
        }
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    static char[] GetClone() => (char[])Array.Clone();

    [MethodImpl(MethodImplOptions.NoInlining)]
    static char[] GetNew() => new char[]
    {
        '\"', '<', '>', '|', '\0',
        (char)1, (char)2, (char)3, (char)4, (char)5, (char)6, (char)7, (char)8, (char)9, (char)10,
        (char)11, (char)12, (char)13, (char)14, (char)15, (char)16, (char)17, (char)18, (char)19, (char)20,
        (char)21, (char)22, (char)23, (char)24, (char)25, (char)26, (char)27, (char)28, (char)29, (char)30,
        (char)31
    };

    static readonly char[] Array = GetNew();
}

64-bit Results:

Clone: 0.8212021
New  : 0.1450638

Clone: 0.7894494
New  : 0.1448052

Clone: 0.8033437
New  : 0.1470242

Clone: 0.8033267
New  : 0.1465283

32-bit Results:

Clone: 1.0977963
New  : 0.1940705

Clone: 1.0942017
New  : 0.1991546

Clone: 1.0645428
New  : 0.1985613

@stephentoub
stephentoub merged commit 0588d18 into dotnet:master Sep 1, 2016
@stephentoub
stephentoub deleted the path_array_clone branch September 1, 2016 17:50
@karelz karelz modified the milestone: 1.1.0 Dec 3, 2016
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
Avoid array cloning in Path.Get*Chars

Commit migrated from dotnet/corefx@0588d18
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants