Skip to content

Commit e75c32a

Browse files
authored
Use persistent_term for faster adapter config lookups (#4521)
1 parent 407912c commit e75c32a

File tree

2 files changed

+8
-11
lines changed

2 files changed

+8
-11
lines changed

lib/ecto/repo/registry.ex

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ defmodule Ecto.Repo.Registry do
1818
end
1919

2020
def lookup(repo) when is_atom(repo) do
21-
GenServer.whereis(repo)
22-
|> Kernel.||(raise "could not lookup Ecto repo #{inspect repo} because it was not started or it does not exist")
23-
|> lookup()
21+
:persistent_term.get(repo, nil) ||
22+
raise "could not lookup Ecto repo #{inspect(repo)} because it was not started or it does not exist"
2423
end
2524

2625
def lookup(pid) when is_pid(pid) do
@@ -38,13 +37,15 @@ defmodule Ecto.Repo.Registry do
3837
@impl true
3938
def handle_call({:associate, pid, name, value}, _from, table) do
4039
ref = Process.monitor(pid)
40+
name && :persistent_term.put(name, value)
4141
true = :ets.insert(table, {pid, ref, name, value})
4242
{:reply, :ok, table}
4343
end
4444

4545
@impl true
4646
def handle_info({:DOWN, ref, _type, pid, _reason}, table) do
47-
[{^pid, ^ref, _, _}] = :ets.lookup(table, pid)
47+
[{^pid, ^ref, name, _}] = :ets.lookup(table, pid)
48+
name && :persistent_term.erase(name)
4849
:ets.delete(table, pid)
4950
{:noreply, table}
5051
end

lib/ecto/repo/supervisor.ex

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,6 @@ defmodule Ecto.Repo.Supervisor do
187187

188188
@doc false
189189
def init({name, repo, otp_app, adapter, opts}) do
190-
# Normalize name to atom, ignore via/global names
191-
name = if is_atom(name), do: name, else: nil
192-
193190
case init_config(:supervisor, repo, otp_app, opts) do
194191
{:ok, opts} ->
195192
:telemetry.execute(
@@ -199,6 +196,9 @@ defmodule Ecto.Repo.Supervisor do
199196
)
200197

201198
{:ok, child, meta} = adapter.init([repo: repo] ++ opts)
199+
200+
# Normalize name to atom, ignore via/global names
201+
name = if is_atom(name), do: name, else: nil
202202
cache = Ecto.Query.Planner.new_query_cache(name)
203203
meta = Map.merge(meta, %{repo: repo, cache: cache})
204204
child_spec = wrap_child_spec(child, [name, adapter, meta])
@@ -221,10 +221,6 @@ defmodule Ecto.Repo.Supervisor do
221221
end
222222
end
223223

224-
defp wrap_child_spec({id, start, restart, shutdown, type, mods}, args) do
225-
{id, {__MODULE__, :start_child, [start | args]}, restart, shutdown, type, mods}
226-
end
227-
228224
defp wrap_child_spec(%{start: start} = spec, args) do
229225
%{spec | start: {__MODULE__, :start_child, [start | args]}}
230226
end

0 commit comments

Comments
 (0)