compiler_cache v1.2.1 CompilerCache behaviour

A callback-based compilation cache GenServer.

This genserver caches compiled expressions into a module.

While expressions are compiling (which can take some time), it returns values by calling Code.eval_quoted/3 which is more efficient in the short run.

Internally this uses a fixed pool of atoms as not to exhaust the atom table. A LRU cache mechanism is used to clean up unused expressions.

Example usage

defmodule MyExpressionParser do
  use CompilerCache

  def create_ast(expression) do
    # Create your AST here based on the expression, e.g.:
    {:ok, ast} = Code.string_to_quoted(expr)
    {ast, []}
  end
end

Configuration

In the use statement it is possible to give extra compile-time options to the compiler cache, like this:

use CompilerCache, [max_size: 1000, cache_misses: 0]

The possible options are:

  • input_name - The name of the variable that’s being used for the input to the compiled function. Defaults to input.

  • max_size - how many entries we can have in the cache table; defaults to 10_000.

  • cache_misses - after how many cache misses the compiler should start compiling the module. Defaults to 1, which means that the first expression will not start compilation, but the second hit will. When specifying :none, no caching will be done. (for test purposes)

  • max_ttl - how long entries are allowed to linger in the cache (without being called), in seconds. Defaults to 10.

Summary

Functions

Returns a specification to start this module under a supervisor

Invoked when the server is started. start_link/3 or start/3 will block until it returns

Callbacks

Create the Abstract Syntax Tree and the execution context for the given expression

Start the given compiler cache

Functions

child_spec(arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

init(mod_def)

Invoked when the server is started. start_link/3 or start/3 will block until it returns.

args is the argument term (second argument) passed to start_link/3.

Returning {:ok, state} will cause start_link/3 to return {:ok, pid} and the process to enter its loop.

Returning {:ok, state, timeout} is similar to {:ok, state} except handle_info(:timeout, state) will be called after timeout milliseconds if no messages are received within the timeout.

Returning {:ok, state, :hibernate} is similar to {:ok, state} except the process is hibernated before entering the loop. See c:handle_call/3 for more information on hibernation.

Returning :ignore will cause start_link/3 to return :ignore and the process will exit normally without entering the loop or calling c:terminate/2. If used when part of a supervision tree the parent supervisor will not fail to start nor immediately try to restart the GenServer. The remainder of the supervision tree will be (re)started and so the GenServer should not be required by other processes. It can be started later with Supervisor.restart_child/2 as the child specification is saved in the parent supervisor. The main use cases for this are:

  • The GenServer is disabled by configuration but might be enabled later.
  • An error occurred and it will be handled by a different mechanism than the Supervisor. Likely this approach involves calling Supervisor.restart_child/2 after a delay to attempt a restart.

Returning {:stop, reason} will cause start_link/3 to return {:error, reason} and the process to exit with reason reason without entering the loop or calling c:terminate/2.

Callback implementation for GenServer.init/1.

Callbacks

create_ast(expression)
create_ast(expression :: any) :: {ast :: term, opts :: [term]}

Create the Abstract Syntax Tree and the execution context for the given expression.

start_link()
start_link() :: GenServer.on_start

Start the given compiler cache