Binary comprehensions

Thomas Lindgren <>
Fri Aug 13 14:04:58 CEST 2004


--- Jay Nelson <> wrote:

> Has anyone requested binary comprehensions?

I think it could be an interesting extension, though
it needs a bit of work to compile well (in contrast
with list comprehensions, presumably the elements
wouldn't need to have the same nice size, for
example).

For some special cases, a list comprehension wrapped
in binary_to_list/list_to_binary would suffice. E.g.,

list_to_binary([ foo(X) || X <- binary_to_list(Bin)])

Let's analyze the costs of the above compared to a
direct binary comprehension. Assume that the work
foo(X) is negligible so that we can concentrate on
overheads.

- extracting the bytes from the starting binary: using
binary_to_list, there is the overhead of an
intermediate list and the limitation of only
extracting bytes; better compilation might get rid of
this. Or maybe just a generalized binary_to_list which
splits binaries into user-defined chunks. (And a
library of higher-order operations on binaries?)

- building the final binary: if we want to directly
construct the final binary, we have to know an upper
bound of its size (otherwise we cannot preallocate
memory for it). Since we can't do that, the general
case may have to stitch together smaller binaries.
(Does Erlang/OTP have "heap binaries" these days?)

So, my guess is: binary comprehensions can be a
pleasant notation, but performance wins compared to
list comprehensions are unclear. It will need a bit of
compiler work to do well.

On the other hand, good notation is an end in itself,
and it might be blazingly fast on selected code :-)

Best,
Thomas



		
__________________________________
Do you Yahoo!?
New and Improved Yahoo! Mail - Send 10MB messages!
http://promotions.yahoo.com/new_mail 



More information about the erlang-questions mailing list