sashaafm

sashaafm

Hey Forum, I’m trying to find some info about the compiler/BEAM. When a production release of an Elixir app is generated does it include dead code? Or can the compiler identify unused code (like it can identify unused variables from Erlang R6B onwards I believe) and not include it?

And what about if a whole module is unused? I’m more keen on understanding this from a metaprogramming perspective. Let’s assume we’ve got the following code:

defmodule MyMod do 
  use OtherMod
end

defmodule OtherMod do
  defmacro __using__ do
    ... some functions ...
  end
end

defmodule UnusedMod do
  defmacro __using__ do
    ... some functions ...
  end
end
  1. When I generated a production release, will the OtherMod be included? Since MyMod just receives the generated functions there’s no use for it to be there or can it be used for hot code reloading purposes?

  2. And what about UnusedMod (considering no other module is using it)?

  3. If the answer is to the above questions is that the code is always included then is there a way to not include dead/unused code?

Thanks!

Showing Posts 1 to 4

ericmj

ericmj

Elixir Core Team

All public functions and macros will be included in the release because you can load new code later or have dynamic code that uses the seemingly unused functions.

Elixir 1.3 will warn for (seemingly) unused functions unless you explicitly ignore them so you will know if you need to remove them.

sashaafm

sashaafm OP

Thank you for your reply @ericmj :slight_smile: so is there no way to not include or remove the beam files/source code from unused functions and modules during compile-time automatically, considering you have no intentions of ever loading new code?

I know it may sound like a weird thing to want to do but it’s for an experiment I’m working on :grin:

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Consider the following code:

defmodule Commands do
  def say_hello, do: IO.puts "Hello!"
  def say_goodbye, do: IO.puts "Goodbye!"
end

defmodule Runner do
  def run_command do
    module = IO.gets |> List.wrap |> Module.concat
    fun = IO.gets |> String.to_atom
    apply(module, fun, [])
  end
end

In this case it isn’t even possible in principle to determine wether the Command module can be removed or not as its use is completely determined at runtime. Now obviously my example program here is a bit contrived but you get the idea.

sashaafm

sashaafm OP

Yes thank you @benwilson512. I understand now :slight_smile:

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
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
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews