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

login
register
mail settings
Submitter Katsunori FUJIWARA
Date Feb. 15, 2014, 10:59 a.m.
Message ID <af90654a3bc55a37e922.1392461974@juju>
Download mbox | patch
Permalink /patch/3671/
State Accepted
Commit 0e8417131a29e203936accde7a3ecf4db27cc27e
Headers show

Comments

Katsunori FUJIWARA - Feb. 15, 2014, 10:59 a.m.
# HG changeset patch
# User FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
# Date 1392461546 -32400
#      Sat Feb 15 19:52:26 2014 +0900
# Node ID af90654a3bc55a37e9225eedbfc64008049455b4
# Parent  89fbd429ad6b0b182e593dc39885e405d25762b5
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".

Patch

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