Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    Tomasz Kajetan Stańczak
    @tkstanczak
    hello
    Adrian Nasui
    @adriannasui
    hi!
    Tomasz Kajetan Stańczak
    @tkstanczak
    Piotr Gankiewicz
    @spetz
    Can be also run via CLI: dotnet run -c release
    Tomasz Kajetan Stańczak
    @tkstanczak
    public class SomeBenchmark
    {
        [Benchmark(Baseline = true)]
        public string A()
        {
            return "aaa" + "bbb" + "ccc" + "ddd";
        }
    
        [Benchmark]
        public string B()
        {
            return string.Concat("aaa", "bbb", "ccc", "ddd");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            BenchmarkRunner.Run<SomeBenchmark>();
        }
    }
    Piotr Gankiewicz
    @spetz
    using BenchmarkDotNet.Attributes;
    using BenchmarkDotNet.Running;
    using System;
    using System.Reflection.Metadata;
    using System.Text;
    
    [MemoryDiagnoser]
    [DisassemblyDiagnoser]
    public class SomeBenchmark
    {
        string ConvertNumberToString(int x)
        {
    
            if (x < 0)
            {
                return "Invalid number!";
            }
    
            return x.ToString();
        }
    
        string ConvertNumberToStringWithException(int x)
        {
            try
            {
                if (x < 0)
                {
                    throw new ArgumentException(nameof(x));
                }
    
                return x.ToString();
            }
            catch (ArgumentException)
            {
                return "Invlaid number!";
            }
        }
    
        [Benchmark(Baseline = true)]
        public string A()
        {
            StringBuilder result = new StringBuilder();
            for(int i = -100;i<100;++i)
            {
                result.AppendLine(ConvertNumberToString(i));
            }
            return result.ToString();
        }
    
        [Benchmark]
        public string B()
        {
            StringBuilder result = new StringBuilder();
            for (int i = -100; i < 100; ++i)
            {
                result.AppendLine(ConvertNumberToStringWithException(i));
            }
            return result.ToString();
        }
    }
    
    
    class Program
    {
        static void Main(string[] args)
        {
            BenchmarkRunner.Run<SomeBenchmark>();
        }
    }
    Jasper
    @jasper-d
    Tomasz Kajetan Stańczak
    @tkstanczak

    // Copyright (c) 2018 Demerzel Solutions Limited
    // This file is part of the Nethermind library.
    //
    // The Nethermind library is free software: you can redistribute it and/or modify
    // it under the terms of the GNU Lesser General Public License as published by
    // the Free Software Foundation, either version 3 of the License, or
    // (at your option) any later version.
    //
    // The Nethermind library is distributed in the hope that it will be useful,
    // but WITHOUT ANY WARRANTY; without even the implied warranty of
    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    // GNU Lesser General Public License for more details.
    //
    // You should have received a copy of the GNU Lesser General Public License
    // along with the Nethermind. If not, see http://www.gnu.org/licenses/.

    using System;
    using BenchmarkDotNet.Attributes;
    using BenchmarkDotNet.Running;
    using Nethermind.Core.Extensions;

    namespace Nethermind.Perfshop
    {
    [MemoryDiagnoser]
    [DisassemblyDiagnoser(printAsm: true)]
    [CoreJob(baseline: true)]
    public class ReverseBytesBenchmark
    {
    private byte[] Scenario0 = Bytes.FromHexString("0x000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f");

        [GlobalSetup]
        public void Setup()
        {
            byte[] clone = Scenario0.Clone() as byte[];
    
            ArrayVersion();
            ArrayVersion();
            if (!Bytes.AreEqual(clone, Scenario0))
            {
                throw new InvalidBenchmarkDeclarationException($"{nameof(ArrayVersion)}");
            }
    
            Avx2Version();
            Avx2Version();
            if (!Bytes.AreEqual(clone, Scenario0))
            {
                throw new InvalidBenchmarkDeclarationException($"{nameof(Avx2Version)}");
            }
    
            SpanVersion();
            SpanVersion();
            if (!Bytes.AreEqual(clone, Scenario0))
            {
                throw new InvalidBenchmarkDeclarationException($"{nameof(SpanVersion)}");
            }
    
            NaiveVersion();
            NaiveVersion();
            if (!Bytes.AreEqual(clone, Scenario0))
            {
                throw new InvalidBenchmarkDeclarationException($"{nameof(NaiveVersion)}");
            }
    
            NaiveInPlaceVersion();
            NaiveInPlaceVersion();
            if (!Bytes.AreEqual(clone, Scenario0))
            {
                throw new InvalidBenchmarkDeclarationException($"{nameof(NaiveInPlaceVersion)}");
            }
        }
    
        [Benchmark(Baseline = true)]
        public void ArrayVersion()
        {
            Array.Reverse(Scenario0);
        }
    
        [Benchmark]
        public void Avx2Version()
        {
            Bytes.Avx2Reverse256InPlace(Scenario0);
        }
    
        [Benchmark]
        public void SpanVersion()
        {
            Scenario0.AsSpan().Reverse();
        }
    
        [Benchmark]
        public void NaiveInPlaceVersion()
        {
            Bytes.ReverseInPlace(Scenario0);
        }
    
        [Benchmark]
        public void NaiveVersion()
        {
            Bytes.Reverse(Scenario0);
        }
    }

    }

    dotnet tool install --global dotnet-trace
    dotnet tool install --global dotnet-counters
    dotnet tool install --global dotnet-dump
    Piotr Gankiewicz
    @spetz
    Nethermind.PerfTest -> Program
    Piotr Gankiewicz
    @spetz
    dotnet-trace collect --process-id $PID
    Piotr Gankiewicz
    @spetz
    dotnet-counters monitor --process-id $PID
    Piotr Gankiewicz
    @spetz
    image.png
    Piotr Gankiewicz
    @spetz
    Nethermind.Evm -> VirtualMachine.cs -> UpdateGas (hurt the performance)
    Tomasz Kajetan Stańczak
    @tkstanczak

    // Copyright (c) 2018 Demerzel Solutions Limited
    // This file is part of the Nethermind library.
    //
    // The Nethermind library is free software: you can redistribute it and/or modify
    // it under the terms of the GNU Lesser General Public License as published by
    // the Free Software Foundation, either version 3 of the License, or
    // (at your option) any later version.
    //
    // The Nethermind library is distributed in the hope that it will be useful,
    // but WITHOUT ANY WARRANTY; without even the implied warranty of
    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    // GNU Lesser General Public License for more details.
    //
    // You should have received a copy of the GNU Lesser General Public License
    // along with the Nethermind. If not, see http://www.gnu.org/licenses/.

    using System;
    using System.Collections.Generic;
    using BenchmarkDotNet.Attributes;
    using Nethermind.Core;
    using Nethermind.Core.Extensions;
    using Nethermind.Dirichlet.Numerics;

    namespace Nethermind.Perfshop
    {
    [MemoryDiagnoser]
    [DisassemblyDiagnoser(printAsm: true)]
    [CoreJob(baseline: true)]
    public class SwapBytesBenchmark
    {
    private static Random random = new Random();

        private const ulong number = 1230123812841984UL;
    
        private List<List<byte[]>> _data = new List<List<byte[]>>();
    
        private List<(List<byte[]>, Bloom)> _dataWithBlooms = new List<(List<byte[]>, Bloom)>();
    
        [GlobalSetup]
        public void GlobalSetup()
        {
            for (int i = 0; i < 100000; i++)
            {
                var nested = new List<byte[]>();
                _data.Add(nested);
                Bloom bloom = new Bloom();
                _dataWithBlooms.Add((nested, bloom));
                for (int j = 0; j < 1000; j++)
                {
                    byte[] bytes = new byte[32];
                    random.NextBytes(bytes);
                    nested.Add(bytes);
                }
            }
        }
    
        [Benchmark(Baseline = true)]
        public bool Search()
        {
            byte[] searchedFor = new byte[32];
            searchedFor[5] = 1;
    
            foreach (List<byte[]> byteses in _data)
            {
                foreach (byte[] bytes in byteses)
                {
                    if (Bytes.AreEqual(searchedFor, bytes))
                    {
                        return true;
                    }
                }
            }
    
            return false;
        }
    
        [Benchmark(Baseline = true)]
        public bool SearchWithBlooms()
        {
            byte[] searchedFor = new byte[32];
            searchedFor[5] = 1;
    
            foreach (List<byte[]> byteses in _data)
            {
                foreach (byte[] bytes in byteses)
                {
                    if (Bytes.AreEqual(searchedFor, bytes))
                    {
                        return true;
                    }
                }
            }
    
            return false;
        }
    }

    }

            foreach ((List<byte[]> Data, Bloom Bloom) dataWithBloom in _dataWithBlooms)
            {
                foreach (byte[] bytes in dataWithBloom.Data)
                {
                    if (Bytes.AreEqual(searchedFor, bytes))
                    {
                        return true;
                    }
                }
            }
    Tomasz Kajetan Stańczak
    @tkstanczak

    // Copyright (c) 2018 Demerzel Solutions Limited
    // This file is part of the Nethermind library.
    //
    // The Nethermind library is free software: you can redistribute it and/or modify
    // it under the terms of the GNU Lesser General Public License as published by
    // the Free Software Foundation, either version 3 of the License, or
    // (at your option) any later version.
    //
    // The Nethermind library is distributed in the hope that it will be useful,
    // but WITHOUT ANY WARRANTY; without even the implied warranty of
    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    // GNU Lesser General Public License for more details.
    //
    // You should have received a copy of the GNU Lesser General Public License
    // along with the Nethermind. If not, see http://www.gnu.org/licenses/.

    using System;
    using System.Collections.Generic;
    using BenchmarkDotNet.Attributes;
    using Nethermind.Core;
    using Nethermind.Core.Extensions;
    using Nethermind.Dirichlet.Numerics;

    namespace Nethermind.Perfshop
    {
    [MemoryDiagnoser]
    [DisassemblyDiagnoser(printAsm: true)]
    [CoreJob(baseline: true)]
    public class BloomsBenchmark
    {
    private static Random random = new Random();

        private const ulong number = 1230123812841984UL;
    
        private List<List<byte[]>> _data = new List<List<byte[]>>();
    
        private List<(List<byte[]>, Bloom)> _dataWithBlooms = new List<(List<byte[]>, Bloom)>();
    
        [GlobalSetup]
        public void GlobalSetup()
        {
            for (int i = 0; i < 100000; i++)
            {
                var nested = new List<byte[]>();
                _data.Add(nested);
                Bloom bloom = new Bloom();
                _dataWithBlooms.Add((nested, bloom));
                for (int j = 0; j < 1000; j++)
                {
                    byte[] bytes = new byte[32];
                    random.NextBytes(bytes);
                    bloom.Set(bytes);
                    nested.Add(bytes);
                }
            }
        }
    
        [Benchmark(Baseline = true)]
        public bool Search()
        {
            byte[] searchedFor = new byte[32];
            searchedFor[5] = 1;
    
            foreach (List<byte[]> byteses in _data)
            {
                foreach (byte[] bytes in byteses)
                {
                    if (Bytes.AreEqual(searchedFor, bytes))
                    {
                        return true;
                    }
                }
            }
    
            return false;
        }
    
        [Benchmark]
        public bool SearchWithBlooms()
        {
            byte[] searchedFor = new byte[32];
            searchedFor[5] = 1;
    
            foreach ((List<byte[]> Data, Bloom Bloom) dataWithBloom in _dataWithBlooms)
            {
                if (!dataWithBloom.Bloom.Matches(searchedFor))
                {
                    continue;
                }
    
                foreach (byte[] bytes in dataWithBloom.Data)
                {
                    if (Bytes.AreEqual(searchedFor, bytes))
                    {
                        return true;
                    }
                }
            }
    
            return false;
        }
    }

    }

    Jasper
    @jasper-d
    Just checked how local functions are compiled. In IL they are represented in the same way as local lambda functions, i.e. as internal static methods if no variables are captured. Otherwise, a nested private class is generated as a closure and the local function becomes an instance method of that generated class. I guess that makes sense considering that local functions and local lambdas are equivalent.