[erlang-bugs] R14B04 and R15A: Possible bug when compiling code using binary expressions with hipe

Björn Gustavsson bgustavsson@REDACTED
Sun Dec 11 10:42:58 CET 2011


On Sat, Dec 10, 2011 at 10:14 AM, Kostis Sagonas <kostis@REDACTED> wrote:
[...]
> However, the native compiler crash should be fixed.  It really boils down to
> the following code:
>
>  -module(cowboy).
>  -export([f/1]).
>
>  f(<<42, _/bits>> = B) -> B.
>
> for which the BEAM compiler produces:
>
>  label 1:
>    {func_info,{atom,cowboy},{atom,f},1}
>  label 2:
>    {test,bs_start_match2,{f,1},[{x,0},1,0,{x,1}]}
>    {test,bs_match_string,{f,1},[{x,1},8,<<"*">>]}
>    return
>
> This is obviously correct BEAM code, but I think it's suboptimal. I would
> have expected some lighter-weight instruction instead of a bs_match_string
> one, which presumably involves some loop in its implementation.  Bjorn what
> say you?

It is not obvious to me without measuring.

If you don't use bs_match_string, you will need one more
instruction:

 label 1:
   {func_info,{atom,cowboy},{atom,f},1}
 label 2:
   {test,bs_start_match2,{f,1},[{x,0},1,0,{x,1}]}
   {test,bs_get_integer2,{f,1},2,[{x,1},{integer,8},
           1,{field_flags,[{anno,[5,{file,"t.erl"}]},unsigned,big]}],
           {x,2}}.
    {test,is_eq_exact,{f,1},[{x,2},{integer,42}]}.
    return.

That is, one instruction to retrieve 8 bits from the binary and one
instruction to compare that it is equal to 42.

Provided that the binary is aligned on a byte boundary, bs_match_string
will use memcmp(). I think that GCC will inline calls to memcmp(), but
I have no idea how efficient it will be for a size value of one.

/Björn

-- 
Björn Gustavsson, Erlang/OTP, Ericsson AB



More information about the erlang-bugs mailing list