Skip to content

JIT: Redundant range check #126264

@rameel

Description

@rameel

https://godbolt.org/z/KMPGzdnv5

public static string? TryUnwrapPrefix(string prefix, string path)
{
    if (prefix == "/")
        return path;

    if (path == prefix)
        return "/";

    if ((uint)prefix.Length < (uint)path.Length)
        if (path.StartsWith(prefix, StringComparison.Ordinal))
            if (path[prefix.Length] == '/')
                return new string(path.AsSpan(prefix.Length));

    return null;
}

Compiled with an unnecessary range check.

If one of the initial if statements is removed or the (uint)prefix.Length < (uint)path.Length check is moved after path.StartsWith then the JIT correctly eliminates the redundant bounds check.


This optimization works as expected in .NET 8-10

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIuntriagedNew issue has not been triaged by the area owner

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions