[PATCH 2 of 5] util: add the code path to "cachefunc()" for the function taking no arguments

FUJIWARA Katsunori foozy at lares.dti.ne.jp
Sat Jan 18 09:23:59 CST 2014


# HG changeset patch
# User FUJIWARA Katsunori <foozy at lares.dti.ne.jp>
# Date 1390058567 -32400
#      Sun Jan 19 00:22:47 2014 +0900
# Node ID 3a11772a30d072c0de2535feac4d9b04e0866041
# Parent  50464e8da22cc03df8c785dab591c88fef276ec4
util: add the code path to "cachefunc()" for the function taking no arguments

Before this patch, "util.cachefunc()" caches the value returned by the
specified function into dictionary "cache", even if the specified
function takes no arguments.

In such case, "cache" has at most one entry, and distinction between
entries in "cache" is meaningless.

This patch adds the code path to "cachefunc()" for the function taking
no arguments for efficiency: to store only one cached value, using
list "cache" is a little faster than using dictionary "cache".

diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -188,6 +188,13 @@
 def cachefunc(func):
     '''cache the result of function calls'''
     # XXX doesn't handle keywords args
+    if func.func_code.co_argcount == 0:
+        cache = []
+        def f():
+            if len(cache) == 0:
+                cache.append(func())
+            return cache[0]
+        return f
     cache = {}
     if func.func_code.co_argcount == 1:
         # we gain a small amount of time because


More information about the Mercurial-devel mailing list