Skip to content

peephole optimization for lea command #67187

@ShuiRuTian

Description

@ShuiRuTian

Description

I am not sure whether this is a bug or known issue or just that my environment is out of time. Anyway, let the code talks:

        public struct Foo
        {
            public int a;
            public int b;
        }

        public class StructContainer
        {
            Foo foo;

            public void Update()
            {
                this.foo.a += 1;
                this.foo.b += 1;
            }
        }

        public class NormalContainer
        {
            int a;
            int b;
            public void Update()
            {
                this.a += 1;
                this.b += 1;
            }
        }

        [Benchmark]
        public StructContainer StructContainerUpdate()
        {
            var sc = new StructContainer();
            sc.Update();
            return sc;
        }


        [Benchmark]
        public NormalContainer NormalContainerUpdate()
        {
            var nc = new NormalContainer();
            nc.Update();
            return nc;
        }

Generated code:

## .NET 6.0.3 (6.0.322.12309), X64 RyuJIT
; Benchmarks.StructInline.StructContainerUpdate()
       sub       rsp,28
       mov       rcx,offset MT_Benchmarks.StructInline+StructContainer
       call      CORINFO_HELP_NEWSFAST
       lea       rdx,[rax+8]    ; we do not really need this line
       inc       dword ptr [rdx]
       lea       rdx,[rax+0C]
       inc       dword ptr [rdx]
       add       rsp,28
       ret
; Total bytes of code 36

; Benchmarks.StructInline.NormalContainerUpdate()
       sub       rsp,28
       mov       rcx,offset MT_Benchmarks.StructInline+NormalContainer
       call      CORINFO_HELP_NEWSFAST
       inc       dword ptr [rax+8]
       inc       dword ptr [rax+0C]
       add       rsp,28
       ret
; Total bytes of code 30

I know little about CLR, so I may have seriously underestimated the complexity of it. But it feels like the lea command is bit of not clever, right?
The memory layout will not change. So something like this.struct1.struct2.struct3.struct4.property could always be calculated by [rax+offset](if the offset is not too large), isn't it?
We does have this, so this problem is pretty limited, should be another peephole optimization I think.

Configuration

BenchmarkDotNet=v0.13.1, OS=Windows 10.0.22000
Intel Core i9-10900X CPU 3.70GHz, 1 CPU, 20 logical and 10 physical cores
.NET SDK=6.0.201

Regression?

Data

Analysis

category:cq
theme:optimization

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMItenet-performancePerformance related issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions