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

Use string.Create in BitConverter.ToString(byte[]) #15218

Merged
merged 1 commit into from
Nov 26, 2017

Conversation

justinvp
Copy link

Avoids unnecessary allocations and copying, and reduces the amount of unsafe code.

Benchmark:

using System;
using System.Diagnostics;
using System.Text;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Attributes.Jobs;
using BenchmarkDotNet.Running;

[MemoryDiagnoser]
[InProcess]
public class Program
{
    public static void Main() => BenchmarkRunner.Run<Program>();

    const string SmallData = "Lorem ipsum dolor sit amet";
    const string LargeData =
        "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do " +
        "eiusmod tempor incididunt ut labore et dolore magna aliqua. " +
        "Ipsum suspendisse ultrices gravida dictum fusce.";
    public static readonly byte[] s_smallData = Encoding.UTF8.GetBytes(SmallData);
    public static readonly byte[] s_largeData = Encoding.UTF8.GetBytes(LargeData);

    [Benchmark]
    public string BeforeSmall() => OldConverter.ToString(s_smallData);

    [Benchmark]
    public string AfterSmall() => NewConverter.ToString(s_smallData);

    [Benchmark]
    public string BeforeLarge() => OldConverter.ToString(s_largeData);

    [Benchmark]
    public string AfterLarge() => NewConverter.ToString(s_largeData);
}

Results:

BenchmarkDotNet=v0.10.10, OS=Windows 10 Redstone 2 [1703, Creators Update] (10.0.15063.726)
Processor=Intel Core i7-6700K CPU 4.00GHz (Skylake), ProcessorCount=2
Frequency=3914037 Hz, Resolution=255.4907 ns, Timer=TSC
.NET Core SDK=2.2.0-preview1-007558
  [Host] : .NET Core 2.1.0-preview1-25915-01 (Framework 4.6.25915.02), 64bit RyuJIT

Job=InProcess  Toolchain=InProcessToolchain

      Method |      Mean |     Error |    StdDev |  Gen 0 | Allocated |
------------ |----------:|----------:|----------:|-------:|----------:|
 BeforeSmall |  80.10 ns | 0.4675 ns | 0.4373 ns | 0.0437 |     184 B |
  AfterSmall |  77.63 ns | 0.2555 ns | 0.1995 ns | 0.0437 |     184 B |
 BeforeLarge | 464.79 ns | 1.6609 ns | 1.3870 ns | 0.5031 |    2112 B |
  AfterLarge | 457.05 ns | 2.2694 ns | 2.1228 ns | 0.2513 |    1056 B |

Avoids unnecessary allocations and copying, and reduces the amount of unsafe code.
Copy link
Member

@stephentoub stephentoub left a comment

Choose a reason for hiding this comment

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

Nice. Thanks.

@stephentoub stephentoub merged commit dd74fca into dotnet:master Nov 26, 2017
dotnet-bot pushed a commit to dotnet/corert that referenced this pull request Nov 26, 2017
)

Avoids unnecessary allocations and copying, and reduces the amount of unsafe code.

Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
jkotas pushed a commit to dotnet/corert that referenced this pull request Nov 26, 2017
)

Avoids unnecessary allocations and copying, and reduces the amount of unsafe code.

Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
@justinvp justinvp deleted the bitconverter branch November 26, 2017 04:17
jkotas added a commit to dotnet/corert that referenced this pull request Nov 26, 2017
dotnet-bot pushed a commit to dotnet/corefx that referenced this pull request Jan 13, 2018
)

Avoids unnecessary allocations and copying, and reduces the amount of unsafe code.

Signed-off-by: dotnet-bot-corefx-mirror <dotnet-bot@microsoft.com>
dotnet-bot pushed a commit to dotnet/corefx that referenced this pull request Jan 13, 2018
)

Avoids unnecessary allocations and copying, and reduces the amount of unsafe code.

Signed-off-by: dotnet-bot-corefx-mirror <dotnet-bot@microsoft.com>
safern pushed a commit to dotnet/corefx that referenced this pull request Jan 16, 2018
)

Avoids unnecessary allocations and copying, and reduces the amount of unsafe code.

Signed-off-by: dotnet-bot-corefx-mirror <dotnet-bot@microsoft.com>
safern pushed a commit to dotnet/corefx that referenced this pull request Jan 16, 2018
)

Avoids unnecessary allocations and copying, and reduces the amount of unsafe code.

Signed-off-by: dotnet-bot-corefx-mirror <dotnet-bot@microsoft.com>
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.

2 participants