performance improvements

Replaced server member lists, channel lists, and private channel lists
with dicts. This allows O(1) lookups and removes (previously it would be
an O(N) operation to lookup or remove). I did pretty extensive testing
and benchmarking to compare the performance of using lists vs using
dicts. Iterating through lists to find an item is only faster in the
average case for extremely small lists (less than 3 items). For 100
items, using a dict is about 10 times faster on average (and about 100
times faster for 1000 items). The overhead in dicts is in memory usage
and initial creation time. Creating and populating a dict is about 2 to
3 times slower than creating and appending items to a list. However this
cost is still tiny.  For 1000 items this equates to about a 70
microsecond difference (on an i7 CPU) for populating the entire dict.
The memory overhead for a dict (compared to a list) is about 25-60 KB
per 1000 items (can vary depending on dict resizing).

Originally I wanted to use OrderedDicts to presereve order, but in my
testing OrderedDicts have about 6x the memory overhead compared to
normal dicts.
This commit is contained in:
Steven Berler
2016-01-01 04:58:47 -08:00
committed by Rapptz
parent 25588955e4
commit 10b0b62f50
4 changed files with 120 additions and 74 deletions

View File

@ -122,13 +122,13 @@ class Message:
if self.channel is not None:
for mention in mentions:
id_search = mention.get('id')
member = utils.find(lambda m: m.id == id_search, self.server.members)
member = self.server.get_member(id_search)
if member is not None:
self.mentions.append(member)
if self.server is not None:
for mention in self.raw_channel_mentions:
channel = utils.find(lambda m: m.id == mention, self.server.channels)
channel = self.server.get_channel(mention)
if channel is not None:
self.channel_mentions.append(channel)
@ -191,6 +191,6 @@ class Message:
if not self.channel.is_private:
self.server = self.channel.server
found = utils.find(lambda m: m.id == self.author.id, self.server.members)
found = self.server.get_member(self.author.id)
if found is not None:
self.author = found