Mercurial > hg
changeset 20835:0e8417131a29
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".
author | FUJIWARA Katsunori <foozy@lares.dti.ne.jp> |
---|---|
date | Sat, 15 Feb 2014 19:52:26 +0900 |
parents | 8c210b169c69 |
children | a8b4541bb961 |
files | mercurial/util.py |
diffstat | 1 files changed, 7 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/mercurial/util.py Wed Mar 26 12:52:57 2014 -0500 +++ b/mercurial/util.py Sat Feb 15 19:52:26 2014 +0900 @@ -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