Which is the better way to transfer data from c to erlang

I am writing an erlang nif binding based on msquic. I have a question about the performance of transferring data from c to erlang. When a quic connection is established, a lot of data is transferred from c to erlang, which may be binary or composite term, and these messages are sent by event triggering, so they are sent concurrently. And then I thought there were three ways to do it. The first would assign an env to each quic connect enif_alloc_env, lock it when it is used, send it using enif_send, and reuse the env. The second one is every time there’s data that needs to be sent enif_alloc_env an env, and then we send it using enif_send, and then enif_free_env this env, This is done to avoid locking operations, but adds duplicate enif_alloc_env and enif_free_env. The third is to use the functions in “ei.h”

encode data into binary and send it using enif_send, then binary_to_term in erlang. This is to avoid enif_alloc_env and lock operation. I feel enif_alloc_env has a high performance overhead. And I don’t know which way is better or there are other better ways, of course I can write some test code, but I would like to get your professional advice first.

2 Likes

If the buffers are “locked” at some point, you can use a Resource Binary to not copy at all.

1 Like

Well, Resource Binary is better, but it doesn’t fit my needs

1 Like