[erlang-questions] Dict size in guards

Björn Gustavsson bgustavsson@REDACTED
Sat Feb 12 08:28:12 CET 2011


On Fri, Feb 11, 2011 at 6:10 PM, James Churchman
<jameschurchman@REDACTED> wrote:
> Also why does the compiler do a length check for every time you use length in a guard ?? I thought it did not do repetitive pattern matches that are "the same", so surly repetitive guard would also be similarly optimised?

That would be possible, but it is not done yet.
The compiler currently does not do much
optimization of guards and it does not do
common sub-expression elimination.

>
> also "hacks" like [_,_,_,_,_,_,_,_,_,_,_|_] look very clever, and i guess for very long lists have a speed benefit, but again could the compiler not realise that when you put "when Len > 10" it only needs to check that the list is longer than 10? not get the genuine numeric length and then numeric compare to 10. That seems a basic optimisation that could be made

That basic optimization is not done because the
length(L) will cause an exception if the L is improper,
but only checking 10 elements will not cause an exception.
The compiler does not do optimizations that would
silence potential exceptions (silencing exceptions could
hide bugs in the program and make debugging more
difficult).

Here is an example of an improper list that will
cause a badarg exception in length/1:

lists:seq(1, 20) ++ [a|b]

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


More information about the erlang-questions mailing list