Aetherus

Aetherus

Advent of Code 2022 - Day 21

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
 ]}

Marked As Solved

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.)

Also Liked

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.

Where Next?

Popular in Challenges Top

sb8244
Note: This topic is to talk about Day 10 of the Advent of Code 2019 . There is a private leaderboard for elixirforum members. You can jo...
New
sasajuric
Note by the Moderators: This topic is to talk about Day 5 of the Advent of Code. For general discussion about the Advent of Code 2018 an...
New
lud
At first I was scared but I found is a simple way to compute the sides. defmodule AdventOfCode.Solutions.Y24.Day12 do alias AdventOfCo...
New
bjorng
Note: This topic is to talk about Day 12 of the Advent of Code 2019. There is a private leaderboard for elixirforum members. You can joi...
New
bjorng
This topic is about Day 9 of the Advent of Code 2021 . We have a private leaderboard (shared with users of Erlang Forums): https://adve...
New
bjorng
Note: This topic is to talk about Day 2 of the Advent of Code 2019 . There is a private leaderboard for elixirforum members. You can joi...
New
bjorng
Note: This topic is to talk about Day 1 of the Advent of Code 2019.
New
stevensonmt
Trying to get more facility with dynamic programming concepts on Leetcode and having an issue I can’t find a way around. It’s a chutes an...
New
New
christhekeele
Thought I’d kick today’s thread off! Parsing Enum rocks, so most of my code was actually in parsing input. ▶ Preprocessing input Part 1...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
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
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
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&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New

We're in Beta

About us Mission Statement