Skip to content

Commit

Permalink
client.d: is_watching() membership check optimization (#28)
Browse files Browse the repository at this point in the history
* client.d: is_watching() membership check optimization

* client.d: abolish User[] watched_by() list method

* room.d: is_joined() fast user membership check

---------

Co-authored-by: Mat <[email protected]>
  • Loading branch information
slook and mathiascode authored Oct 15, 2024
1 parent f1586f4 commit 2a2e0e9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 28 deletions.
30 changes: 8 additions & 22 deletions src/client.d
Original file line number Diff line number Diff line change
Expand Up @@ -282,41 +282,27 @@ class User
watch_list.remove(username);
}

private User[] watched_by()
private bool is_watching(string peer_username)
{
User[] list;
foreach (user ; server.users)
if (user !is this && username in user.watching) list ~= user;

return list;
}

private User[string] watching()
{
User[string] list;

foreach (username ; watch_list) {
auto user = server.get_user(username);
if (user) list[username] = user;
}
if (peer_username in watch_list)
return true;

foreach (room_name, room ; joined_rooms)
foreach (User user ; room.users) list[user.username] = user;
if (room.is_joined(peer_username))
return true;

return list;
return false;
}

private void send_to_watching(Message msg)
{
if (!watched_by)
return;

debug (msg) writefln(
"Transmit=> %s (code %d) to users watching user %s...",
blue ~ message_name[msg.code] ~ norm, msg.code,
blue ~ username ~ norm
);
foreach (user ; watched_by) user.send_message(msg);
foreach (user ; server.users) if (user !is this)
if (user.is_watching(username)) user.send_message(msg);
}

private void set_status(uint new_status)
Expand Down
7 changes: 6 additions & 1 deletion src/room.d
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,17 @@ class Room
user.join_room(this);
}

bool is_joined(string username)
{
return (username in user_list) ? true : false;
}

ulong nb_users()
{
return user_list.length;
}

User[] users()
private User[] users()
{
return user_list.values;
}
Expand Down
5 changes: 0 additions & 5 deletions src/server.d
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,6 @@ class Server
user_list[user.username] = user;
}

bool find_user(User user)
{
return (user.username in user_list) ? true : false;
}

User get_user(string username)
{
if (username in user_list)
Expand Down

0 comments on commit 2a2e0e9

Please sign in to comment.