Merge pull request #10417 from qzhuyan/perf/william/no-make-ref-in-get-config

perf(config): eliminate make_ref() calls in config get calls
This commit is contained in:
William Yang 2023-05-03 16:56:44 +02:00 committed by GitHub
commit c38cec77a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 17 deletions

View File

@ -102,6 +102,8 @@
-define(ZONE_CONF_PATH(ZONE, PATH), [zones, ZONE | PATH]). -define(ZONE_CONF_PATH(ZONE, PATH), [zones, ZONE | PATH]).
-define(LISTENER_CONF_PATH(TYPE, LISTENER, PATH), [listeners, TYPE, LISTENER | PATH]). -define(LISTENER_CONF_PATH(TYPE, LISTENER, PATH), [listeners, TYPE, LISTENER | PATH]).
-define(CONFIG_NOT_FOUND_MAGIC, '$0tFound').
-export_type([ -export_type([
update_request/0, update_request/0,
raw_config/0, raw_config/0,
@ -163,9 +165,8 @@ get(KeyPath, Default) -> do_get(?CONF, KeyPath, Default).
-spec find(emqx_utils_maps:config_key_path()) -> -spec find(emqx_utils_maps:config_key_path()) ->
{ok, term()} | {not_found, emqx_utils_maps:config_key_path(), term()}. {ok, term()} | {not_found, emqx_utils_maps:config_key_path(), term()}.
find([]) -> find([]) ->
Ref = make_ref(), case do_get(?CONF, [], ?CONFIG_NOT_FOUND_MAGIC) of
case do_get(?CONF, [], Ref) of ?CONFIG_NOT_FOUND_MAGIC -> {not_found, []};
Ref -> {not_found, []};
Res -> {ok, Res} Res -> {ok, Res}
end; end;
find(KeyPath) -> find(KeyPath) ->
@ -178,9 +179,8 @@ find(KeyPath) ->
-spec find_raw(emqx_utils_maps:config_key_path()) -> -spec find_raw(emqx_utils_maps:config_key_path()) ->
{ok, term()} | {not_found, emqx_utils_maps:config_key_path(), term()}. {ok, term()} | {not_found, emqx_utils_maps:config_key_path(), term()}.
find_raw([]) -> find_raw([]) ->
Ref = make_ref(), case do_get_raw([], ?CONFIG_NOT_FOUND_MAGIC) of
case do_get_raw([], Ref) of ?CONFIG_NOT_FOUND_MAGIC -> {not_found, []};
Ref -> {not_found, []};
Res -> {ok, Res} Res -> {ok, Res}
end; end;
find_raw(KeyPath) -> find_raw(KeyPath) ->
@ -679,11 +679,9 @@ do_get_raw(Path, Default) ->
do_get(?RAW_CONF, Path, Default). do_get(?RAW_CONF, Path, Default).
do_get(Type, KeyPath) -> do_get(Type, KeyPath) ->
Ref = make_ref(), case do_get(Type, KeyPath, ?CONFIG_NOT_FOUND_MAGIC) of
Res = do_get(Type, KeyPath, Ref), ?CONFIG_NOT_FOUND_MAGIC -> error({config_not_found, KeyPath});
case Res =:= Ref of Res -> Res
true -> error({config_not_found, KeyPath});
false -> Res
end. end.
do_get(Type, [], Default) -> do_get(Type, [], Default) ->

View File

@ -2,7 +2,7 @@
{application, emqx_utils, [ {application, emqx_utils, [
{description, "Miscellaneous utilities for EMQX apps"}, {description, "Miscellaneous utilities for EMQX apps"},
% strict semver, bump manually! % strict semver, bump manually!
{vsn, "5.0.0"}, {vsn, "5.0.1"},
{modules, [ {modules, [
emqx_utils, emqx_utils,
emqx_utils_api, emqx_utils_api,

View File

@ -41,14 +41,13 @@
-type config_key_path() :: [config_key()]. -type config_key_path() :: [config_key()].
-type convert_fun() :: fun((...) -> {K1 :: any(), V1 :: any()} | drop). -type convert_fun() :: fun((...) -> {K1 :: any(), V1 :: any()} | drop).
-define(CONFIG_NOT_FOUND_MAGIC, '$0tFound').
%%----------------------------------------------------------------- %%-----------------------------------------------------------------
-spec deep_get(config_key_path(), map()) -> term(). -spec deep_get(config_key_path(), map()) -> term().
deep_get(ConfKeyPath, Map) -> deep_get(ConfKeyPath, Map) ->
Ref = make_ref(), case deep_get(ConfKeyPath, Map, ?CONFIG_NOT_FOUND_MAGIC) of
Res = deep_get(ConfKeyPath, Map, Ref), ?CONFIG_NOT_FOUND_MAGIC -> error({config_not_found, ConfKeyPath});
case Res =:= Ref of Res -> Res
true -> error({config_not_found, ConfKeyPath});
false -> Res
end. end.
-spec deep_get(config_key_path(), map(), term()) -> term(). -spec deep_get(config_key_path(), map(), term()) -> term().

View File

@ -0,0 +1 @@
Improve get config performance by eliminating temporary references.