[erlang-questions] '--' BIF to be used with caution?
Mikael Pettersson
mikpe@REDACTED
Tue Jan 13 14:31:05 CET 2009
Malcolm Dowse writes:
> Hello,
>
> I ran into an issue with Erlang the other day. I'm not sure if it's a bug,
> but I thought I should warn others.
>
> I was using '--' to get the difference between two large lists. As expected
> it was very slow, but, to my surprise, ERTS became completely unresponsive
> for a long time. (Only one CPU was maxed out, and there was still plenty of
> memory available.)
>
> To reproduce (on Ubuntu 7.x, R12B-x), open an Erlang shell and type:
>
> spawn(fun() -> lists:seq(1,1000000) -- lists:seq(1,1000000,2) end).
>
> The unresponsiveness seems to be caused by '--' being a BIF. Is there a good
> reason for making it a BIF if it's not always an improvement?
1) The time complexity for '--'/2 is quadratic, so you shouldn't
expect it to be fast.
2) The unresponsiveness appears to be an implementation bug/limitation.
BIFs have a mechanism for "yielding" the CPU when "enough work"
has been done, in order to allow other processes to make progress,
but the implementation of '--'/2 does not make use of that
mechanism so it has to run to completion.
More information about the erlang-questions
mailing list