Skip to content

Difference in codegen of string.IsNullOrEmpty() #63116

Closed

Description

@ShreyasJejurkar

I was playing with JIT a little bit recently, and found the below scenario where I am checking does give string is null or empty with two different styles,

  1. With raw null and length check.
  2. and with string.IsNullOrEmpty() built-in method, which does the same raw null and length check behind the scenes.

But the collagen is a little different for both cases!
For given a program below (corresponding sharplab link)

using System;
public class C {
    public void M() {
        var someString = "";
        
        if(someString == null || someString.Length == 0) {
            Console.Write("hello");
        }
    }
    
    public void T() {
        string someString = "";
        
        if(string.IsNullOrEmpty(someString)) {
            Console.Write("hello");
        }
    }
}

Following is the resulting codegen

C.M()
    L0000: push ebp
    L0001: mov ebp, esp
    L0003: mov ecx, [0x8d72018]
    L0009: cmp dword ptr [ecx+4], 0
    L000d: jne short L001a
    L000f: mov ecx, [0x8dce1d8]
    L0015: call System.Console.Write(System.String)
    L001a: pop ebp
    L001b: ret

C.T()
    L0000: mov ecx, [0x8dce1d8]
    L0006: call System.Console.Write(System.String)
    L000b: ret

As we can see above, codegen for string.IsNullOrEmpty(string) is quite optimized, so my question is can we do the same optimization for the raw null check call as well (OR maybe fold that raw call with the built-in method by the compiler), given the fact that the built-in also does the same behind the scenes, but still codegen is quite different!

Activity

  1. ghost added
    area-CodeGen-coreclr
    on Dec 24, 2021
  2. ghost added
    untriaged
    on Dec 24, 2021
  3. huoyaoyuan commented on Dec 24, 2021

    @huoyaoyuan
    Member

    See #63095 and #4207

  4. ShreyasJejurkar commented on Dec 24, 2021

    @ShreyasJejurkar
    ContributorAuthor

    I can see your PR linked to the issue, will that PR will fix this issue!?
    I saw some regression in diff!

  5. EgorBo commented on Dec 24, 2021

    @EgorBo
    Member

    @ShreyasJejurkar in your T method jit sees that someString is a string literal which is never null so it folded everything

  6. ShreyasJejurkar commented on Dec 24, 2021

    @ShreyasJejurkar
    ContributorAuthor

    Thanks @EgorBo for the reply! Won't JIT see same thing for method M because it is the same thing there as well and can fold?

  7. EgorBo commented on Dec 24, 2021

    @EgorBo
    Member
        public void M() {
            var someString = "";
            
            if(someString == null || someString.Length == 0) {
                Console.Write("hello");
            }
        }

    Ah, I missed that part.
    Yes, it's a known problem in jit - it needs "Forward Substitution" feature to handle cases like this one. Honestly, I thought Roslyn always propagated ldstr by value 🤔

  8. ShreyasJejurkar commented on Dec 24, 2021

    @ShreyasJejurkar
    ContributorAuthor

    Even more what got me into thinking that, the string.IsNullOrEmpty() under the hood implementation is the same as method M, but still it did not fold! https://source.dot.net/#System.Private.CoreLib/String.cs,486

  9. EgorBo commented on Dec 24, 2021

    @EgorBo
    Member

    Even more what got me into thinking that, the string.IsNullOrEmpty() under the hood implementation is the same as method M, but still it did not fold! https://source.dot.net/#System.Private.CoreLib/String.cs,486

    It's because JIT actually supports "Forward substitution" but only when it inlines functions.

  10. ShreyasJejurkar commented on Dec 24, 2021

    @ShreyasJejurkar
    ContributorAuthor

    I created a new version of the same IsNullOrEmpty() with the same implementation and with aggressive inline, but still codegen is not optimized, not sure why JIT cannot inline that method as well!

    using System;
    using System.Runtime.CompilerServices;
    public class C {
        
        public void M() {
            var someString = "";
            
            if(IsNullOrEmptyVer(someString)) {
                Console.Write("hello");
            }
        }
        
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public bool IsNullOrEmptyVer(string s) {
            return s == null || s.Length == 0;
        }
        
        
        public void T() {
            string someString = "";
            
            if(string.IsNullOrEmpty(someString)) {
                Console.Write("hello");
            }
        }
    }

    SharpLab link -> https://sharplab.io/#v2:EYLgtghglgdgNAFxBAzmAPgAQEwEYCwAUJgAwAEmuAdAEoCuMCUYAplQMID2YADlADYsATgGVhANygBjFigDcRTAGYK2MuzIBvImV1kde5RQAsZALIAKAJRaDeveIhCyKbixEIhsAOZkAvGQARIEKhPb2duFQAGYWAJIoAHJ0/PwA8kIAorwIAJ4AasIWrqweXjDeVjbaYeF1XDCuglQA6l4ILBaBABYsqZyBVqF1ugC+keO1upEA2mYsCN2cACZxvPwW84srazzpPEycjVQAgt7eQrIoUOIscTD8sD5WALqRRsCcnPxkCcmpGWyBwKRUo5BQ1Ui9kwAHYXP4AjAUj90OgXFQADIsCqLBFkEjDPSTCJTfSkoyYUwAFWstlJ0Nw4LcZR8/iCIShek5uhiFjBVD+yMBOVyxWZnmekPpIwaTTYbSgHS6vX6g0JdWJRKIoyAA===

  11. ShreyasJejurkar commented on Dec 24, 2021

    @ShreyasJejurkar
    ContributorAuthor

    I saw this issue about "Forward Substitution", but it's dead I guess! #6973

  12. EgorBo commented on Dec 24, 2021

    @EgorBo
    Member

    @ShreyasJejurkar it's Roslyn - it decided to save your "" into a temp. Make your IsNullOrEmptyVer static.

            IL_0000: ldstr ""
            IL_0005: stloc.0
            IL_0006: ldarg.0
            IL_0007: ldloc.0
            IL_0008: call instance bool C::IsNullOrEmptyVer(string)
    

    but when you make it static:

            IL_0000: ldstr ""
            IL_0005: call bool C::IsNullOrEmptyVer(string)
    
  13. ShreyasJejurkar commented on Dec 24, 2021

    @ShreyasJejurkar
    ContributorAuthor

    Thanks @EgorBo , it did. SharpLab link

  14. EgorBo commented on Dec 24, 2021

    @EgorBo
    Member

    NOTE: I am not blaming Roslyn here 🙂 - it's a correct IL and it's jit's fault that it doesn't do forward sub.

  15. ShreyasJejurkar commented on Dec 24, 2021

    @ShreyasJejurkar
    ContributorAuthor

    @EgorBo ahh, it's ok. No one to blame here, just an opportunity.
    Are there any plans to address this kind of issue!?

  16. EgorBo commented on Dec 24, 2021

    @EgorBo
    Member

    Are there any plans to address this kind of issue!?

    Soon or later 🙂

  17. removed
    untriaged
    on Jan 25, 2022
  18. added this to the Future milestone on Jan 25, 2022
  19. EgorBo commented on Dec 3, 2022

    @EgorBo
    Member

    Closing since it has expected codegen now (apparently, via Fsub)

  20. ghost locked as resolved and limited conversation to collaborators on Jan 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-CodeGen-coreclr

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions