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 (code — OTP 29.0.2 (kernel 11.0.2))

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. (code — OTP 29.0.2 (kernel 11.0.2))

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

New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
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...
274 41539 114
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement