##// END OF EJS Templates
context: use descriptors to speed up lazy attributes
Dirkjan Ochtman -
r7368:595ba253 default
parent child Browse files
Show More
@@ -9,6 +9,15 b' from node import nullid, nullrev, short,'
9 9 from i18n import _
10 10 import ancestor, bdiff, revlog, util, os, errno
11 11
12 class propertycache(object):
13 def __init__(self, func):
14 self.func = func
15 self.name = func.__name__
16 def __get__(self, obj, type=None):
17 result = self.func(obj)
18 setattr(obj, self.name, result)
19 return result
20
12 21 class changectx(object):
13 22 """A changecontext object makes access to data related to a particular
14 23 changeset convenient."""
@@ -51,25 +60,24 b' class changectx(object):'
51 60 def __nonzero__(self):
52 61 return self._rev != nullrev
53 62
54 def __getattr__(self, name):
55 if name == '_changeset':
56 self._changeset = self._repo.changelog.read(self.node())
57 return self._changeset
58 elif name == '_manifest':
59 self._manifest = self._repo.manifest.read(self._changeset[0])
60 return self._manifest
61 elif name == '_manifestdelta':
62 md = self._repo.manifest.readdelta(self._changeset[0])
63 self._manifestdelta = md
64 return self._manifestdelta
65 elif name == '_parents':
66 p = self._repo.changelog.parentrevs(self._rev)
67 if p[1] == nullrev:
68 p = p[:-1]
69 self._parents = [changectx(self._repo, x) for x in p]
70 return self._parents
71 else:
72 raise AttributeError(name)
63 def _changeset(self):
64 return self._repo.changelog.read(self.node())
65 _changeset = propertycache(_changeset)
66
67 def _manifest(self):
68 return self._repo.manifest.read(self._changeset[0])
69 _manifest = propertycache(_manifest)
70
71 def _manifestdelta(self):
72 return self._repo.manifest.readdelta(self._changeset[0])
73 _manifestdelta = propertycache(_manifestdelta)
74
75 def _parents(self):
76 p = self._repo.changelog.parentrevs(self._rev)
77 if p[1] == nullrev:
78 p = p[:-1]
79 return [changectx(self._repo, x) for x in p]
80 _parents = propertycache(_parents)
73 81
74 82 def __contains__(self, key):
75 83 return key in self._manifest
@@ -193,33 +201,35 b' class filectx(object):'
193 201 if fileid is not None:
194 202 self._fileid = fileid
195 203
196 def __getattr__(self, name):
197 if name == '_changectx':
198 self._changectx = changectx(self._repo, self._changeid)
199 return self._changectx
200 elif name == '_filelog':
201 self._filelog = self._repo.file(self._path)
202 return self._filelog
203 elif name == '_changeid':
204 if '_changectx' in self.__dict__:
205 self._changeid = self._changectx.rev()
206 else:
207 self._changeid = self._filelog.linkrev(self._filerev)
208 return self._changeid
209 elif name == '_filenode':
210 if '_fileid' in self.__dict__:
211 self._filenode = self._filelog.lookup(self._fileid)
212 else:
213 self._filenode = self._changectx.filenode(self._path)
214 return self._filenode
215 elif name == '_filerev':
216 self._filerev = self._filelog.rev(self._filenode)
217 return self._filerev
218 elif name == '_repopath':
219 self._repopath = self._path
220 return self._repopath
204 def _changectx(self):
205 return changectx(self._repo, self._changeid)
206 _changectx = propertycache(_changectx)
207
208 def _filelog(self):
209 return self._repo.file(self._path)
210 _filelog = propertycache(_filelog)
211
212 def _changeid(self):
213 if '_changectx' in self.__dict__:
214 return self._changectx.rev()
221 215 else:
222 raise AttributeError(name)
216 return self._filelog.linkrev(self._filerev)
217 _changeid = propertycache(_changeid)
218
219 def _filenode(self):
220 if '_fileid' in self.__dict__:
221 return self._filelog.lookup(self._fileid)
222 else:
223 return self._changectx.filenode(self._path)
224 _filenode = propertycache(_filenode)
225
226 def _filerev(self):
227 return self._filelog.rev(self._filenode)
228 _filerev = propertycache(_filerev)
229
230 def _repopath(self):
231 return self._path
232 _repopath = propertycache(_repopath)
223 233
224 234 def __nonzero__(self):
225 235 try:
@@ -505,29 +515,7 b' class workingctx(changectx):'
505 515 def __contains__(self, key):
506 516 return self._dirstate[key] not in "?r"
507 517
508 def __getattr__(self, name):
509 if name == '_status':
510 self._status = self._repo.status(unknown=True)
511 return self._status
512 elif name == '_user':
513 self._user = self._repo.ui.username()
514 return self._user
515 elif name == '_date':
516 self._date = util.makedate()
517 return self._date
518 if name == '_manifest':
519 self._buildmanifest()
520 return self._manifest
521 elif name == '_parents':
522 p = self._repo.dirstate.parents()
523 if p[1] == nullid:
524 p = p[:-1]
525 self._parents = [changectx(self._repo, x) for x in p]
526 return self._parents
527 else:
528 raise AttributeError(name)
529
530 def _buildmanifest(self):
518 def _manifest(self):
531 519 """generate a manifest corresponding to the working directory"""
532 520
533 521 man = self._parents[0].manifest().copy()
@@ -547,7 +535,28 b' class workingctx(changectx):'
547 535 if f in man:
548 536 del man[f]
549 537
550 self._manifest = man
538 return man
539 _manifest = propertycache(_manifest)
540
541 def _status(self):
542 return self._repo.status(unknown=True)
543 _status = propertycache(_status)
544
545 def _user(self):
546 return self._repo.ui.username()
547 _user = propertycache(_user)
548
549 def _date(self):
550 return util.makedate()
551 _date = propertycache(_date)
552
553 def _parents(self):
554 p = self._repo.dirstate.parents()
555 if p[1] == nullid:
556 p = p[:-1]
557 self._parents = [changectx(self._repo, x) for x in p]
558 return self._parents
559 _parents = propertycache(_parents)
551 560
552 561 def manifest(self): return self._manifest
553 562
@@ -622,19 +631,17 b' class workingfilectx(filectx):'
622 631 if workingctx:
623 632 self._changectx = workingctx
624 633
625 def __getattr__(self, name):
626 if name == '_changectx':
627 self._changectx = workingctx(self._repo)
628 return self._changectx
629 elif name == '_repopath':
630 self._repopath = (self._repo.dirstate.copied(self._path)
631 or self._path)
632 return self._repopath
633 elif name == '_filelog':
634 self._filelog = self._repo.file(self._repopath)
635 return self._filelog
636 else:
637 raise AttributeError(name)
634 def _changectx(self):
635 return workingctx(self._repo)
636 _changectx = propertycache(_changectx)
637
638 def _repopath(self):
639 return self._repo.dirstate.copied(self._path) or self._path
640 _repopath = propertycache(_repopath)
641
642 def _filelog(self):
643 return self._repo.file(self._repopath)
644 _filelog = propertycache(_filelog)
638 645
639 646 def __nonzero__(self):
640 647 return True
General Comments 0
You need to be logged in to leave comments. Login now