EgorBot / runtime-utils

MIT License
0 stars 1 forks source link

stackalloc zeroing test #98

Closed EgorBo closed 1 month ago

EgorBo commented 1 month ago

@EgorBot -intel -arm64

using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;

BenchmarkSwitcher.FromAssembly(typeof(Bench).Assembly).Run(args);

public unsafe class Bench
{
    [Benchmark]
    [Arguments(64)]
    [Arguments(256)]
    [Arguments(1024)]
    [Arguments(4096)]
    public void ZeroingImplicit(int len)
    {
        Span<byte> span = stackalloc byte[len];
        Consume(span);
    }

    [Benchmark]
    [Arguments(64)]
    [Arguments(256)]
    [Arguments(1024)]
    [Arguments(4096)]

    [SkipLocalsInit] // we're zeroing it by hands
    public void ZeroingExplicit(int len)
    {
        Span<byte> span = stackalloc byte[len];
        span.Clear();
        Consume(span);
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    static void Consume(Span<byte> _) {}
}
EgorBot commented 1 month ago

Benchmark results on AzureIntel

BenchmarkDotNet v0.14.0, Ubuntu 22.04.5 LTS (Jammy Jellyfish)
Intel Xeon Platinum 8370C CPU 2.80GHz, 1 CPU, 4 logical and 2 physical cores
DefaultJob : .NET 9.0.0 (9.0.24.43107), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI
Method len Mean Error
ZeroingImplicit 64 3.156 ns 0.0011 ns
ZeroingExplicit 64 3.213 ns 0.0160 ns
ZeroingImplicit 256 6.179 ns 0.0030 ns
ZeroingExplicit 256 5.985 ns 0.0521 ns
ZeroingImplicit 1024 20.532 ns 0.1192 ns
ZeroingExplicit 1024 13.067 ns 0.0085 ns
ZeroingImplicit 4096 82.392 ns 0.1007 ns
ZeroingExplicit 4096 32.656 ns 0.0242 ns

BDN_Artifacts.zip

EgorBot commented 1 month ago

cc @EgorBo (logs)

EgorBot commented 1 month ago

Benchmark results on AzureAmpere

BenchmarkDotNet v0.14.0, Ubuntu 22.04.5 LTS (Jammy Jellyfish)
AzureAmpere
DefaultJob : .NET 9.0.0 (9.0.24.43107), Arm64 RyuJIT AdvSIMD
Method len Mean Error
ZeroingImplicit 64 4.358 ns 0.0028 ns
ZeroingExplicit 64 6.120 ns 0.1005 ns
ZeroingImplicit 256 8.299 ns 0.0024 ns
ZeroingExplicit 256 11.384 ns 0.0026 ns
ZeroingImplicit 1024 28.283 ns 0.0069 ns
ZeroingExplicit 1024 28.306 ns 0.0234 ns
ZeroingImplicit 4096 108.201 ns 0.0504 ns
ZeroingExplicit 4096 57.645 ns 0.1081 ns

BDN_Artifacts.zip

EgorBot commented 1 month ago

cc @EgorBo (logs)

EgorBo commented 1 month ago

@EgorBot help

EgorBo commented 1 month ago

@EgorBot help

EgorBot commented 1 month ago

EgorBot manual

Usage: @EgorBot [-%target%] [-profiler] [raw args for BDN] `C# snippet surrounded with triple ticks`
-%target%:       Can be -arm64, -amd or -intel. Or multiple at once, e.g. '-arm64 -intel'
                 -intel is used when none of the targets are specified.
                 There are also experimental AWS targets.

-profiler:       Use 'perf record' to collect a flamegraph/hot asm - shouldn't be used 
                 when the given benchmark snippet contains more than one [Benchmark]

-perf_events     Exact events to collect with 'perf', e.g. -perf_events ld_align_lat
                 (events are target-specific).

-commit:         E.g. '-commit 72ea87f vs 380898a' or just '-commit 72ea87f'
                 Runs the benchmark on the specified commit(s) (PR is ignored)

-mono:           Use Mono runtime instead of CoreCLR for all targets. Should be possible to use
                 Mono interp too (LLVM is not supported yet).
                 Mono doesn't support -profiler (at least JIT)
                 To use mono-interp, use BDN args, e.g. --envvars MONO_ENV_OPTIONS:"--interpreter"

-nonativepgo:    Disable native PGO (Build with /p:NoPgoOptimize=true)

-pr %num%:       Run for changes in a specific PR. If bot is invoked from a PR - it takes changes
                 from that PR implicitly.

-[args for BDN]: Args directly passed to BDN e.g. '--disasm', see
                 https://github.com/dotnet/BenchmarkDotNet/blob/master/docs/articles/guides/console-args.md

All targets are Linux-only at the moment. NOTE: BenchmarkRunner.Run or BenchmarkSwitcher.From* can be omitted (snippet without an entrypoint) Although, if they're presented then Program's args must be be forwarded to Run(args: args)

NOTE: [DisassemblyDiagnoser] may cause unexpected crashes in BDN on Linux (at least on x64)

Examples: -intel -arm64 -perf --envvars DOTNET_JitDisasm:Foo

Usage example: link

EgorBot commented 1 month ago

EgorBot manual

Usage: @EgorBot [-%target%] [-profiler] [raw args for BDN] `C# snippet surrounded with triple ticks`
-%target%:       Can be -arm64, -amd or -intel. Or multiple at once, e.g. '-arm64 -intel'
                 -intel is used when none of the targets are specified.
                 There are also experimental AWS targets.

-profiler:       Use 'perf record' to collect a flamegraph/hot asm - shouldn't be used 
                 when the given benchmark snippet contains more than one [Benchmark]

-perf_events     Exact events to collect with 'perf', e.g. -perf_events ld_align_lat
                 (events are target-specific).

-commit:         E.g. '-commit 72ea87f vs 380898a' or just '-commit 72ea87f'
                 Runs the benchmark on the specified commit(s) (PR is ignored)

-mono:           Use Mono runtime instead of CoreCLR for all targets. Should be possible to use
                 Mono interp too (LLVM is not supported yet).
                 Mono doesn't support -profiler (at least JIT)
                 To use mono-interp, use BDN args, e.g. --envvars MONO_ENV_OPTIONS:"--interpreter"

-nonativepgo:    Disable native PGO (Build with /p:NoPgoOptimize=true)

-pr %num%:       Run for changes in a specific PR. If bot is invoked from a PR - it takes changes
                 from that PR implicitly.

-[args for BDN]: Args directly passed to BDN e.g. '--disasm', see
                 https://github.com/dotnet/BenchmarkDotNet/blob/master/docs/articles/guides/console-args.md

All targets are Linux-only at the moment. NOTE: BenchmarkRunner.Run or BenchmarkSwitcher.From* can be omitted (snippet without an entrypoint) Although, if they're presented then Program's args must be be forwarded to Run(args: args)

NOTE: [DisassemblyDiagnoser] may cause unexpected crashes in BDN on Linux (at least on x64)

Examples: -intel -arm64 -perf --envvars DOTNET_JitDisasm:Foo

Usage example: link

EgorBot commented 1 month ago

cc @EgorBo

EgorBot commented 1 month ago

cc @EgorBo