gavid

gavid

Compile elixir module directly to BEAM bytecode without loading into memory

I have a system that has thousands of run-time generated modules, some of which I want to cache for future reloading.

Ordinarily, the BEAM bytecode for a runtime generated module can be captured like so (How to read the BEAM bytecode compiled into memory and that is not on the disk?):

{:module, ^module_name, binary_contents, _} =
                    module_definition =
                    defmodule module_name do
                      unquote(contents)
                    end

And then saving bytecode to disk. The module can then later be loaded into memory with :code.load_binary or similar (Erlang -- code)

However, the issue with using defmodule and then capturing the output is that because of Erlang’s hot reloading rules, loading versions of a module can cause processes to be killed. (Erlang -- code)

So what I want to be able to do is create a BEAM file for a module without loading into memory.

I see the beginnings of an answer here: Getting each stage of Elixir's compilation all the way to the BEAM bytecode but I’m not sure how to implement what I need using this information.

I am not sure how to create an arbitrary function that will allow me to compile Elixir source code (as string or AST) directly into BEAM without affecting running processes at all.

Does anyone know how to do this? I have been scouring the elixir source code (elixir/lib/elixir/src/elixir_module.erl at 11a493ec4a07479a74e605b409dd16e23bb5cafe · elixir-lang/elixir · GitHub) to try understand how this may be done but I am struggling to get the answer I need.

P.S. Yes I know that when the BEAM file is loaded manually, it will still potentially cause processes to die, but I don’t think that this is avoidable.

Marked As Solved

josevalim

josevalim

Creator of Elixir

You can pass @compile {:autoload, false} in the module body.

Where Next?

Popular in Questions Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New

We're in Beta

About us Mission Statement