- Optimized list concatenation in PacketConsumer from O(n) to O(1) by prepending events - Refactored coordinate rounding to avoid recreating anonymous functions on each call - Added database indexes for case-insensitive searches on upper(sender) and upper(base_callsign) - Implemented RegexCache to avoid recompiling regex patterns for wildcard device matching - Added compound index on upper(sender) with received_at for efficient sorted queries These changes improve performance in hot paths: - Packet batching is now more efficient with better list operations - Coordinate processing avoids function allocation overhead - Database queries using UPPER() now use functional indexes - Device wildcard matching no longer recompiles regex on every lookup 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
60 lines
1.5 KiB
Elixir
60 lines
1.5 KiB
Elixir
defmodule Aprsme.RegexCache do
|
|
@moduledoc """
|
|
A simple in-memory cache for compiled regex patterns to avoid recompilation.
|
|
Uses ETS for thread-safe access.
|
|
"""
|
|
|
|
use GenServer
|
|
|
|
require Logger
|
|
|
|
@table_name :regex_cache
|
|
@max_cache_size 1000
|
|
|
|
def start_link(_opts) do
|
|
GenServer.start_link(__MODULE__, %{}, name: __MODULE__)
|
|
end
|
|
|
|
@impl true
|
|
def init(_) do
|
|
# Create ETS table for caching compiled regexes
|
|
:ets.new(@table_name, [:set, :public, :named_table, read_concurrency: true])
|
|
{:ok, %{}}
|
|
end
|
|
|
|
@doc """
|
|
Get or compile a regex pattern. Returns {:ok, regex} or {:error, reason}.
|
|
"""
|
|
def get_or_compile(pattern_string) do
|
|
case :ets.lookup(@table_name, pattern_string) do
|
|
[{^pattern_string, regex}] ->
|
|
{:ok, regex}
|
|
|
|
[] ->
|
|
case Regex.compile(pattern_string) do
|
|
{:ok, regex} ->
|
|
# Check cache size and clear if needed
|
|
if :ets.info(@table_name, :size) >= @max_cache_size do
|
|
clear_oldest_entries()
|
|
end
|
|
|
|
:ets.insert(@table_name, {pattern_string, regex})
|
|
{:ok, regex}
|
|
|
|
error ->
|
|
error
|
|
end
|
|
end
|
|
end
|
|
|
|
defp clear_oldest_entries do
|
|
# Simple strategy: clear half the cache
|
|
# In production, you might want LRU eviction
|
|
entries = :ets.tab2list(@table_name)
|
|
to_remove = Enum.take(entries, div(length(entries), 2))
|
|
|
|
Enum.each(to_remove, fn {key, _} ->
|
|
:ets.delete(@table_name, key)
|
|
end)
|
|
end
|
|
end
|