Skip to content

Commit 6f51ea1

Browse files
authored
Optimize unused includes detection by avoiding unnecessary work (#1322)
While identifying unused includes, the current implementation performs a go-to-definition operation for all POIs within a document, even if all the .hrl candidates have already been excluded. This change stops the iteration as soon as no candidates are present. The whole detection algorithm could probably be revisited (it would be good to check if this problem has been tackled in literature), but for now this optimization will lower the execution time for "unused includes" diagnostics and reduce the stress on the language server, especially for big modules. As an example, this optimization reduces the computing time for "unused includes" diagnostics for the `els_parser.erl` module from ~1s to ~200ms.
1 parent 32ced53 commit 6f51ea1

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

apps/els_lsp/src/els_unused_includes_diagnostics.erl

+22-19
Original file line numberDiff line numberDiff line change
@@ -86,28 +86,31 @@ find_unused_includes(#{uri := Uri} = Document) ->
8686
els_config:get(exclude_unused_includes)
8787
),
8888
IncludedUris = IncludedUris1 -- ExcludeUnusedIncludes,
89-
Fun = fun(POI, Acc) ->
90-
update_unused(Graph, Uri, POI, Acc)
91-
end,
92-
UnusedIncludes = lists:foldl(Fun, IncludedUris, POIs),
89+
UnusedIncludes = update_unused(IncludedUris, Graph, Uri, POIs),
9390
digraph:delete(Graph),
9491
UnusedIncludes.
9592

96-
-spec update_unused(digraph:graph(), uri(), els_poi:poi(), [uri()]) -> [uri()].
97-
update_unused(Graph, Uri, POI, Acc) ->
98-
case els_code_navigation:goto_definition(Uri, POI) of
99-
{ok, Uri, _DefinitionPOI} ->
100-
Acc;
101-
{ok, DefinitionUri, _DefinitionPOI} ->
102-
case digraph:get_path(Graph, DefinitionUri, Uri) of
103-
false ->
104-
Acc;
105-
Path ->
106-
Acc -- Path
107-
end;
108-
{error, _Reason} ->
109-
Acc
110-
end.
93+
-spec update_unused([uri()], digraph:graph(), uri(), [els_poi:poi()]) -> [uri()].
94+
update_unused(Acc = [], _Graph, _Uri, _POIs) ->
95+
Acc;
96+
update_unused(Acc, _Graph, _Uri, _POIs = []) ->
97+
Acc;
98+
update_unused(Acc, Graph, Uri, [POI | POIs]) ->
99+
NewAcc =
100+
case els_code_navigation:goto_definition(Uri, POI) of
101+
{ok, DefinitionUri, _DefinitionPOI} when DefinitionUri =:= Uri ->
102+
Acc;
103+
{ok, DefinitionUri, _DefinitionPOI} ->
104+
case digraph:get_path(Graph, DefinitionUri, Uri) of
105+
false ->
106+
Acc;
107+
Path ->
108+
Acc -- Path
109+
end;
110+
{error, _Reason} ->
111+
Acc
112+
end,
113+
update_unused(NewAcc, Graph, Uri, POIs).
111114

112115
-spec expand_includes(els_dt_document:item()) -> digraph:graph().
113116
expand_includes(Document) ->

0 commit comments

Comments
 (0)