[erlang-questions] Selective receive optimization for NIFs

Paul Davis <>
Fri Jul 13 20:29:36 CEST 2012


Just to round this off I've managed to hack in the recv_set/recv_mark
instructions by editing the Erlang assembler output by hand and then
building the beam from there.

The basic steps are:

    1> compile:file("filename.erl", 'S').
    % Edit filename.S to add recv_set/recv_mark instructions
    2> compile:file("filename.S", [asm, no_postopt]).

Obviously this is horribly dangerous for a whole host of reasons but
it'll work for the short term. At some point I'll have to try diving
into the compiler code again to see if I can't figure out how to make
this work automatically.

On Mon, Jul 9, 2012 at 2:07 PM, Paul Davis <> wrote:
> On Mon, Jul 9, 2012 at 9:05 AM, Dave Smith <> wrote:
>> In the past, I think I solved this problem on a NIF by just passing
>> the ref in. It's a bit more of a pain, but should do the trick.
>>
>> D.
>>
>
> That was the method I tried first and it didn't have any effect. I'll
> spend some time reading the generated beam code for both versions to
> see if I can't figure out a way around it.
>
>> On Mon, Jul 9, 2012 at 7:52 AM, Paul Davis <> wrote:
>>> Sverker,
>>>
>>> Ah! That would explain a lot of my confusion. I spent a lot of time
>>> reading through code thinking the magic was that refs were tagged with
>>> when they were created or had some special flag when they got copied
>>> out of a process. If I have time later this week I'll take some time
>>> to start reading through decompiled beam to try and get a better
>>> understanding and see what I can come up with.
>>>
>>> Thanks,
>>> Paul
>>>
>>> On Mon, Jul 9, 2012 at 4:30 AM, Sverker Eriksson
>>> <> wrote:
>>>> Hi Paul
>>>>
>>>> The OTP-8623 selective receive optimization was achieved partly with static
>>>> code analysis done by the Erlang compiler.
>>>>
>>>> In your case the NIF is creating the ref. To do the same optimization for
>>>> that, would need some sort of dynamic analysis done in runtime.
>>>>
>>>>
>>>> /Sverker, Erlang/OTP
>>>>
>>>>
>>>>
>>>> Paul Davis wrote:
>>>>>
>>>>> I've got a bit of a curious question in relation to OTP-8623 (which is
>>>>> the selective receive for a newly created ref skips messages in the
>>>>> inbox). Apparently this doesn't appear to apply to messages generated
>>>>> from NIFs. Or at least, not in the code I've got.
>>>>>
>>>>> I did spend some time trawling around erts internals but the few
>>>>> references I found weren't enough for the whole thing to click. I'll
>>>>> paste the sample test code I've been using in case I'm doing something
>>>>> supremely silly.
>>>>>
>>>>> Thanks,
>>>>> Paul Davis
>>>>>
>>>>> https://gist.github.com/3073295
>>>>> _______________________________________________
>>>>> erlang-questions mailing list
>>>>> 
>>>>> http://erlang.org/mailman/listinfo/erlang-questions
>>>>>
>>>>>
>>>>
>>>>
>>> _______________________________________________
>>> erlang-questions mailing list
>>> 
>>> http://erlang.org/mailman/listinfo/erlang-questions



More information about the erlang-questions mailing list