runtime: JIT: Why increment so slow with others operations?

Hi there. While working on a project, I ran into an unexpected problem. The code began to work much slower than the similar one. Trying to understand what was the matter, I found out that the line of code with the field increment causes a performance drop of 10 times. I have given a short example below. I have prepared a code that demonstrates the problem here. I ask for advice on how to solve this. And I also want to draw attention to the possible need to improve the JIT compiler.

Source

        // 2300ms
        public void Write0(T value)
        {
            var channel = _channel;
            var operation = channel.WriterOperation;
            
            channel.WriterOperation++;

            var data = channel.Storage[operation % DATA_CAPACITY];
            var seg = data.Writer;

            seg.WriterMessages[seg.WriterPosition] = value;
        }

        // 240ms
        public void Write1(T value)
        {
            var channel = _channel;
            var operation = channel.WriterOperation;

            //channel.WriterOperation++;

            var data = channel.Storage[operation % DATA_CAPACITY];
            var seg = data.Writer;

            seg.WriterMessages[seg.WriterPosition] = value;
        }

        // 170ms   (170+240=2300?)
        public void Write2(T value)
        {
            var channel = _channel;

            channel.WriterOperation++;
        }

Configuration

win10 x64 .net6

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 25 (10 by maintainers)

Most upvoted comments

[DisassemblyDiagnoser] attribute in Benchmark.Net.