[erlang-questions] Network vs native endianness in binary

Hynek Vychodil <>
Fri Apr 27 16:02:47 CEST 2012

I'm working on some kind special "database" engine and found weird
performance behavior. When I use Erlang default (network, big) integer
representation I got better performance then if I use machine native
(little) representation. It is consistent either for HiPE and byte
code BEAM. It seems weird for me especially in HiPE case when I
imagine it have to swap bytes twice. I expect either BEAM and HiPE is
using native byte order in internal integer representation. When there
is less difference between native (little) HiPE and BEAM
implementation then Erlang default which seems to me that HiPE calls
some sort of Bif same as in BEAM. May be I understand it totally wrong
but it seems to me that there can be some fix in HiPE which will make
using native endianness faster than  Erlang default. I'm planning
rewrite mine pure Erlang prototype to Nif's where using native
endianness will be simpler but I'm just curious what is behind.

There is simple testing module and result:


-export([test/0, big/1, native/1]).

test() ->
  Big = generate(1048576, <<>>),
  Native = << <<X:32/native>> || <<X:32>> <=Big >>,
  [timer:tc(?MODULE, big, [Big]),
   timer:tc(?MODULE, native, [Native])].

generate(0, Acc) -> Acc;
generate(N, Acc) -> generate(N-1, <<Acc/binary, (random:uniform(4096)-1):32>>).

big(Bin) when is_binary(Bin) -> big(Bin, 0).

big(<<X:32, Rest/binary>>, S) -> big(Rest, S+X);
big(<<>>, S) -> S.

native(Bin) when is_binary(Bin) -> native(Bin, 0).

native(<<X:32/native, Rest/binary>>, S) -> native(Rest, S+X);
native(<<>>, S) -> S.

17> c(bln, [native, {hipe,[o3]}]).
18> bln:test().
19> bln:test().
20> c(bln).
21> bln:test().
22> bln:test().

I'm getting similar results for mine much more complicated POC.
(Anyway 126Mr/s either 30Mr/s is impressive.)


--Hynek (Pichi) Vychodil

More information about the erlang-questions mailing list