[erlang-questions] The is_record() guard

Robert Virding rvirding@REDACTED
Thu Jul 23 22:16:19 CEST 2009


Hi Steve,

2009/7/23 Steve Davis <steven.charles.davis@REDACTED>

> Hi Oscar,
> Oscar Hellström wrote:
>
>> You could also do it this way:
>>
>> transform(#rec_a{} = R) -> ... ;
>> transform(#rec_b{} = R) -> ... ;
>> ...
>>
>
> Ah yes. Hmmm... looks like I have bought myself two possible refactors to
> try/test now ;(


This is is a very good way of doing the test, both clean and efficient. It
will expand the records into tuples and do ONE tuple match for them all. It
is probably even faster than extracting the record name from the tuple with
element/1.

>
>
>  Not that it mattered in the end though but useless
>> optimizations are very common...
>>
>>
> Yep, I have tried to write the code "correct" first - it was the fprof
> result that made me look at this - this match function is necessarily called
> at every invocation and is by far and away the slowest part of the code.


No, using the is_record guard test is not a good way of doing it if you have
the record definition. It might also be useful if the record is in the
middle of a large pattern and it would be messy to write it as a pattern,
but it is slower.

Robert


More information about the erlang-questions mailing list