Skip to content

Conversation

@stephentoub
Copy link
Member

@stephentoub stephentoub commented Jul 2, 2021

Fixes #55029

Method Toolchain Mean Ratio Allocated
Encode \main\corerun.exe 107,747,075.7 ns 1.00 3,686 B
Encode \pr\corerun.exe 2,269,515.9 ns 0.02 210 B
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Reports;
using BenchmarkDotNet.Running;
using Perfolizer.Horology;
using System;
using System.Globalization;
using System.IO;
using System.Security.Cryptography;

[MemoryDiagnoser]
public class Program
{
    public static void Main(string[] args) =>
        BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args,
            DefaultConfig.Instance.WithSummaryStyle(new SummaryStyle(CultureInfo.InvariantCulture,
                printUnitsInHeader: false, sizeUnit: SizeUnit.B, timeUnit: TimeUnit.Nanosecond, printZeroValuesInContent: true)));

    private byte[] _data = RandomNumberGenerator.GetBytes(10_000_000);
    private MemoryStream _destination = new MemoryStream();

    [Benchmark]
    public void Encode()
    {
        _destination.Position = 0;
        using (var toBase64 = new ToBase64Transform())
        using (var stream = new CryptoStream(_destination, toBase64, CryptoStreamMode.Write, leaveOpen: true))
        {
            Span<byte> remaining = _data;
            while (!remaining.IsEmpty)
            {
                Span<byte> toWrite = remaining.Slice(0, Math.Min(4096, remaining.Length));
                stream.Write(toWrite);
                remaining = remaining.Slice(toWrite.Length);
            }
        }
    }
}

@stephentoub stephentoub added this to the 6.0.0 milestone Jul 2, 2021
@ghost
Copy link

ghost commented Jul 2, 2021

Tagging subscribers to this area: @bartonjs, @vcsjones, @krwq, @GrabYourPitchforks
See info in area-owners.md if you want to be subscribed.

Issue Details

Fixes #55029

Method Toolchain Mean Ratio Allocated
Encode \main\corerun.exe 107,747,075.7 ns 1.00 3,686 B
Encode \pr\corerun.exe 2,269,515.9 ns 0.02 210 B
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Running;
using System;
using BenchmarkDotNet.Diagnosers;
using Perfolizer.Horology;
using BenchmarkDotNet.Columns;
using System.Globalization;
using BenchmarkDotNet.Reports;
using System.Security.Cryptography;
using System.IO;

[MemoryDiagnoser]
public class Program
{
    public static void Main(string[] args) =>
        BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args,
            DefaultConfig.Instance.WithSummaryStyle(new SummaryStyle(CultureInfo.InvariantCulture,
                printUnitsInHeader: false, sizeUnit: SizeUnit.B, timeUnit: TimeUnit.Nanosecond, printZeroValuesInContent: true)));

    private byte[] _data = RandomNumberGenerator.GetBytes(10_000_000);
    private MemoryStream _destination = new MemoryStream();

    [Benchmark]
    public void Encode()
    {
        _destination.Position = 0;
        using (var toBase64 = new ToBase64Transform())
        using (var stream = new CryptoStream(_destination, toBase64, CryptoStreamMode.Write, leaveOpen: true))
        {
            Span<byte> remaining = _data;
            while (!remaining.IsEmpty)
            {
                Span<byte> toWrite = remaining.Slice(0, Math.Min(4096, remaining.Length));
                stream.Write(toWrite);
                remaining = remaining.Slice(toWrite.Length);
            }
        }
    }
}
Author: stephentoub
Assignees: -
Labels:

area-System.Security

Milestone: 6.0.0

@stephentoub
Copy link
Member Author

I'm realizing now I misunderstood the contract, and the implementation needs to be much more complex, to handle cases where more input is provided than can fit into the output space provided... I was only encoding what would fit, but it appears the contract actually requires the remaining input data to be saved off for later.

@stephentoub stephentoub closed this Jul 2, 2021
@stephentoub
Copy link
Member Author

Turns out the implementation doesn't need to be more complex as it can make simplifying assumptions about the inputs, ala what UniversalCryptoTransform does. And since this never previously supported anything other than one input block that entirely fit into the output, this won't be a breaking change. I'll fix up the input validation accordingly.

@stephentoub stephentoub reopened this Jul 2, 2021
@stephentoub stephentoub merged commit ae5ee8f into dotnet:main Jul 6, 2021
@stephentoub stephentoub deleted the cantransformtrue branch July 6, 2021 20:42
@ghost ghost locked as resolved and limited conversation to collaborators Aug 5, 2021
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.

ToBase64Transform wholly inadequate to live in the BCL

4 participants