repoview: have unfilteredpropertycache using the underlying cache stable
authorPierre-Yves David <pierre-yves.david@ens-lyon.org>
Mon, 30 Sep 2013 14:23:14 +0200
branchstable
changeset 19846 9789670992d6
parent 19845 a1237a4b437d
child 19847 45c3086880c7
repoview: have unfilteredpropertycache using the underlying cache A `unfilteredpropertycache` is a kind of `propertycache` used on `localrepo` to unsure it will always be run against unfiltered repo and stored only once. As the cached value is never stored in the repoview instance, the descriptor will always be called. Before this patch such calls always result in a call to the `__get__` method of the `propertycache` on the unfiltered repo. That was recomputing a new value on every access through a repoview. We can't prevent the repoview's `unfilteredpropertycache` to get called on every access. In that case the new code makes a standard attribute access to the property. If a value is cached it will be used. The `propertycache` test file have been augmented with test about this issue.
mercurial/localrepo.py
tests/test-propertycache.py
tests/test-propertycache.py.out
--- a/mercurial/localrepo.py	Mon Sep 30 14:36:11 2013 +0200
+++ b/mercurial/localrepo.py	Mon Sep 30 14:23:14 2013 +0200
@@ -39,7 +39,10 @@
     """propertycache that apply to unfiltered repo only"""
 
     def __get__(self, repo, type=None):
-        return super(unfilteredpropertycache, self).__get__(repo.unfiltered())
+        unfi = repo.unfiltered()
+        if unfi is repo:
+            return super(unfilteredpropertycache, self).__get__(unfi)
+        return getattr(unfi, self.name)
 
 class filteredpropertycache(propertycache):
     """propertycache that must take filtering in account"""
--- a/tests/test-propertycache.py	Mon Sep 30 14:36:11 2013 +0200
+++ b/tests/test-propertycache.py	Mon Sep 30 14:23:14 2013 +0200
@@ -24,8 +24,19 @@
     calllog.append(val)
     return val
 
+unficalllog = []
+@mercurial.localrepo.unfilteredpropertycache
+def testcachedunfifoobar(repo):
+    name = repo.filtername
+    if name is None:
+        name = ''
+    val = 100 + len(name)
+    unficalllog.append(val)
+    return val
+
 #plug them on repo
 mercurial.localrepo.localrepository.testcachedfoobar = testcachedfoobar
+mercurial.localrepo.localrepository.testcachedunfifoobar = testcachedunfifoobar
 
 
 # create an empty repo. and instanciate it. It is important to run
@@ -92,3 +103,77 @@
 print 'cached value ("immutable" view):',
 print vars(immutableview).get('testcachedfoobar', 'NOCACHE')
 
