You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@couchdb.apache.org by GitBox <gi...@apache.org> on 2022/06/03 15:17:57 UTC
[GitHub] [couchdb] iilyak commented on a diff in pull request #4033: Implement view_report function
iilyak commented on code in PR #4033:
URL: https://github.com/apache/couchdb/pull/4033#discussion_r889043058
##########
src/couch/src/couch_debug.erl:
##########
@@ -794,6 +795,68 @@ id("couch_file:init" ++ _, Pid, _Props) ->
id(_IdStr, _Pid, _Props) ->
"".
+first_call(Pid) ->
+ IC =
+ case process_info(Pid, initial_call) of
+ {initial_call, IC0} -> IC0;
+ undefined -> undefined
+ end,
+ Dict =
+ case process_info(Pid, dictionary) of
+ {dictionary, Dict0} -> Dict0;
+ undefined -> []
+ end,
+ MaybeCall = proplists:get_value('$initial_call', Dict, IC),
+ proplists:get_value(initial_call, Dict, MaybeCall).
+
+find_by_first_call(Module, Function) ->
+ FilterPids = fun(Pid) ->
+ case first_call(Pid) of
+ {Module, Function, _} -> true;
+ _ -> false
+ end
+ end,
+ lists:filter(FilterPids, processes()).
+
+view_report() ->
+ IndexerPids = find_by_first_call(couch_index, init),
+ Pid = lists:nth(1, IndexerPids),
+ {ok, IdxState} = couch_index:get_state(Pid, 0),
+ case element(1, IdxState) of
+ mrst ->
+ {ok, Info} = couch_index:get_info(Pid),
+ {signature, Sig} = lists:keyfind(signature, 1, Info),
+ DbName = element(5, IdxState),
+ Report = [
+ {signature, [{signature, couch_index_util:hexsig(Sig)}]},
+ {db_name, [{db_name, DbName}]},
+ {idx_name, [{idx_name, element(6, IdxState)}]},
+ {reduce_funs, [{reduce_funs, element(1, element(10, IdxState))}]},
+ {def, [{def, element(7, lists:nth(1, element(11, IdxState)))}]},
Review Comment:
You use `lists:nth(1...` to get the first element of a list. This is not idiomatic in erlang for two reasons.
1. There is a specialized function to return first element of a list `hd/1`.
2. The lists in Erlang is represented as nested structure and `lists:nth` recursively traverses the structure. This is not very efficient. On the other hand `hd/1` is in the core of Erlang and implemented in `C`. Which is order of magnitude more efficient.
For these reasons the `lists:nth(1...` is an anti-pattern.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@couchdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org