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

bjorng
This topic is about Day 18 of the Advent of Code 2020 . Thanks to @egze, we have a private leaderboard: https://adventofcode.com/2020/l...
New
Aetherus
Today’s problem is really tense. I don’t think I can do it without libgraph.
New
Qqwy
Note by the Moderators: This topic is to talk about Day 6 of the Advent of Code. For general discussion about the Advent of Code 2018 an...
New
shritesh
This was way too easy after the last few days. Simple map, filter and count. https://github.com/shritesh/advent/blob/main/2023/06.livemd
New
bjorng
Note: This topic is to talk about Day 4 of the Advent of Code 2019. There is a private leaderboard for elixirforum members. You can join...
New
adamu
Probably not the most efficient implementation, because part 1 took &gt;1 ms and part 2 &gt;4ms, but the code was simple enough. def p...
New
mattbaker
I’m having so much fun working on the “Protohackers” challenges, I never got into Advent of Code much but this has been amazing. The chal...
New
stevensonmt
Anyone else think the prompt for this challenge is contradictory? The rules for comparing packets include If both values are lists, c...
New
lud
Gosh this one took me sooo much time. At first I was trying to iterate each digit independently on the input A number to make digits cha...
New
Aetherus
Don’t know why the regex ~r/[\W &amp;&amp; [^\.]]/x does not work in Elixir. It works pretty well in Ruby. Anyway, here is my solution: ...
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36128 110
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
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

We're in Beta

About us Mission Statement