+# unfiltered property cache test
+print ''
+print ''
+print '=== unfiltered property cache ==='
+print ''
+print 'unficalllog:', unficalllog
+print 'cached value (unfiltered):      ',
+print vars(repo).get('testcachedunfifoobar', 'NOCACHE')
+print 'cached value ("visible" view):  ',
+print vars(visibleview).get('testcachedunfifoobar', 'NOCACHE')
+print 'cached value ("immutable" view):',
+print vars(immutableview).get('testcachedunfifoobar', 'NOCACHE')
+
+print ''
+print '= first access on unfiltered, should do a call'
+print 'access (unfiltered):', repo.testcachedunfifoobar
+print 'unficalllog:', unficalllog
+print 'cached value (unfiltered):      ',
+print vars(repo).get('testcachedunfifoobar', 'NOCACHE')
+
+print ''
+print '= second access on unfiltered, should not do call'
+print 'access (unfiltered):', repo.testcachedunfifoobar
+print 'unficalllog:', unficalllog
+print 'cached value (unfiltered):      ',
+print vars(repo).get('testcachedunfifoobar', 'NOCACHE')
+
+print ''
+print '= access on view should use the unfiltered cache'
+print 'access (unfiltered):      ', repo.testcachedunfifoobar
+print 'access ("visible" view):  ', visibleview.testcachedunfifoobar
+print 'access ("immutable" view):', immutableview.testcachedunfifoobar
+print 'unficalllog:', unficalllog
+print 'cached value (unfiltered):      ',
+print vars(repo).get('testcachedunfifoobar', 'NOCACHE')
+print 'cached value ("visible" view):  ',
+print vars(visibleview).get('testcachedunfifoobar', 'NOCACHE')
+print 'cached value ("immutable" view):',
+print vars(immutableview).get('testcachedunfifoobar', 'NOCACHE')
+
+print ''
+print '= even if we clear the unfiltered cache'
+del repo.__dict__['testcachedunfifoobar']
+print 'cached value (unfiltered):      ',
+print vars(repo).get('testcachedunfifoobar', 'NOCACHE')
+print 'cached value ("visible" view):  ',
+print vars(visibleview).get('testcachedunfifoobar', 'NOCACHE')
+print 'cached value ("immutable" view):',
+print vars(immutableview).get('testcachedunfifoobar', 'NOCACHE')
+print 'unficalllog:', unficalllog
+print 'access ("visible" view):  ', visibleview.testcachedunfifoobar
+print 'unficalllog:', unficalllog
+print 'cached value (unfiltered):      ',
+print vars(repo).get('testcachedunfifoobar', 'NOCACHE')
+print 'cached value ("visible" view):  ',
+print vars(visibleview).get('testcachedunfifoobar', 'NOCACHE')
+print 'cached value ("immutable" view):',
+print vars(immutableview).get('testcachedunfifoobar', 'NOCACHE')
+print 'access ("immutable" view):', immutableview.testcachedunfifoobar
+print 'unficalllog:', unficalllog
+print 'cached value (unfiltered):      ',
+print vars(repo).get('testcachedunfifoobar', 'NOCACHE')
+print 'cached value ("visible" view):  ',
+print vars(visibleview).get('testcachedunfifoobar', 'NOCACHE')
+print 'cached value ("immutable" view):',
+print vars(immutableview).get('testcachedunfifoobar', 'NOCACHE')
+print 'access (unfiltered):      ', repo.testcachedunfifoobar
+print 'unficalllog:', unficalllog
+print 'cached value (unfiltered):      ',
+print vars(repo).get('testcachedunfifoobar', 'NOCACHE')
+print 'cached value ("visible" view):  ',
+print vars(visibleview).get('testcachedunfifoobar', 'NOCACHE')
+print 'cached value ("immutable" view):',
+print vars(immutableview).get('testcachedunfifoobar', 'NOCACHE')
--- a/tests/test-propertycache.py.out	Mon Sep 30 14:36:11 2013 +0200
+++ b/tests/test-propertycache.py.out	Mon Sep 30 14:23:14 2013 +0200
@@ -34,3 +34,51 @@
 cached value (unfiltered): 0
 cached value ("visible" view): 7
 cached value ("immutable" view): 9
+
+
+=== unfiltered property cache ===
+
+unficalllog: []
+cached value (unfiltered):       NOCACHE
+cached value ("visible" view):   NOCACHE
+cached value ("immutable" view): NOCACHE
+
+= first access on unfiltered, should do a call
+access (unfiltered): 100
+unficalllog: [100]
+cached value (unfiltered):       100
+
+= second access on unfiltered, should not do call
+access (unfiltered): 100
+unficalllog: [100]
+cached value (unfiltered):       100
+
+= access on view should use the unfiltered cache
+access (unfiltered):       100
+access ("visible" view):   100
+access ("immutable" view): 100
+unficalllog: [100]
+cached value (unfiltered):       100
+cached value ("visible" view):   NOCACHE
+cached value ("immutable" view): NOCACHE
+
+= even if we clear the unfiltered cache
+cached value (unfiltered):       NOCACHE
+cached value ("visible" view):   NOCACHE
+cached value ("immutable" view): NOCACHE
+unficalllog: [100]
+access ("visible" view):   100
+unficalllog: [100, 100]
+cached value (unfiltered):       100
+cached value ("visible" view):   NOCACHE
+cached value ("immutable" view): NOCACHE
+access ("immutable" view): 100
+unficalllog: [100, 100]
+cached value (unfiltered):       100
+cached value ("visible" view):   NOCACHE
+cached value ("immutable" view): NOCACHE
+access (unfiltered):       100
+unficalllog: [100, 100]
+cached value (unfiltered):       100
+cached value ("visible" view):   NOCACHE
+cached value ("immutable" view): NOCACHE