Skip to content

propagate empty check to foreach enumeration over Span? #93708

@danmoseley

Description

@danmoseley

should these ideally generate the same code?

static void A(Span<char> chars)
{
    if (!chars.IsEmpty)
    {
        foreach (char c in chars)  
        {
            Console.Write(c);
        }
    }
}

static void B(Span<char> chars)
{
    foreach (char c in chars)  
    {
        Console.Write(c);
    }
}

currently (well, whatever sharplab uses) the 1st one generates an extra test.

https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA0AXEBDAzgWwB8ABAJgAYBYAKDIEYabi6kACYlVgQQAoBlAA7YAdgB4wAC2xQAfK0nTcAShoBvGq02sAlgDNWPAIQKouAHQBJXAFF8AjAE8V1La3UvXW3dBjZJBk3kdYXkpUyVNDU9Nd2jogGEIYVwIABsYMwB1KG0MGB4wJQBuKOiAX1LWCupqphZ2TgAhfiExEzkTZTVK71g/CQCwoO0QzojWSti4xOS0jOzc/MKSj01q2tXWIA===

category:cq
theme:redundant-branches
skill-level:expert
cost:medium
impact:low

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions