nova icon indicating copy to clipboard operation
nova copied to clipboard

Optimize hot path performance: cache config lookups and streamline list operations

Open Copilot opened this issue 2 months ago • 0 comments

Several performance bottlenecks exist in request handling hot paths, particularly repeated application:get_env/3 calls and inefficient list operations.

Changes

Cache configuration lookups in nova_router.erl

  • Cache dispatch_backend in function scope for execute/2, lookup_url/3, add_routes/2, render_status_page/*, and add_plugin/1
  • Hoist global_plugins lookup from compile_paths/3 loop to compile/3, passing via Options map
  • Remove redundant lookup_url/4 helper

Cache session manager in nova_session.erl

  • Store session manager module in persistent_term during supervisor init
  • Replace application:get_env/3 with persistent_term:get/2 in get_session_module/0
%% Before: called on every session operation
get_session_module() ->
    application:get_env(nova, session_manager, nova_session_ets).

%% After: cached lookup
get_session_module() ->
    persistent_term:get(nova_session_manager, nova_session_ets).

Optimize list operations in nova_router.erl

  • Replace RouterRoutes ++ lists:append(ControllerRoutes) with single lists:append([RouterRoutes | ControllerRoutes]) in get_routes/2

Simplify module loading in nova_basic_handler.erl

  • Replace code:is_loaded/1 + code:load_file/1 pattern with direct try/catch
  • Erlang's code server auto-loads modules on first function call
  • Add stacktrace arity matching to distinguish template errors from other undef errors

Impact

Reduces overhead in:

  • Request routing (execute/2)
  • Session operations (every get/set)
  • Template rendering (render_dtl/3)
  • Route compilation (compile_paths/3 loop)

[!WARNING]

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • esm.ubuntu.com
    • Triggering command: /usr/lib/apt/methods/https /usr/lib/apt/methods/https (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

Identify and suggest improvements to slow or inefficient code


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot avatar Dec 17 '25 17:12 Copilot