[PATCH 3 of 3] filectx.annotate: return filectx for each line instead of rev

Brendan Cully brendan at kublai.com
Fri Sep 22 04:53:57 UTC 2006


On Thursday, 21 September 2006 at 23:27, Matt Mackall wrote:
> On Thu, Sep 21, 2006 at 07:23:11PM -0700, Brendan Cully wrote:
> > +    def getname(fctx):
> > +        return trimuser(ui, fctx.user(), fctx.rev(), ucache)
> 
> I think this can just be ui.shortname(fctx.user()). ucache can go.
>
> >      dcache = {}
> > -    def getdate(rev):
> > -        datestr = dcache.get(rev)
> > +    def getdate(fctx):
> > +        datestr = dcache.get(fctx)
> >          if datestr is None:
> > -            datestr = dcache[rev] = util.datestr(repo.changectx(rev).date())
> > +            datestr = dcache[fctx] = util.datestr(fctx.date())
> 
> And here, I think we can skip the dcache too.
> 
> > -    opmap = [['user', getname], ['number', str], ['changeset', getnode],
> > +    opmap = [['user', getname], ['number', lambda x: str(x.rev())],
> > +             ['changeset', lambda x: short(x.node())],
> >               ['date', getdate]]
> 
> Which means they can be more lambdas here.

ok. It probably saves a small amount of processing to cache it, but I
haven't measured it. Annotate itself should dwarf this, I suppose. The
extra code isn't much though...

> >      if not opts['user'] and not opts['changeset'] and not opts['date']:
> >          opts['number'] = 1
> > diff -r 0b94209691a4 -r 9470e405efd6 mercurial/context.py
> > +++ b/mercurial/context.py	Thu Sep 21 19:21:01 2006 -0700
> > @@ -138,4 +138,15 @@ class filectx(object):
> >                           filelog=self._filelog) for x in c ]
> >  
> >      def annotate(self):
> > -        return self._filelog.annotate(self._filenode)
> > +        fcache = {}
> > +        def getctx(rev):
> > +            ctx = fcache.get(rev)
> > +            if not ctx:
> > +                ctx = filectx(self._repo, self._path, changeid=rev,
> > +                              filelog=self._filelog)
> > +                fcache[rev] = ctx
> > +            return ctx
> 
> I think the best pattern here is:
> 
> if x not in cache:
>   cache[x] = func(x)
> y = cache[x]
> 
> Another pattern is:
> 
> try:
>   y = cache[x]
> except:
>   cache[x] = func(x)
>   y = cache[x]
> 
> ..which may win if x is usually in cache.

ok.



More information about the Mercurial mailing list