Skip to content

Commit d243ebc

Browse files
committed
Performance improvements
1 parent f6361cd commit d243ebc

3 files changed

Lines changed: 22 additions & 31 deletions

File tree

pyrogram/client/client.py

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -765,14 +765,17 @@ def fetch_peers(
765765
types.Channel, types.ChannelForbidden
766766
]
767767
]
768-
):
768+
) -> bool:
769+
is_min = False
770+
769771
for entity in entities:
770772
if isinstance(entity, types.User):
771773
user_id = entity.id
772774

773775
access_hash = entity.access_hash
774776

775777
if access_hash is None:
778+
is_min = True
776779
continue
777780

778781
username = entity.username
@@ -808,6 +811,7 @@ def fetch_peers(
808811
access_hash = entity.access_hash
809812

810813
if access_hash is None:
814+
is_min = True
811815
continue
812816

813817
username = getattr(entity, "username", None)
@@ -822,6 +826,8 @@ def fetch_peers(
822826
if username is not None:
823827
self.peers_by_username[username.lower()] = input_peer
824828

829+
return is_min
830+
825831
def download_worker(self):
826832
name = threading.current_thread().name
827833
log.debug("{} started".format(name))
@@ -837,7 +843,6 @@ def download_worker(self):
837843

838844
try:
839845
data, file_name, done, progress, progress_args, path = packet
840-
data = data # type: BaseClient.FileData
841846

842847
directory, file_name = os.path.split(file_name)
843848
directory = directory or "downloads"
@@ -917,8 +922,10 @@ def updates_worker(self):
917922

918923
try:
919924
if isinstance(updates, (types.Update, types.UpdatesCombined)):
920-
self.fetch_peers(updates.users)
921-
self.fetch_peers(updates.chats)
925+
is_min = self.fetch_peers(updates.users) or self.fetch_peers(updates.chats)
926+
927+
users = {u.id: u for u in updates.users}
928+
chats = {c.id: c for c in updates.chats}
922929

923930
for update in updates.updates:
924931
channel_id = getattr(
@@ -935,7 +942,7 @@ def updates_worker(self):
935942
if isinstance(update, types.UpdateChannelTooLong):
936943
log.warning(update)
937944

938-
if isinstance(update, types.UpdateNewChannelMessage):
945+
if isinstance(update, types.UpdateNewChannelMessage) and is_min:
939946
message = update.message
940947

941948
if not isinstance(message, types.MessageEmpty):
@@ -957,22 +964,10 @@ def updates_worker(self):
957964
pass
958965
else:
959966
if not isinstance(diff, types.updates.ChannelDifferenceEmpty):
960-
updates.users += diff.users
961-
updates.chats += diff.chats
962-
963-
if channel_id and pts:
964-
if channel_id not in self.channels_pts:
965-
self.channels_pts[channel_id] = []
966-
967-
if pts in self.channels_pts[channel_id]:
968-
continue
969-
970-
self.channels_pts[channel_id].append(pts)
971-
972-
if len(self.channels_pts[channel_id]) > 50:
973-
self.channels_pts[channel_id] = self.channels_pts[channel_id][25:]
967+
users.update({u.id: u for u in diff.users})
968+
chats.update({c.id: c for c in diff.chats})
974969

975-
self.dispatcher.updates_queue.put((update, updates.users, updates.chats))
970+
self.dispatcher.updates_queue.put((update, users, chats))
976971
elif isinstance(updates, (types.UpdateShortMessage, types.UpdateShortChatMessage)):
977972
diff = self.send(
978973
functions.updates.GetDifference(
@@ -989,13 +984,13 @@ def updates_worker(self):
989984
pts=updates.pts,
990985
pts_count=updates.pts_count
991986
),
992-
diff.users,
993-
diff.chats
987+
{u.id: u for u in diff.users},
988+
{c.id: c for c in diff.chats}
994989
))
995990
else:
996-
self.dispatcher.updates_queue.put((diff.other_updates[0], [], []))
991+
self.dispatcher.updates_queue.put((diff.other_updates[0], {}, {}))
997992
elif isinstance(updates, types.UpdateShort):
998-
self.dispatcher.updates_queue.put((updates.update, [], []))
993+
self.dispatcher.updates_queue.put((updates.update, {}, {}))
999994
elif isinstance(updates, types.UpdatesTooLong):
1000995
log.warning(updates)
1001996
except Exception as e:

pyrogram/client/ext/base_client.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ def __init__(self):
9696
self.date = None
9797

9898
self.rnd_id = MsgId
99-
self.channels_pts = {}
10099

101100
self.peers_by_id = {}
102101
self.peers_by_username = {}

pyrogram/client/ext/dispatcher.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,16 +125,13 @@ def update_worker(self):
125125
log.debug("{} started".format(name))
126126

127127
while True:
128-
update = self.updates_queue.get()
128+
packet = self.updates_queue.get()
129129

130-
if update is None:
130+
if packet is None:
131131
break
132132

133133
try:
134-
users = {i.id: i for i in update[1]}
135-
chats = {i.id: i for i in update[2]}
136-
update = update[0]
137-
134+
update, users, chats = packet
138135
parser = self.update_parsers.get(type(update), None)
139136

140137
parsed_update, handler_type = (

0 commit comments

Comments
 (0)