Aetherus

Aetherus

I have a question in Part 1. Let me show my solution first:

defmodule Day21.Part1 do
  def solve(), do: root()

  @external_resource "#{__DIR__}/day21.txt"
      
  for <<monkey::binary-4, ": ", expr::binary>> <- File.stream!(hd @external_resource),
      monkey = String.to_atom(monkey) do
    
    @compile {:inline, [{monkey, 0}]}
    defp unquote(monkey)() do
      unquote(Code.string_to_quoted!(expr))
    end
  end
end

I can get the result calling Day21.Part1.solve() (though it returns a float instead of an integer).

My question is, when I try disassembling the module (thanks to @isaac-rstor for teaching me this feature) and extract the instructions of solve/0,

Day21.Part1
|> :code.get_object_code()
|> elem(1)
|> :beam_disasm.file()
|> elem(5)
|> Enum.find(fn tuple ->
  list = Tuple.to_list(tuple)
  match?([:function, :solve | _], list)
end)

I get

{:function, :solve, 0, 789,
 [
   {:line, 3},
   {:label, 788},
   {:func_info, {:atom, Day21.Part1}, {:atom, :solve}, 0},
   {:label, 789},
   {:allocate, 0, 0},
   {:line, 2},
   {:call, 0, {Day21.Part1, :wvvv, 0}},
   {:call, 0, {Day21.Part1, :whqc, 0}},
   {:move, {:float, 83056452926300.0}, {:x, 0}},
   {:deallocate, 0},
   :return
 ]}

You can see the result in the :move instruction, that’s expected. The question is why there are still 2 :calls to the other functions? Why the instructions are not just

{:function, :solve, 0, 789,
 [
   {:line, 3},
   {:label, 788},
   {:func_info, {:atom, Day21.Part1}, {:atom, :solve}, 0},
   {:label, 789},
   {:line, 2},
   {:move, {:float, 83056452926300.0}, {:x, 0}},
   :return
 ]}

Showing Posts 1 to 3

bjorng

bjorng

Erlang Core Team

The Erlang compiler intentionally never removes function calls (unless the inline option is used) to ensure that tracing of function calls will work predictably. That is, every call in source code should be present in the trace output.

(Edited to mention the inline option.)

Aetherus

Aetherus OP

So, the 2 :calls are never executed in runtime?

bjorng

bjorng

Erlang Core Team

Yes, they are executed, but their return values are ignored.

I now see that you used the inline option. When inlining, the compiler will remove redundant function calls when calls are inlined. My guess why that didn’t happen is that the inliner gave up before it could inline all function calls in the module. It might be possible to force it to inline more if you also give the {inline_size, Size} option.

— All posts loaded —

Where Next? Top

Trending in Challenges Top

Other Trending Topics Top

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
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
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; 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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews