Sign Up
Log In
Log In
or
Sign Up
Places
All Projects
Status Monitor
Collapse sidebar
home:Ledest:erlang:23
erlang
6171-Avoid-redundant-checks-of-the-key-returned...
Overview
Repositories
Revisions
Requests
Users
Attributes
Meta
File 6171-Avoid-redundant-checks-of-the-key-returned-by-ets-lo.patch of Package erlang
From 1dfaa9483a3b36c5b6b7db54a8dae01ffd118c50 Mon Sep 17 00:00:00 2001 From: Robin Morisset <rmorisset@fb.com> Date: Tue, 11 Oct 2022 14:29:22 +0200 Subject: [PATCH] Avoid redundant checks of the key returned by ets:lookup in pg.erl Part of pg.erl in the stdlib has the following pattern: [{Key, Value1, Value2}] = ets:lookup(Table, Key) Because Key appears on the left side, and is already bound, it results in an equality check between the Key that is returned by lookup and the one that was passed to lookup. This is pointless since they are guaranteed to match. I verified on a small example, and this results in 3 bytecode instructions and 19 (!) useless x86 instructions. It can be trivially avoided by using an unbound variable such as _Key: [{_Key, Value1, Value2}] = ets:lookup(Table, Key) I don't expect the performance win to be significant, but it is so simple that I think it worthwhile to land. --- lib/kernel/src/pg.erl | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/kernel/src/pg.erl b/lib/kernel/src/pg.erl index 017b1942b4..aca00d1cb5 100644 --- a/lib/kernel/src/pg.erl +++ b/lib/kernel/src/pg.erl @@ -492,7 +492,7 @@ sync_groups(Scope, ScopeMon, MG, RemoteGroups, [{Group, Pids} | Tail]) -> {Pids, NewRemoteGroups} -> sync_groups(Scope, ScopeMon, MG, NewRemoteGroups, Tail); {OldPids, NewRemoteGroups} -> - [{Group, AllOldPids, LocalPids}] = ets:lookup(Scope, Group), + [{_Group, AllOldPids, LocalPids}] = ets:lookup(Scope, Group), %% should be really rare... AllNewPids = Pids ++ AllOldPids -- OldPids, true = ets:insert(Scope, {Group, AllNewPids, LocalPids}), @@ -517,7 +517,7 @@ join_local([Pid | Tail], Group, Local) -> join_local_update_ets(Scope, ScopeMon, MG, Group, Pid) when is_pid(Pid) -> case ets:lookup(Scope, Group) of - [{Group, All, Local}] -> + [{_Group, All, Local}] -> ets:insert(Scope, {Group, [Pid | All], [Pid | Local]}); [] -> ets:insert(Scope, {Group, [Pid], [Pid]}) @@ -525,7 +525,7 @@ join_local_update_ets(Scope, ScopeMon, MG, Group, Pid) when is_pid(Pid) -> notify_group(ScopeMon, MG, join, Group, [Pid]); join_local_update_ets(Scope, ScopeMon, MG, Group, Pids) -> case ets:lookup(Scope, Group) of - [{Group, All, Local}] -> + [{_Group, All, Local}] -> ets:insert(Scope, {Group, Pids ++ All, Pids ++ Local}); [] -> ets:insert(Scope, {Group, Pids, Pids}) @@ -534,7 +534,7 @@ join_local_update_ets(Scope, ScopeMon, MG, Group, Pids) -> join_remote_update_ets(Scope, ScopeMon, MG, Group, Pid) when is_pid(Pid) -> case ets:lookup(Scope, Group) of - [{Group, All, Local}] -> + [{_Group, All, Local}] -> ets:insert(Scope, {Group, [Pid | All], Local}); [] -> ets:insert(Scope, {Group, [Pid], []}) @@ -542,7 +542,7 @@ join_remote_update_ets(Scope, ScopeMon, MG, Group, Pid) when is_pid(Pid) -> notify_group(ScopeMon, MG, join, Group, [Pid]); join_remote_update_ets(Scope, ScopeMon, MG, Group, Pids) -> case ets:lookup(Scope, Group) of - [{Group, All, Local}] -> + [{_Group, All, Local}] -> ets:insert(Scope, {Group, Pids ++ All, Local}); [] -> ets:insert(Scope, {Group, Pids, []}) @@ -576,10 +576,10 @@ leave_local([Pid | Tail], Group, Local) -> leave_local_update_ets(Scope, ScopeMon, MG, Group, Pid) when is_pid(Pid) -> case ets:lookup(Scope, Group) of - [{Group, [Pid], [Pid]}] -> + [{_Group, [Pid], [Pid]}] -> ets:delete(Scope, Group), notify_group(ScopeMon, MG, leave, Group, [Pid]); - [{Group, All, Local}] -> + [{_Group, All, Local}] -> ets:insert(Scope, {Group, lists:delete(Pid, All), lists:delete(Pid, Local)}), notify_group(ScopeMon, MG, leave, Group, [Pid]); [] -> @@ -588,7 +588,7 @@ leave_local_update_ets(Scope, ScopeMon, MG, Group, Pid) when is_pid(Pid) -> end; leave_local_update_ets(Scope, ScopeMon, MG, Group, Pids) -> case ets:lookup(Scope, Group) of - [{Group, All, Local}] -> + [{_Group, All, Local}] -> case All -- Pids of [] -> ets:delete(Scope, Group); @@ -603,10 +603,10 @@ leave_local_update_ets(Scope, ScopeMon, MG, Group, Pids) -> leave_remote_update_ets(Scope, ScopeMon, MG, Pid, Groups) when is_pid(Pid) -> _ = [ case ets:lookup(Scope, Group) of - [{Group, [Pid], []}] -> + [{_Group, [Pid], []}] -> ets:delete(Scope, Group), notify_group(ScopeMon, MG, leave, Group, [Pid]); - [{Group, All, Local}] -> + [{_Group, All, Local}] -> ets:insert(Scope, {Group, lists:delete(Pid, All), Local}), notify_group(ScopeMon, MG, leave, Group, [Pid]); [] -> @@ -616,7 +616,7 @@ leave_remote_update_ets(Scope, ScopeMon, MG, Pid, Groups) when is_pid(Pid) -> leave_remote_update_ets(Scope, ScopeMon, MG, Pids, Groups) -> _ = [ case ets:lookup(Scope, Group) of - [{Group, All, Local}] -> + [{_Group, All, Local}] -> case All -- Pids of [] when Local =:= [] -> ets:delete(Scope, Group); -- 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