jmitchell

jmitchell

TLDR: :compile_time_purge_level and dependency compilation are your friends.

Moments ago I published my first Elixir package on hex! While testing trying it out from a new project I discovered it was performing ~20x slower than expected, and I’ve figured out why.

First some context: my nascent package backtrex provides easy access to the backtracking algorithm as a behaviour. For verification purposes it currently includes Sudoku puzzle and solver modules.

If you want to follow along, make a new project, add {:backtrex, "~> 0.1.0"} to the dependency list, and run mix deps.get. Then add the following to the top-level lib file:

  alias Backtrex.Examples.Sudoku.Puzzle
  alias Backtrex.Examples.Sudoku.Solver

  @doc """
  ## Examples

      iex> TmpBdemo.hello
      true
  """
  def hello do
    {:ok, puzzle} = Puzzle.from_list([
      [5,   3, :_, :_,  7, :_, :_, :_, :_],
      [6,  :_, :_,  1,  9,  5, :_, :_, :_],
      [:_,  9,  8, :_, :_, :_, :_,  6, :_],
      [8,  :_, :_, :_,  6, :_, :_, :_,  3],
      [4,  :_, :_,  8, :_,  3, :_, :_,  1],
      [7,  :_, :_, :_,  2, :_, :_, :_,  6],
      [:_,  6, :_, :_, :_, :_,  2,  8, :_],
      [:_, :_, :_,  4,  1,  9, :_, :_,  5],
      [:_, :_, :_, :_,  8, :_, :_,  7,  9]])

    {:ok, expected_solution} = Puzzle.from_list([
      [5, 3, 4, 6, 7, 8, 9, 1, 2],
      [6, 7, 2, 1, 9, 5, 3, 4, 8],
      [1, 9, 8, 3, 4, 2, 5, 6, 7],
      [8, 5, 9, 7, 6, 1, 4, 2, 3],
      [4, 2, 6, 8, 5, 3, 7, 9, 1],
      [7, 1, 3, 9, 2, 4, 8, 5, 6],
      [9, 6, 1, 5, 3, 7, 2, 8, 4],
      [2, 8, 7, 4, 1, 9, 6, 3, 5],
      [3, 4, 5, 2, 8, 6, 1, 7, 9]])

    {:ok, :solution, solution} = puzzle |> Solver.solve

    solution == expected_solution
  end

Finally run mix test.

Ahhh, that’s a lot of logs!! Go to the config/config.exs and add config :logger, level: :warn to silence them. Peace and quiet.

If the test passes, kiss your machine for me. At least on my dev VM it fails because it doesn’t finish within the 60-second timeout. In any case it’s taking far too long.

As you probably gathered from the TLDR, the solution ends up being to tell mix to purge the logs below :warn as well:

config :logger,
  level: :warn,
  compile_time_purge_level: :warn

AND run this before running mix test again.

$ mix do deps.clean backtrex, deps.get, deps.compile

Okay, now it should pass within 5 seconds or so. Out of curiosity I increased the test timeout and ran the test again under the previous configuration. It takes up to 80 seconds to finish. Quite a difference.

Questions

  1. How, if at all, can I keep any of the logs in my package source and still deliver a good user experience?
  2. Those of you with code in production: how sure are you that potentially unused logging calls in your (or your dependencies’) BEAM files aren’t killing your performance? I think if you diligently set an aggressive :compile_time_purge_level in your project’s config and carefully recompiled your dependencies you’ll be fine.

Showing Posts 11 to 2

Tuxified

Tuxified

If I got your question straight, the query tool you’re looking for is xref and is shipped with Elixir (since 1.2?).
You can get some info about it’s usage by issuing mix help xref in your terminal. For example mix xref callers Logger.debug should return a list of occurrences (path to file, line number, function/arity). Hope that helps :smiley:

jmitchell

jmitchell OP

Yes, I plan on doing that today. Thanks again for your help :slight_smile:

jwarlander

jwarlander

I noted that @josevalim responded; my initial thought about this issue was also that the Logger documentation could be more explicit about the advantage of passing a fun.. Maybe a pull request might be in order? :slight_smile:

jmitchell

jmitchell OP

I also submitted a proposal to the elixir-lang-core list: [Proposal] Logger configuration flag to lazily evaluate input

jmitchell

jmitchell OP

That sounds nice, although I’m not familiar enough yet with when/where Hex dependencies are compiled and/or cached. From my experiments it seemed explicitly recompiling a dependency is necessary for :compile_time_purge_level to have any effect.

Lessons learned

As a package consumer I need a tool that can query a call graph from a project and its dependencies. Of course this assumes I maintain a list of queries I care about. If I were using Backtrex (before @jwarlander’s improvement) I’d use such a tool to query for potentially problematic calls like Logger.debug. In this case I’d expect to get back a report of filenames and line numbers that call Logger.debug, and then I could quickly audit all those lines. Anyone know of a query tool that already does this?

Looking at the problem from the perspective of the package publisher a static analysis tool could have helped detect the problem and suggest a refactoring. I’m already using credo to lint Backtrex’s code, but it doesn’t yet pick up on potentially expensive logging calls. [edit: I’ve submitted a proposal]

jmitchell

jmitchell OP

Thank you for your advice and going the extra length to make a pull request. I’ve merged it and verified it resolves logging performance issues even without relying on :compile_time_purge_level.

jwarlander

jwarlander

Passing funs to the Logger.debug/2 calls in backtrex will, at least on my machine, take mix test down to just below 6 seconds given the example outlined in the initial post above - see pull request for details:

https://github.com/jmitchell/backtrex/pull/1

jwarlander

jwarlander

You could try the following pattern, as per the Logger docs:

Logger.debug fn -> {"expensive to calculate debug", [additional: :metadata]} end
minhajuddin

minhajuddin

Good find :thumbsup:
I always assumed that all debug log statements from my code were removed at compile time. I think :compile_time_purge_level should by default follow the Logger level.

:compile_time_purge_level - purges at compilation time all calls that have log level lower than the value of this option. This means that Logger calls with level lower than this option will be completely removed at compile time, accruing no overhead at runtime. Defaults to :debug and only applies to the Logger.debug/2, Logger.info/2, Logger.warn/2, and Logger.error/2 macros (e.g., it doesn’t apply to Logger.log/3. Note that arguments passed to Logger calls that are removed from the AST at compilation time are never evaluated, thus any function call that occurs in these arguments is never executed. As a consequence, avoid code that looks like Logger.debug(“Cleanup: #{perform_cleanup()}”) as in the example perform_cleanup/0 won’t be executed if the :compile_time_purge_level is :info or higher.

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
thiagogsr
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews