[erlang-questions] Erlang list equality check question

Jesper Louis Andersen jesper.louis.andersen@REDACTED
Mon Mar 26 17:27:51 CEST 2018

On Sat, Mar 24, 2018 at 11:47 PM mko_io <me@REDACTED> wrote:

> But X =:= X1 takes 600us, which shows it walks the whole list to do the
> equality check, my question is why not just do a cheap con cell immediate
> comparison since erlang variables don’t vary and tl(X2) points to X1. Is
> there any method to do the cons equality comparison?
Normally, an equality check runs through three levels:

1. Pointer equality
2. Type analysis. Eterms having different tags must be different.
3. Value analysis, perhaps recursion/congruence checks for the subterms.

So in principle, if your X and X1 are equal at the pointer level, they
should stop dong a comparison. What is a bit more likely though is that you
are checking in the shell and that the shell is interpreted. So it might
react a bit differently. Also, you did not tell us what X1 is, so depending
on how you generated that, it might be an entirely new set of data (and
this requires a deep comparison with recursion then).

The Erlang system does no hash-consing, so if you generate new data,
comparisons will always be expensive.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20180326/2c96893b/attachment.htm>

More information about the erlang-questions mailing list