Sign Up
Log In
Log In
or
Sign Up
Places
All Projects
Status Monitor
Collapse sidebar
home:Ledest:erlang:23
erlang
6181-Replace-some-uses-of-ets-lookup-by-ets-loo...
Overview
Repositories
Revisions
Requests
Users
Attributes
Meta
File 6181-Replace-some-uses-of-ets-lookup-by-ets-lookup_elemen.patch of Package erlang
From 8134a8078433d46acb4e20df98ec7939845567e3 Mon Sep 17 00:00:00 2001 From: Robin Morisset <rmorisset@fb.com> Date: Tue, 11 Oct 2022 15:45:15 +0200 Subject: [PATCH] Replace some uses of ets:lookup by ets:lookup_element in the stdlib When only one field of an ETS record is needed, using ets:lookup_element is usually faster than ets:lookup. This patch makes sure that we do so whenever possible, taking advantage of the recently landed ets:lookup_element/4 (https://github.com/erlang/otp/pull/6234) in places. --- lib/asn1/src/asn1_db.erl | 11 ++++------- lib/kernel/src/application.erl | 9 ++------- lib/kernel/src/application_controller.erl | 17 +++++++++-------- lib/kernel/src/inet_db.erl | 4 +--- lib/kernel/src/pg.erl | 14 ++------------ lib/stdlib/src/beam_lib.erl | 3 +-- 6 files changed, 19 insertions(+), 39 deletions(-) diff --git a/lib/asn1/src/asn1_db.erl b/lib/asn1/src/asn1_db.erl index 7486fa2db9..28c1fc4563 100644 --- a/lib/asn1/src/asn1_db.erl +++ b/lib/asn1/src/asn1_db.erl @@ -82,11 +82,11 @@ loop(#state{parent = Parent, monitor = MRef, table = Table, includes = Includes} = State) -> receive {set, Mod, K2, V} -> - [{_, Modtab}] = ets:lookup(Table, Mod), + Modtab = ets:lookup_element(Table, Mod, 2), ets:insert(Modtab, {K2, V}), loop(State); {set, Mod, Kvs} -> - [{_, Modtab}] = ets:lookup(Table, Mod), + Modtab = ets:lookup_element(Table, Mod, 2), ets:insert(Modtab, Kvs), loop(State); {From, {get, Mod, K2}} -> @@ -105,7 +105,7 @@ loop(#state{parent = Parent, monitor = MRef, table = Table, end, loop(State); {save, OutFile, Mod} -> - [{_,Mtab}] = ets:lookup(Table, Mod), + Mtab = ets:lookup_element(Table, Mod, 2), TempFile = OutFile ++ ".#temp", ok = ets:tab2file(Mtab, TempFile), ok = file:rename(TempFile, OutFile), @@ -146,10 +146,7 @@ get_table(Table, Mod, Includes) -> end. lookup(Tab, K) -> - case ets:lookup(Tab, K) of - [] -> undefined; - [{K,V}] -> V - end. + ets:lookup_element(Tab, K, 2, undefined). info(EruleMaps) -> {asn1ct:vsn(),EruleMaps}. diff --git a/lib/kernel/src/application.erl b/lib/kernel/src/application.erl index 3eb87d73c7..1730e1e822 100644 --- a/lib/kernel/src/application.erl +++ b/lib/kernel/src/application.erl @@ -397,13 +397,8 @@ get_env(Application, Key) -> Def :: term(), Val :: term(). -get_env(Application, Key, Def) -> - case get_env(Application, Key) of - {ok, Val} -> - Val; - undefined -> - Def - end. +get_env(Application, Key, Default) -> + application_controller:get_env(Application, Key, Default). -spec get_all_env() -> Env when Env :: [{Par :: atom(), Val :: term()}]. diff --git a/lib/kernel/src/application_controller.erl b/lib/kernel/src/application_controller.erl index b82c3b0784..c5b4445bb8 100644 --- a/lib/kernel/src/application_controller.erl +++ b/lib/kernel/src/application_controller.erl @@ -27,7 +27,7 @@ change_application_data/2, prep_config_change/0, config_change/1, which_applications/0, which_applications/1, loaded_applications/0, info/0, set_env/2, - get_pid_env/2, get_env/2, get_pid_all_env/1, get_all_env/1, + get_pid_env/2, get_env/2, get_env/3, get_pid_all_env/1, get_all_env/1, get_pid_key/2, get_key/2, get_pid_all_key/1, get_all_key/1, get_master/1, get_application/1, get_application_module/1, start_type/1, permit_application/2, do_config_diff/2, @@ -343,11 +343,15 @@ get_pid_env(Master, Key) -> end. get_env(AppName, Key) -> - case ets:lookup(ac_tab, {env, AppName, Key}) of - [{_, Val}] -> {ok, Val}; - _ -> undefined + NotFound = make_ref(), + case ets:lookup_element(ac_tab, {env, AppName, Key}, 2, NotFound) of + NotFound -> undefined; + Val -> {ok, Val} end. +get_env(AppName, Key, Default) -> + ets:lookup_element(ac_tab, {env, AppName, Key}, 2, Default). + get_pid_all_env(Master) -> case ets:match(ac_tab, {{application_master, '$1'}, Master}) of [[AppName]] -> get_all_env(AppName); @@ -442,10 +446,7 @@ start_type(Master) -> get_master(AppName) -> - case ets:lookup(ac_tab, {application_master, AppName}) of - [{_, Pid}] -> Pid; - _ -> undefined - end. + ets:lookup_element(ac_tab, {application_master, AppName}, 2, undefined). get_application(Master) -> case ets:match(ac_tab, {{application_master, '$1'}, Master}) of diff --git a/lib/kernel/src/inet_db.erl b/lib/kernel/src/inet_db.erl index 408f563909..4937bc0751 100644 --- a/lib/kernel/src/inet_db.erl +++ b/lib/kernel/src/inet_db.erl @@ -579,9 +579,7 @@ res_update(Option, TagTm) -> end. db_get(Name) -> - try ets:lookup_element(inet_db, Name, 2) - catch error:badarg -> undefined - end. + ets:lookup_element(inet_db, Name, 2, undefined). add_rr(RR) -> %% Questionable if we need to support this; diff --git a/lib/kernel/src/pg.erl b/lib/kernel/src/pg.erl index 017b1942b4..94c9772468 100644 --- a/lib/kernel/src/pg.erl +++ b/lib/kernel/src/pg.erl @@ -203,12 +203,7 @@ get_members(Group) -> -spec get_members(Scope :: atom(), Group :: group()) -> [pid()]. get_members(Scope, Group) -> - try - ets:lookup_element(Scope, Group, 2) - catch - error:badarg -> - [] - end. + ets:lookup_element(Scope, Group, 2, []). %%-------------------------------------------------------------------- %% @doc @@ -219,12 +214,7 @@ get_local_members(Group) -> -spec get_local_members(Scope :: atom(), Group :: group()) -> [pid()]. get_local_members(Scope, Group) -> - try - ets:lookup_element(Scope, Group, 3) - catch - error:badarg -> - [] - end. + ets:lookup_element(Scope, Group, 3, []). %%-------------------------------------------------------------------- %% @doc diff --git a/lib/stdlib/src/beam_lib.erl b/lib/stdlib/src/beam_lib.erl index 0753fbbd2b..b5249df414 100644 --- a/lib/stdlib/src/beam_lib.erl +++ b/lib/stdlib/src/beam_lib.erl @@ -832,8 +832,7 @@ symbol(_, AT, I1, I2, _I3, _Cnt) -> {atm(AT, I1), I2}. atm(AT, N) -> - [{_N, S}] = ets:lookup(AT, N), - S. + ets:lookup_element(AT, N, 2). %% AT is updated. ensure_atoms({empty, AT}, Cs) -> -- 2.35.3
Locations
Projects
Search
Status Monitor
Help
OpenBuildService.org
Documentation
API Documentation
Code of Conduct
Contact
Support
@OBShq
Terms
openSUSE Build Service is sponsored by
The Open Build Service is an
openSUSE project
.
Sign Up
Log In
Places
Places
All Projects
Status Monitor