Performance difference between pattern matching and if-else

前端 未结 2 1989
温柔的废话
温柔的废话 2021-02-15 17:06

Why can OCaml generate efficient machine code for pattern matching and not for if-else tests?

I was reading Real World OCaml and I came across this section where they co

2条回答
  •  梦谈多话
    2021-02-15 17:19

    match and if has different semantics: match is parallel, if is strictly sequential. If you have expression:

     match expr with
     | A -> e1
     | B -> e2
     | C -> e3
     | ...
    

    Then it may compare branches in any order. In the example, I provided, it may compile to a binary search, utilizing the fact, that constructors are represented as an oridinary numbers.

    In the expression

    if e1 then e2 else if e3 then e4 else ...
    

    it is required by semantics of the language, that e3 is evaluated strictly after e1 and iff e1 is evaluated to false. That means, that compiler can't rearrange the order of comparisons since it can't know whether e1 is true of false (and if it knows, then with if expression will be pruned with constant folding, so it doesn't matter).

    Back to your example. If you compile your match function you will get, the following code (on x86_64):

    .L104:
        cmpq    $5, %rax
        jbe .L103
        addq    $2, %rax
        ret
    .L103:
        sarq    $1, %rax
        cmpq    $1, %rax
        je  .L101
        jg  .L100
    .L102:
        movq    $3, %rax
        ret
    .L101:
        movq    $5, %rax
        ret
    .L100:
        movq    $7, %rax
        ret
    

    That actually corresponds to expression:

    if x > 2 then x + 1 
    else match compare x 1 with 
    | 0 -> 2
    | 1 -> 3
    | _ -> 1
    

    Quite efficient code, that uses only two comparisons at all. And in runtime it will usually (depending on data distribution) finish with one comparison.

    If you compile an example with if then it will emit the code, that basically equal to the original OCaml code. Because, compiler is required to do so, by the semantics of the if expression. if expression must be sequential.

    One can argue, that the if example, can be compiled to the same code, given that the comparison function is a builtin comparison function. But for that, compiler need to proof, that the comparison function is the builtin or doesn't have side-effects. In that only for one particular case with int. I doubt, that someone will write such optimization, because it doesn't worth.

    Other distinguishing feature of a match expression, is that matching can be performed on a very restricted set of objects, that share one thing in common, they are all ordinal numbers, or can be ordered. In if expressions, we have arbitrary expression.

提交回复
热议问题