jschaeff

jschaeff

Hello,

TL;DR: is it OK in elixir to pass large (MBytes) binary strings through function parameters ? If not, what would be a clean way to transfer this binary to the end user in a Plug HTTP response ?

Context: My program reads binary data from storage using a NIF, in a format that Elixir does not know about. Basically, this binary string has to be delivered to the end user through HTTP request. It can get quite big (10s of MBytes) and it works in my demonstrator by passing the binary string through the return of the NIF function and then passed along to the caller function which would then send it to the client through Plug.Connection.send/2

case MSeed.NIF.stream_data(conn.assigns.datafiles) do
  {:ok, msrecords} -> send_data(conn, msrecords)
end

And the send_data/2 function prepares the HTTP result (resp headers) and sends the content of msrecords, like:

Plug.Conn.send_resp(conn, 200, msrecords)

What are the impacts of passing large data through function parameters ? Are there other better ways to do this ?

Showing Posts 1 to 6

Asd

Asd

It is

None

NobbZ

NobbZ

Large binaries (> 64 byte or so) are passed by reference. Small binaries by value.

Only exception, the small binarie is a substring of a large binarie, in that case a fat pointer is passed, hindering the original binary from garbage collection as long as the substring is “alive”, thats why many slicing operations have a :copy option.

The only “expensive” action is to send them over a network.

If we are talking about large nested data structures (maps + lists) then sending them across processes might already be expensive, as this involves a deep copy!

Binaries are the only datatype that exists on a “shared” heap.

Asd

Asd

All binaries are passed by reference into a function call. You are taking about sending binaries between processes

NobbZ

NobbZ

You are right, in function calls both are passed by reference, my bad. Sending across processes is where it differs.

Though the question is literally about “function parameters” (emph mine):

Anyway, the fact that strings are fine across processes and barely increase the overhead has been made by both of our posts.

christhekeele

christhekeele

Additionally, these shared references are refcounted by processes that have ever accessed them.

So it is safe and cheap to pass them through function parameters, and safe and cheap to send them between processes, but dangerous to handle them in long-running processes as the long-lived process will cause the reference to be held, filling up RAM with your large binary, which cannot be reclaimed without manual intervention. At least not until every process that has ever handled them has died. For a recent thread on this phenomenon with detection, remediation, and other tips check out Agent keeps large binary memory despite no state .

If the process reading from your NIF is short-lived, you will be fine; if it’s a GenServer you may want to spin up a worker process on demand to do the actual handling to prevent memory bloat. Phoenix uses short-lived processes per-request so that process boundary should be fine to handle large binaries.

jschaeff

jschaeff OP

Thanks for all the answers, short and detailed ones.

Indeed, my NIF function is a short-living one, I think I’m safe here.

Again, this forum is great thanks to you all nice folks. Have a nice day !

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews