Patchwork D1337: util: add util.clearcachedproperty

login
register
mail settings
Submitter phabricator
Date Nov. 8, 2017, 5:31 p.m.
Message ID <differential-rev-PHID-DREV-oms557fv32ffncgzyvrp-req@phab.mercurial-scm.org>
Download mbox | patch
Permalink /patch/25422/
State Superseded
Headers show

Comments

phabricator - Nov. 8, 2017, 5:31 p.m.
mbthomas created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  This utility function allows clearing of the cached value set up
  @propertycache, if there is one.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D1337

AFFECTED FILES
  mercurial/util.py

CHANGE DETAILS




To: mbthomas, #hg-reviewers
Cc: mercurial-devel
phabricator - Nov. 10, 2017, 5:18 a.m.
indygreg accepted this revision.
indygreg added inline comments.
This revision is now accepted and ready to land.

INLINE COMMENTS

> util.py:936-937
> +    '''clear a cached property value, if one has been set'''
> +    if prop in obj.__dict__:
> +        del obj.__dict__[prop]
> +

This pattern is commonly implemented using a ``try..except KeyError`` around the ``del``. This avoids the double key lookup and I /think/ is faster.

But since this is a super small function and won't be called with high frequency (I assume), it shouldn't be a problem. If a loop were so tight that it mattered, execution time would be dominated by Python function call overhead, not the low-level primitives inside the function.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D1337

To: mbthomas, #hg-reviewers, indygreg
Cc: indygreg, mercurial-devel

Patch

diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -931,6 +931,11 @@ 
         # __dict__ assignment required to bypass __setattr__ (eg: repoview)
         obj.__dict__[self.name] = value
 
+def clearcachedproperty(obj, prop):
+    '''clear a cached property value, if one has been set'''
+    if prop in obj.__dict__:
+        del obj.__dict__[prop]
+
 def pipefilter(s, cmd):
     '''filter string S through command CMD, returning its output'''
     p = subprocess.Popen(cmd, shell=True, close_fds=closefds,