Skip to content

Commit

Permalink
models.gitrepo: Add some micro-optimizations
Browse files Browse the repository at this point in the history
GitRepoModel._create_dir_entry() gets called many times for large repos so avoid
expensive "." lookups where possible.

GitEntryManager.entry() now uses a local reference to its instance
dictionary to avoid doing extra Python name lookups.

Signed-off-by: David Aguilar <davvid@gmail.com>
  • Loading branch information
davvid committed Aug 9, 2011
1 parent 039b4b2 commit 9961896
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions cola/models/gitrepo.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,16 @@ def _create_dir_entry(self, dirname, direntries):
entries = dirname.split('/')
curdir = []
parent = self.invisibleRootItem()
curdir_append = curdir.append
self_add_directory = self.add_directory
for entry in entries:
curdir.append(entry)
curdir_append(entry)
path = '/'.join(curdir)
if path in direntries:
parent = direntries[path]
else:
grandparent = parent
parent = self.add_directory(grandparent, path)
parent = self_add_directory(grandparent, path)
direntries[path] = parent
return parent

Expand All @@ -185,11 +187,13 @@ class GitRepoEntryManager(object):
static_entries = {}

@classmethod
def entry(cls, path):
def entry(cls, path, _static_entries=static_entries):
"""Return a static instance of a GitRepoEntry."""
if path not in cls.static_entries:
cls.static_entries[path] = GitRepoEntry(path)
return cls.static_entries[path]
try:
e = _static_entries[path]
except KeyError:
e = _static_entries[path] = GitRepoEntry(path)
return e


class GitRepoEntry(QtCore.QObject):
Expand Down

0 comments on commit 9961896

Please sign in to comment.