yuanti.primat.es

yuanti.primat.es

programming archaeology, pedantics, and machine intelligence

19 Dec 2010

HiPE byte arrays

Mutable byte arrays are one of Erlang’s most under-appreciated features. The byte array BIFs are defined in erts/emulator/hipe/hipe_bif0.c – the API is given below.

-spec bytearray(non_neg_fixnum(), byte()) -> bytearray().
-spec bytearray_sub(bytearray(), non_neg_fixnum()) -> byte().
-spec bytearray_update(bytearray(), non_neg_fixnum(), byte()) -> bytearray().

Usage:

Eshell V5.7.4  (abort with ^G)
1> A = hipe_bifs:bytearray(10, 0).
<<0,0,0,0,0,0,0,0,0,0>>
2> hipe_bifs:bytearray_sub(A, 0).
0
3> hipe_bifs:bytearray_update(A, 0, 1).
<<1,0,0,0,0,0,0,0,0,0>>
4> A.
<<1,0,0,0,0,0,0,0,0,0>>
5> hipe_bifs:bytearray_sub(A, 0).
1

What could possibly go wrong? Facebook uses hipe_bifs:bytearrays and so should you!