Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

MihaZupan
Copy link
Member

@MihaZupan MihaZupan commented May 27, 2024

We're already using things like Sse2.PackUnsignedSaturate instead of Vector128.Narrow when we don't care about the edge case values (we've already validated the input).
This PR does something similar for Vector256 and Vector512 paths.

public class AsciiBenchmarks
{
    [Params(1000)]
    public int Length;

    private byte[] _bytes;
    private char[] _chars;

    [GlobalSetup]
    public void Setup()
    {
        _chars = new string('a', Length).ToCharArray();
        _bytes = Encoding.ASCII.GetBytes(_chars);
    }

    [Benchmark]
    public OperationStatus FromUtf16() => Ascii.FromUtf16(_chars, _bytes, out _);

    [Benchmark]
    public OperationStatus FromHexString() => Convert.FromHexString(_chars, _bytes, out _, out _);
}
Method Toolchain Length Mean Error Ratio
FromUtf16 main 1000 45.37 ns 0.057 ns 1.00
FromUtf16 pr 1000 32.70 ns 0.163 ns 0.72
FromHexString main 1000 144.48 ns 0.097 ns 1.00
FromHexString pr 1000 102.58 ns 0.079 ns 0.71

(this is with Avx2)

Diffs: https://gist.github.com/MihuBot/f60c7254060f436ae9c12ded701c85f8

Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-text-encoding
See info in area-owners.md if you want to be subscribed.

@EgorBo
Copy link
Member

EgorBo commented May 28, 2024

@EgorBot -intel -amd --disasm

using BenchmarkDotNet.Attributes;
using System.Buffers;
using System.Text;
using System;
using BenchmarkDotNet.Running;

BenchmarkRunner.Run<AsciiBenchmarks>(args: args);

public class AsciiBenchmarks
{
    [Params(1000)]
    public int Length;

    private byte[] _bytes;
    private char[] _chars;

    [GlobalSetup]
    public void Setup()
    {
        _chars = new string('a', Length).ToCharArray();
        _bytes = Encoding.ASCII.GetBytes(_chars);
    }

    [Benchmark]
    public OperationStatus FromUtf16() => Ascii.FromUtf16(_chars, _bytes, out _);
}

@EgorBot
Copy link

EgorBot commented May 28, 2024

BenchmarkDotNet v0.13.12, Ubuntu 22.04.4 LTS (Jammy Jellyfish)
Intel Xeon Platinum 8370C CPU 2.80GHz, 1 CPU, 16 logical and 8 physical cores
  Job-BWGLXM : .NET 9.0.0 (42.42.42.42424), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI
  Job-ULJSQF : .NET 9.0.0 (42.42.42.42424), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI
Method Toolchain Length Mean Error Ratio Code Size
FromUtf16 Main 1000 34.96 ns 0.007 ns 1.00 1,104 B
FromUtf16 PR 1000 27.99 ns 0.006 ns 0.80 1,033 B
More data

See BDN_Artifacts.zip for details.

@EgorBot
Copy link

EgorBot commented May 28, 2024

BenchmarkDotNet v0.13.12, Ubuntu 22.04.4 LTS (Jammy Jellyfish)
AMD EPYC 7763, 1 CPU, 16 logical and 8 physical cores
  Job-NUESEZ : .NET 9.0.0 (42.42.42.42424), X64 RyuJIT AVX2
  Job-WIGDOZ : .NET 9.0.0 (42.42.42.42424), X64 RyuJIT AVX2
Method Toolchain Length Mean Error Ratio Code Size
FromUtf16 Main 1000 33.02 ns 0.004 ns 1.00 796 B
FromUtf16 PR 1000 23.83 ns 0.006 ns 0.72 760 B
More data

See BDN_Artifacts.zip for details.

@ShreyasJejurkar
Copy link
Contributor

Whooooo nice bot @EgorBo. Pretty interesting...😅🧐

@MihaZupan
Copy link
Member Author

@EgorBot -arm64 -amd --disasm

using BenchmarkDotNet.Attributes;
using System.Buffers;
using System.Text;
using System;
using BenchmarkDotNet.Running;

BenchmarkRunner.Run<AsciiBenchmarks>(args: args);

public class AsciiBenchmarks
{
    [Params(1000)]
    public int Length;

    private byte[] _bytes;
    private char[] _chars;

    [GlobalSetup]
    public void Setup()
    {
        _chars = new string('a', Length).ToCharArray();
        _bytes = Encoding.ASCII.GetBytes(_chars);
    }

    [Benchmark]
    public OperationStatus FromHexString() => Convert.FromHexString(_chars, _bytes, out _, out _);
}

@EgorBot
Copy link

EgorBot commented May 28, 2024

BenchmarkDotNet v0.13.12, Ubuntu 22.04.4 LTS (Jammy Jellyfish)
Intel Xeon Platinum 8370C CPU 2.80GHz, 1 CPU, 16 logical and 8 physical cores
  Job-KKQGMF : .NET 9.0.0 (42.42.42.42424), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI
  Job-GAATJR : .NET 9.0.0 (42.42.42.42424), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI
Method Toolchain Length Mean Error Ratio
FromHexString Main 1000 131.7 ns 0.11 ns 1.00
FromHexString PR 1000 115.3 ns 0.02 ns 0.88
More data

See BDN_Artifacts.zip for details.

@dotnet dotnet deleted a comment from EgorBot May 28, 2024
@tannergooding tannergooding merged commit 5c74f63 into dotnet:main May 29, 2024
Ruihan-Yin pushed a commit to Ruihan-Yin/runtime that referenced this pull request May 30, 2024
* Use cheaper Vector.Narrow altenatives in Ascii

* Use existing Ascii helper in HexConverter
@github-actions github-actions bot locked and limited conversation to collaborators Jun 29, 2024
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