Skip to content

Equality via pattern-matching compiles to slow code #3914

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
jvanburen opened this issue Apr 22, 2025 · 0 comments
Open

Equality via pattern-matching compiles to slow code #3914

jvanburen opened this issue Apr 22, 2025 · 0 comments
Labels
flambda2 Prerequisite for, or part of, flambda2

Comments

@jvanburen
Copy link
Contributor

jvanburen commented Apr 22, 2025

example:

  type t =
    | Naked_immediate
    | Naked_float32
    | Naked_float
    | Naked_int8
    | Naked_int16
    | Naked_int32
    | Naked_int64
    | Naked_nativeint
    | Naked_vec128

  let equal t1 t2 =
    match t1, t2 with
    | Naked_immediate, Naked_immediate -> true
    | Naked_float32, Naked_float32 -> true
    | Naked_float, Naked_float -> true
    | Naked_int8, Naked_int8 -> true
    | Naked_int16, Naked_int16 -> true
    | Naked_int32, Naked_int32 -> true
    | Naked_int64, Naked_int64 -> true
    | Naked_nativeint, Naked_nativeint -> true
    | Naked_vec128, Naked_vec128 -> true
    | ( ( Naked_immediate | Naked_float32 | Naked_float | Naked_int8
        | Naked_int16 | Naked_int32 | Naked_int64 | Naked_nativeint
        | Naked_vec128 ),
        _ ) ->
      false

leads to

camlExample.equal_1:
        .quad   caml_curry2
        .quad   0x280000000000007
        .quad   camlExample.equal_0_1_code
        .quad   0xffc
        .ascii  "unreachable switch case"
        .byte   0
camlExample.equal_0_1_code:
        sar     rax
        lea     rdx, [rip + .L185]
        movsxd  rax, dword ptr [rdx + 4*rax]
        add     rdx, rax
        jmp     rdx
.L117:
        cmp     rbx, 1
        je      .L121
        mov     eax, 1
        ret
.L121:
        mov     eax, 3
        ret
.L124:
        cmp     rbx, 3
        je      .L128
        mov     eax, 1
        ret
.L128:
        mov     eax, 3
        ret
.L131:
        cmp     rbx, 5
        je      .L135
        mov     eax, 1
        ret
.L135:
        mov     eax, 3
        ret
.L138:
        cmp     rbx, 7
        je      .L142
        mov     eax, 1
        ret
.L142:
        mov     eax, 3
        ret
.L145:
        cmp     rbx, 9
        je      .L149
        mov     eax, 1
        ret
.L149:
        mov     eax, 3
        ret
.L152:
        cmp     rbx, 11
        je      .L156
        mov     eax, 1
        ret
.L156:
        mov     eax, 3
        ret
.L159:
        cmp     rbx, 13
        je      .L163
        mov     eax, 1
        ret
.L163:
        mov     eax, 3
        ret
.L166:
        cmp     rbx, 15
        je      .L170
        mov     eax, 1
        ret
.L170:
        mov     eax, 3
        ret
.L173:
        cmp     rbx, 17
        jl      .L177
        mov     eax, 3
        ret
.L177:
        mov     eax, 1
        ret
@jvanburen jvanburen added the flambda2 Prerequisite for, or part of, flambda2 label Apr 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flambda2 Prerequisite for, or part of, flambda2
Projects
None yet
Development

No branches or pull requests

1 participant