[PATCH 2 of 2] util: rename lrucachedict._capacity to capacity

Augie Fackler raf at durin42.com
Wed Jan 6 18:38:21 CST 2016


> On Jan 6, 2016, at 7:37 PM, Gregory Szorc <gregory.szorc at gmail.com> wrote:
> 
> On Wed, Jan 6, 2016 at 4:32 PM, Augie Fackler <raf at durin42.com <mailto:raf at durin42.com>> wrote:
> On Tue, Jan 05, 2016 at 09:18:13PM -0800, Gregory Szorc wrote:
> > # HG changeset patch
> > # User Gregory Szorc <gregory.szorc at gmail.com <mailto:gregory.szorc at gmail.com>>
> > # Date 1452057366 28800
> > #      Tue Jan 05 21:16:06 2016 -0800
> > # Node ID 8ef96a23b22f43e3a1bde06f3f5c1468a62cc4c9
> > # Parent  de3fa1bfeec15608289d07fc45382d0d62800440
> > util: rename lrucachedict._capacity to capacity
> >
> > Make it a "public" attribute so others can use it.
> 
> Why? I'm not clear how this is useful?
> 
> Actually, I think I have a better API in mind for my use case. Stay tuned.
> 
> Could you queue part 1 and drop this one?

Already done.

> 
> 
> >
> > diff --git a/mercurial/util.py b/mercurial/util.py
> > --- a/mercurial/util.py
> > +++ b/mercurial/util.py
> > @@ -535,25 +535,28 @@ class lrucachedict(object):
> >      The dict consists of an actual backing dict - indexed by original
> >      key - and a doubly linked circular list defining the order of entries in
> >      the cache.
> >
> >      The head node is the newest entry in the cache. If the cache is full,
> >      we recycle head.prev and make it the new head. Cache accesses result in
> >      the node being moved to before the existing head and being marked as the
> >      new head node.
> > +
> > +    Changing the ``.capacity`` of the cache after instantiation results in
> > +    undefined behavior.
> 
> Maybe expose it as an @property then, so that it's read-only?
> 
> @property
> def capacity(self):
>   return self._capacity
> 
> or similar.
> 
> >      """
> >      def __init__(self, max):
> >          self._cache = {}
> >
> >          self._head = head = _lrucachenode()
> >          head.prev = head
> >          head.next = head
> >          self._size = 1
> > -        self._capacity = max
> > +        self.capacity = max
> >
> >      def __len__(self):
> >          return len(self._cache)
> >
> >      def __contains__(self, k):
> >          return k in self._cache
> >
> >      def __iter__(self):
> > @@ -571,17 +574,17 @@ class lrucachedict(object):
> >      def __setitem__(self, k, v):
> >          node = self._cache.get(k)
> >          # Replace existing value and mark as newest.
> >          if node is not None:
> >              node.value = v
> >              self._movetohead(node)
> >              return
> >
> > -        if self._size < self._capacity:
> > +        if self._size < self.capacity:
> >              node = self._addcapacity()
> >          else:
> >              # Grab the last/oldest item.
> >              node = self._head.prev
> >
> >          # At capacity. Kill the old entry.
> >          if node.key is not _notset:
> >              del self._cache[node.key]
> > @@ -614,17 +617,17 @@ class lrucachedict(object):
> >          n = self._head
> >          while n.key is not _notset:
> >              n.markempty()
> >              n = n.next
> >
> >          self._cache.clear()
> >
> >      def copy(self):
> > -        result = lrucachedict(self._capacity)
> > +        result = lrucachedict(self.capacity)
> >          n = self._head.prev
> >          # Iterate in oldest-to-newest order, so the copy has the right ordering
> >          for i in range(len(self._cache)):
> >              result[n.key] = n.value
> >              n = n.prev
> >          return result
> >
> >      def _movetohead(self, node):
> > _______________________________________________
> > Mercurial-devel mailing list
> > Mercurial-devel at selenic.com <mailto:Mercurial-devel at selenic.com>
> > https://selenic.com/mailman/listinfo/mercurial-devel <https://selenic.com/mailman/listinfo/mercurial-devel>
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://selenic.com/pipermail/mercurial-devel/attachments/20160106/b22e1510/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <http://selenic.com/pipermail/mercurial-devel/attachments/20160106/b22e1510/attachment.pgp>


More information about the Mercurial-devel mailing list