From 6057232b126c23bb0a97e0efad7c02e448089548 Mon Sep 17 00:00:00 2001 From: Tollef Fog Heen Date: Mon, 3 Oct 2016 21:09:12 +0200 Subject: Slight performance optimisation Looking for if something is in a list is slightly faster than counting the number of that item in a list, since if it is in the list, the loop can exit early. --- wee_slack.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'wee_slack.py') diff --git a/wee_slack.py b/wee_slack.py index 4850350..8461840 100644 --- a/wee_slack.py +++ b/wee_slack.py @@ -95,7 +95,7 @@ class SearchList(list): if name in self.hashtable: return self.hashtable[name] # this is a fallback to __eq__ if the item isn't in the hashtable already - if self.count(name) > 0: + if name in self: self.update_hashtable() return self[self.index(name)] -- cgit From 128f1dc945790cffd65a874d6746644ac919ba2a Mon Sep 17 00:00:00 2001 From: Tollef Fog Heen Date: Mon, 3 Oct 2016 21:10:07 +0200 Subject: Performance: pass in item to update When adding an item to the hash table, pass in the updated item, which saves a bunch of work. --- wee_slack.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'wee_slack.py') diff --git a/wee_slack.py b/wee_slack.py index 8461840..fe18ac9 100644 --- a/wee_slack.py +++ b/wee_slack.py @@ -101,14 +101,24 @@ class SearchList(list): def append(self, item, aliases=[]): super(SearchList, self).append(item) - self.update_hashtable() + self.update_hashtable(item) - def update_hashtable(self): - for child in self: - if hasattr(child, "get_aliases"): - for alias in child.get_aliases(): + def update_hashtable(self, item=None): + if item is not None: + try: + for alias in item.get_aliases(): if alias is not None: - self.hashtable[alias] = child + self.hashtable[alias] = item + except AttributeError: + pass + else: + for child in self: + try: + for alias in child.get_aliases(): + if alias is not None: + self.hashtable[alias] = child + except AttributeError: + pass def find_by_class(self, class_name): items = [] -- cgit From 0f1613d5eac212e07b2751e7a999656afc642443 Mon Sep 17 00:00:00 2001 From: Tollef Fog Heen Date: Mon, 3 Oct 2016 21:12:34 +0200 Subject: Performance: fetch user object only once Instead of lookup up the user object a bunch of times in buffer_prnt, just fetch it once. --- wee_slack.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'wee_slack.py') diff --git a/wee_slack.py b/wee_slack.py index fe18ac9..b2804b7 100644 --- a/wee_slack.py +++ b/wee_slack.py @@ -658,6 +658,7 @@ class Channel(object): set_read_marker = False time_float = float(time) tags = "nick_" + user + user_obj = self.server.users.find(user) # XXX: we should not set log1 for robots. if time_float != 0 and self.last_read >= time_float: tags += ",no_highlight,notify_none,logger_backlog_end" @@ -678,16 +679,16 @@ class Channel(object): if self.channel_buffer: prefix_same_nick = w.config_string(w.config_get('weechat.look.prefix_same_nick')) if user == self.last_active_user and prefix_same_nick != "": - if colorize_nicks and self.server.users.find(user): - name = self.server.users.find(user).color + prefix_same_nick + if colorize_nicks and user_obj: + name = user_obj.color + prefix_same_nick else: name = prefix_same_nick else: nick_prefix = w.config_string(w.config_get('weechat.look.nick_prefix')) nick_suffix = w.config_string(w.config_get('weechat.look.nick_suffix')) - if self.server.users.find(user): - name = self.server.users.find(user).formatted_name() + if user_obj: + name = user_obj.formatted_name() self.last_active_user = user # XXX: handle bots properly here. else: @@ -700,8 +701,8 @@ class Channel(object): if type(message) is not unicode: message = message.decode('UTF-8', 'replace') curr_color = w.color(chat_color) - if colorize_nicks and colorize_messages and self.server.users.find(user): - curr_color = self.server.users.find(user).color + if colorize_nicks and colorize_messages and user_obj: + curr_color = user_obj.color message = curr_color + message for user in self.server.users: if user.name in message: -- cgit From 87bb133838b4862f985277775f55c9a4b48e92ba Mon Sep 17 00:00:00 2001 From: Tollef Fog Heen Date: Mon, 3 Oct 2016 21:14:08 +0200 Subject: Speed up User comparison Avoid creating a full new string and use a string slice instead. --- wee_slack.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'wee_slack.py') diff --git a/wee_slack.py b/wee_slack.py index b2804b7..8a2276f 100644 --- a/wee_slack.py +++ b/wee_slack.py @@ -880,7 +880,9 @@ class User(object): def __eq__(self, compare_str): try: - if compare_str == self.name or compare_str == "@" + self.name or compare_str == self.identifier: + if compare_str == self.name or compare_str == self.identifier: + return True + elif compare_str[0] == '@' and compare_str[1:] == self.name: return True else: return False -- cgit