Explorar o código

perf(config): eliminate make_ref() calls in config get calls

William Yang %!s(int64=2) %!d(string=hai) anos
pai
achega
5ed3c3a92c

+ 9 - 11
apps/emqx/src/emqx_config.erl

@@ -103,6 +103,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,
@@ -164,9 +166,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, [], Ref) of
-        Ref -> {not_found, []};
+    case do_get(?CONF, [], ?CONFIG_NOT_FOUND_MAGIC) of
+        ?CONFIG_NOT_FOUND_MAGIC -> {not_found, []};
         Res -> {ok, Res}
         Res -> {ok, Res}
     end;
     end;
 find(KeyPath) ->
 find(KeyPath) ->
@@ -179,9 +180,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([], Ref) of
-        Ref -> {not_found, []};
+    case do_get_raw([], ?CONFIG_NOT_FOUND_MAGIC) of
+        ?CONFIG_NOT_FOUND_MAGIC -> {not_found, []};
         Res -> {ok, Res}
         Res -> {ok, Res}
     end;
     end;
 find_raw(KeyPath) ->
 find_raw(KeyPath) ->
@@ -666,11 +666,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(),
-    Res = do_get(Type, KeyPath, Ref),
-    case Res =:= Ref of
-        true -> error({config_not_found, KeyPath});
-        false -> Res
+    case do_get(Type, KeyPath, ?CONFIG_NOT_FOUND_MAGIC) of
+        ?CONFIG_NOT_FOUND_MAGIC -> error({config_not_found, KeyPath});
+        Res -> Res
     end.
     end.
 
 
 do_get(Type, [], Default) ->
 do_get(Type, [], Default) ->

+ 1 - 1
apps/emqx_utils/src/emqx_utils.app.src

@@ -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,

+ 4 - 5
apps/emqx_utils/src/emqx_utils_maps.erl

@@ -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(),
-    Res = deep_get(ConfKeyPath, Map, Ref),
-    case Res =:= Ref of
-        true -> error({config_not_found, ConfKeyPath});
-        false -> Res
+    case deep_get(ConfKeyPath, Map, ?CONFIG_NOT_FOUND_MAGIC) of
+        ?CONFIG_NOT_FOUND_MAGIC -> error({config_not_found, ConfKeyPath});
+        Res -> Res
     end.
     end.
 
 
 -spec deep_get(config_key_path(), map(), term()) -> term().
 -spec deep_get(config_key_path(), map(), term()) -> term().

+ 4 - 0
changes/ce/perf-10417.en.md

@@ -0,0 +1,4 @@
+Improve get config performance
+
+eliminate make_ref calls
+