Skip to content

Commit

Permalink
Compact friends cache
Browse files Browse the repository at this point in the history
All the keys should be strongly referenced by ConnectionState._users so this should save a little bit of space
  • Loading branch information
Gobot1234 committed Feb 1, 2023
1 parent 2550aa1 commit 0600178
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
2 changes: 1 addition & 1 deletion steam/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ async def fetch_post(self, id: int) -> Post[Self]:

def is_friend(self) -> bool:
"""Whether the user is in the :class:`ClientUser`'s friends."""
return self.id64 in self._state.user._friends
return self.id in self._state.user._friends


class BaseUser(PartialUser):
Expand Down
9 changes: 4 additions & 5 deletions steam/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -1232,8 +1232,7 @@ async def handle_get_my_chat_groups(self, msg: chat.GetMyChatRoomGroupsResponse)
@register(EMsg.ClientPersonaState)
def parse_persona_state_update(self, msg: friends.CMsgClientPersonaState) -> None:
for friend in msg.friends:
steam_id = ID(friend.friendid)
after = self.get_user(steam_id.id)
after = self.get_user(_ID64_TO_ID32(friend.friendid))
if after is None:
continue

Expand All @@ -1246,7 +1245,7 @@ def parse_persona_state_update(self, msg: friends.CMsgClientPersonaState) -> Non
self.dispatch("user_update", before, after)

def _add_friend(self, user: User) -> Friend:
self.user._friends[user.id64] = friend = Friend(self, user)
self.user._friends[user.id] = friend = Friend(self, user)
return friend

@register(EMsg.ClientFriendsList)
Expand Down Expand Up @@ -1317,7 +1316,7 @@ async def process_friends(self, msg: friends.CMsgClientFriendsList) -> None:
try:
invite = self.invites.pop(id.id64)
except KeyError:
friend = self.user._friends.pop(id.id64, None)
friend = self.user._friends.pop(id.id, None)
if friend is None:
return log.debug("Unknown friend %s removed", id)
self.dispatch("friend_remove", friend)
Expand All @@ -1337,7 +1336,7 @@ async def process_friends(self, msg: friends.CMsgClientFriendsList) -> None:

if is_load:
await self.login_complete.wait()
self.user._friends = {user.id64: Friend(self, user) for user in await self.fetch_users(client_user_friends)}
self.user._friends = {user.id: Friend(self, user) for user in await self.fetch_users(client_user_friends)}
self.handled_friends.set()

async def set_chat_group_active(self, chat_group_id: ChatGroupID) -> chat.GroupState:
Expand Down
12 changes: 6 additions & 6 deletions steam/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
from .app import PartialApp
from .enums import Language, PersonaState, PersonaStateFlag, Result, TradeOfferState, Type
from .errors import ClientException, ConfirmationError, HTTPException
from .id import ID
from .id import _ID64_TO_ID32, ID
from .profile import ClientUserProfile, OwnedProfileItems, ProfileInfo, ProfileItem
from .protobufs import player
from .types.id import ID64, AppID, Intable
from .types.id import ID32, ID64, AppID, Intable
from .utils import DateTime, parse_bb_code

if TYPE_CHECKING:
Expand Down Expand Up @@ -117,7 +117,7 @@ async def add(self) -> None:
async def remove(self) -> None:
"""Remove the user from your friends list."""
await self._state.remove_user(self.id64)
self._state.user._friends.pop(self.id64, None)
self._state.user._friends.pop(self.id, None)

async def cancel_invite(self) -> None:
"""Cancels an invitation sent to the user. This effectively does the same thing as :meth:`remove`."""
Expand Down Expand Up @@ -252,7 +252,7 @@ class ClientUser(_BaseUser):

def __init__(self, state: ConnectionState, proto: UserProto):
super().__init__(state, proto)
self._friends: dict[ID64, Friend] = {}
self._friends: dict[ID32, Friend] = {}
self._inventory_locks = weakref.WeakValueDictionary[AppID, asyncio.Lock]()

async def friends(self) -> Sequence[Friend]:
Expand All @@ -261,8 +261,8 @@ async def friends(self) -> Sequence[Friend]:

def get_friend(self, id: Intable) -> Friend | None:
"""Get a friend from the client user's friends list."""
id64 = utils.parse_id64(id, type=Type.Individual)
return self._friends.get(id64)
id32 = _ID64_TO_ID32(utils.parse_id64(id, type=Type.Individual))
return self._friends.get(id32)

async def inventory(self, app: App, *, language: Language | None = None) -> Inventory[Item[Self], Self]:
try:
Expand Down

0 comments on commit 0600178

Please sign in to comment.