[erlang-questions] Basic question about Erlang binaries
Igor Ribeiro Sucupira
Sat Sep 26 18:06:24 CEST 2009
On Sat, Sep 26, 2009 at 9:56 AM, Robert Virding <rvirding@REDACTED> wrote:
> 2009/9/26 Igor Ribeiro Sucupira <igorrs@REDACTED>
>> On Sat, Sep 26, 2009 at 3:25 AM, Igor Ribeiro Sucupira <igorrs@REDACTED>
>> > 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,
Maybe I'm missing some concept, but to me it seems I'm asking it to
build Bit and to make sure the pattern matching is valid. So, what is
necessary is to check that the integer can be built and is unique.
Can't this be done in constant time?
In other words, I believe the pattern matching above means something
like: "Can you please give me the 1048576th bit, as long as the first
1048575 bits represent a unique (signed, little-endian, blablabla)
integer and and the rest represent an Erlang binary?"
I'm being pretty informal, as it's Saturday (:-)), but I believe you
understand my point.
> 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
I known. I just didn't think it would actually build it and then discard it.
> 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.
Hum... thanks. I believe I heard that before, but had forgotten it.
Sean's pattern would work with a small change, then:
<<_:32, Bit:1/integer, _Rest/bits>> = Binary.
"The secret of joy in work is contained in one word - excellence. To
know how to do something well is to enjoy it." - Pearl S. Buck.
More information about the erlang-questions