Skip to content

Commit

Permalink
cache module completion results
Browse files Browse the repository at this point in the history
avoid reading docs chunks
  • Loading branch information
lukaszsamson committed Jan 2, 2024
1 parent 60f1d1e commit 6b378da
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 24 deletions.
4 changes: 2 additions & 2 deletions lib/elixir_sense/core/introspection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -602,8 +602,8 @@ defmodule ElixirSense.Core.Introspection do
defp to_var(atom, _) when is_atom(atom), do: {:atom, [], nil}
defp to_var(_, position), do: {:"arg#{position}", [], nil}

def get_module_docs_summary(module) do
case NormalizedCode.get_docs(module, :moduledoc) do
def get_module_docs_summary(module, docs \\ nil) do
case docs || NormalizedCode.get_docs(module, :moduledoc) do
{_, doc, metadata} ->
{extract_summary_from_docs(doc), metadata}

Expand Down
78 changes: 56 additions & 22 deletions lib/elixir_sense/providers/suggestion/complete.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ defmodule ElixirSense.Providers.Suggestion.Complete do

alias ElixirSense.Providers.Suggestion.Matcher
alias ElixirSense.Providers.Suggestion.Reducers
require Logger

@module_results_cache_key :"#{__MODULE__}_module_results_cache"

@erlang_module_builtin_functions [{:module_info, 0}, {:module_info, 1}]
@elixir_module_builtin_functions [{:__info__, 1}]
Expand Down Expand Up @@ -510,22 +513,37 @@ defmodule ElixirSense.Providers.Suggestion.Complete do
for mod <- match_modules(hint, true, env, metadata),
usable_as_unquoted_module?(mod) do
mod_as_atom = String.to_atom(mod)
subtype = Introspection.get_module_subtype(mod_as_atom)

desc =
if hint != "" do
Introspection.get_module_docs_summary(mod_as_atom)
else
# performance optimization
# TODO is it still needed on OTP 23+?
{"", %{}}
end

name = ":" <> mod
%{kind: :module, name: name, full_name: name, type: :erlang, desc: desc, subtype: subtype}
case :persistent_term.get({@module_results_cache_key, mod_as_atom}, nil) do
nil -> get_erlang_module_result(mod_as_atom)
result -> result
end
end
end

def fill_erlang_module_cache(module, docs) do
get_erlang_module_result(module, docs)
end

defp get_erlang_module_result(module, docs \\ nil) do
subtype = Introspection.get_module_subtype(module)
desc = Introspection.get_module_docs_summary(module, docs)

name = inspect(module)

result = %{
kind: :module,
name: name,
full_name: name,
type: :erlang,
desc: desc,
subtype: subtype
}

:persistent_term.put({@module_results_cache_key, module}, result)
result
end

defp struct_module_filter(true, %State.Env{} = _env, %Metadata{} = metadata) do
fn module -> Struct.is_struct(module, metadata.structs) end
end
Expand Down Expand Up @@ -715,17 +733,13 @@ defmodule ElixirSense.Providers.Suggestion.Complete do
end)
|> Enum.uniq_by(&elem(&1, 1))
|> Enum.map(fn {name, module, required_alias?} ->
{desc, meta} = Introspection.get_module_docs_summary(module)
subtype = Introspection.get_module_subtype(module)
result =
case :persistent_term.get({@module_results_cache_key, module}, nil) do
nil -> get_elixir_module_result(module)
result -> result
end

result = %{
kind: :module,
type: :elixir,
name: name,
full_name: inspect(module),
desc: {desc, meta},
subtype: subtype
}
result = Map.put(result, :name, name)

if required_alias? do
Map.put(result, :required_alias, module)
Expand All @@ -735,6 +749,26 @@ defmodule ElixirSense.Providers.Suggestion.Complete do
end)
end

def fill_elixir_module_cache(module, docs) do
get_elixir_module_result(module, docs)
end

defp get_elixir_module_result(module, docs \\ nil) do
{desc, meta} = Introspection.get_module_docs_summary(module, docs)
subtype = Introspection.get_module_subtype(module)

result = %{
kind: :module,
type: :elixir,
full_name: inspect(module),
desc: {desc, meta},
subtype: subtype
}

:persistent_term.put({@module_results_cache_key, module}, result)
result
end

defp valid_alias_piece?(<<?., char, rest::binary>>) when char in ?A..?Z,
do: valid_alias_rest?(rest)

Expand Down

0 comments on commit 6b378da

Please sign in to comment.