[erlang-questions] Basic question about Erlang binaries

Robert Virding rvirding@REDACTED
Sat Sep 26 14:56:37 CEST 2009


2009/9/26 Igor Ribeiro Sucupira <igorrs@REDACTED>

> On Sat, Sep 26, 2009 at 3:25 AM, Igor Ribeiro Sucupira <igorrs@REDACTED>
> wrote:
> > On Sat, Sep 26, 2009 at 1:29 AM, Sean Cribbs <seancribbs@REDACTED>
> wrote:
> >
> > If it was just pointer arithmetic, it would be constant (not
> > proportional to the offset). But, as I said, it doesn't seem to be.
> >
> > Try this in the shell:
> >
> > Binary = term_to_binary(lists:seq(1, 1000000)).
> > lists:foreach(fun(_) -> <<_:7, Bit:1, _/binary>> = Binary end,
> > lists:seq(1, 1000000)).
> > lists:foreach(fun(_) -> <<_:1048575, Bit:1, _/binary>> = Binary end,
> > lists:seq(1, 1000000)).
> >
> > The second statement takes some seconds to run, while the third one
> > will take forever (of course I haven't waited  :-)).
>
>
> Hum... this one below finishes fast.  :-)
>
> lists:foreach(fun(_) -> <<_:1048568/binary, Bit:8, _/binary>> = Binary
> end, lists:seq(1, 1000000)).
>
> What happened in the other example (with _:1048575)? Is some
> validation taking place there to make sure an integer matches the
> initial portion of Binary (?)?
>
> Just curious.  :-)
>

This is not really strange if you consider what you are asking it to do. You
are telling it to build a 1048575 bit integer, which you then discard, with
bits from the binary. It will actually build the integer here before it
throws it away. When you tell it to build a 1048568 bit binary it can do
that without copying the data so it is much faster. I haven't tested this in
the compiler if it actually builds the integer.

Instead of /binary you can use /bits or /bitstring (synonyms) to build
binaries without the restriction of being full bytes.

Robert


More information about the erlang-questions mailing list