Verified Commit 3b0cddd3 authored by mathieui's avatar mathieui
Browse files

Micro-optimize the roster refresh

The roster wrapper sucks and is way too slow. Halve refresh time by more
than 50% using manually managed counters.
parent 22e2ba44
......@@ -867,7 +867,11 @@ def on_got_offline(self, presence):
# If a resource got offline, display the message in the conversation with this
# precise resource.
contact = roster[jid.bare]
name = contact.name if contact and contact.name else jid.bare
name = jid.bare
if contact:
roster.connected -= 1
if contact.name:
name = contact.name
if jid.resource:
self.add_information_message_to_conversation_tab(jid.full, '\x195}%s is \x191}offline' % name)
self.add_information_message_to_conversation_tab(jid.bare, '\x195}%s is \x191}offline' % name)
......@@ -887,6 +891,7 @@ def on_got_online(self, presence):
if contact is None:
# Todo, handle presence coming from contacts not in roster
return
roster.connected += 1
roster.modified()
if not logger.log_roster_change(jid.bare, 'got online'):
self.information('Unable to write in the log file', 'Error')
......
......@@ -40,6 +40,8 @@ class Roster(object):
section='var').split(':'))
self.groups = {}
self.contacts = {}
self.length = 0
self.connected = 0
# Used for caching roster infos
self.last_built = datetime.now()
......@@ -104,11 +106,12 @@ class Roster(object):
return self.__node.jid
def get_and_set(self, jid):
if not jid in self.contacts:
contact = self.contacts.get(jid)
if contact is None:
contact = Contact(self.__node[jid])
self.contacts[jid] = contact
return contact
return self.contacts[jid]
return contact
def set_node(self, value):
"""Set the slixmpp RosterSingle for our roster"""
......@@ -146,6 +149,7 @@ class Roster(object):
contact = self.get_and_set(key)
if key != self.jid and (contact and self.exists(contact)):
l.append(key)
self.length = len(l)
return l
def get_contacts(self):
......@@ -190,11 +194,7 @@ class Roster(object):
"""
Get the number of connected contacts
"""
n = 0
for contact in self:
if self.exists(contact) and len(contact):
n += 1
return n
return self.connected
def update_contact_groups(self, contact):
"""Regenerate the RosterGroups when receiving a contact update"""
......@@ -219,7 +219,7 @@ class Roster(object):
(used to return the display size, but now we have
the display cache in RosterWin for that)
"""
return len(self.jids())
return self.length
def __repr__(self):
ret = '== Roster:\nContacts:\n'
......@@ -244,7 +244,6 @@ class Roster(object):
except OSError:
return
def exists(self, contact):
if not contact:
return False
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment