Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid table scans on registry #1330

Merged
merged 2 commits into from
Jul 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions lib/absinthe/subscription.ex
Original file line number Diff line number Diff line change
Expand Up @@ -187,24 +187,22 @@ defmodule Absinthe.Subscription do

@doc false
def get(pubsub, key) do
pubsub
|> registry_name
|> Registry.lookup(key)
|> then(fn doc_ids ->
pubsub
|> registry_name
|> Registry.select(
# We compose a list of match specs that basically mean "lookup all keys
# in the doc_ids list"
for {_, doc_id} <- doc_ids,
do: {{:"$1", :_, :"$2"}, [{:==, :"$1", doc_id}], [{{:"$1", :"$2"}}]}
)
end)
|> Map.new(fn {doc_id, doc} ->
doc = Map.update!(doc, :initial_phases, &PipelineSerializer.unpack/1)
name = registry_name(pubsub)

{doc_id, doc}
name
|> Registry.lookup(key)
|> MapSet.new(fn {_pid, doc_id} -> doc_id end)
|> Enum.reduce([], fn doc_id, acc ->
case Registry.lookup(name, doc_id) do
[] ->
acc

[{_pid, doc} | _rest] ->
doc = Map.update!(doc, :initial_phases, &PipelineSerializer.unpack/1)
[{doc_id, doc} | acc]
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The structure here where you first uniq the doc_ids and then reduce into a map with Map.put_new_lazy seems redundant to me. The Mapset already guarantees that each doc_id will only exist once, so Map.put_new_lazy will never be lazy. Each doc_id will only happen once.

Beyond that, in my experiments with the MapSet and Map.new code back in the day it was generally faster to make a list of keys and values and then call Map.new on those keys / values. So perhaps can you benchmark it with using a list as the accumulator instead of the map and then pipe it to map.new?

end)
|> Map.new()
end

@doc false
Expand Down
Loading