Skip to content

Commit

Permalink
Avoid listing code path directories
Browse files Browse the repository at this point in the history
If a code path directory has too many .beam files,
then `list_dir/1` becomes a quite expensive operation.
With this commit, code:where_is_file/2 and
code:which/1 attempt to directly find a file,
instead of listing the directory.
  • Loading branch information
josevalim committed Oct 2, 2023
1 parent 0164d3d commit 20f3c11
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lib/kernel/src/code.erl
Original file line number Diff line number Diff line change
Expand Up @@ -859,8 +859,7 @@ which(Module, Path) when is_atom(Module) ->
File = atom_to_list(Module) ++ objfile_extension(),
where_is_file(Path, File).

%% Search the code path for a specific file. Try to locate
%% it in the code path cache if possible.
%% Search the code path for a specific file.

-spec where_is_file(Filename) -> non_existing | Absname when
Filename :: file:filename(),
Expand All @@ -880,9 +879,10 @@ where_is_file([], _) ->
where_is_file([{Path, Files}|Tail], File) ->
where_is_file(Tail, File, Path, Files);
where_is_file([Path|Tail], File) ->
case erl_prim_loader:list_dir(Path) of
{ok,Files} ->
where_is_file(Tail, File, Path, Files);
Full = filename:append(Path, File),
case erl_prim_loader:read_file_info(Full) of
{ok,_} ->
Full;
_Error ->
where_is_file(Tail, File)
end.
Expand Down

0 comments on commit 20f3c11

Please sign in to comment.