Mercurial > public > mercurial-scm > hg
comparison mercurial/util.py @ 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 | 0916f829eb8d |
children | c848bfd02366 |
comparison
equal
deleted
inserted
replaced
20834:8c210b169c69 | 20835:0e8417131a29 |
---|---|
186 ) | 186 ) |
187 | 187 |
188 def cachefunc(func): | 188 def cachefunc(func): |
189 '''cache the result of function calls''' | 189 '''cache the result of function calls''' |
190 # XXX doesn't handle keywords args | 190 # XXX doesn't handle keywords args |
191 if func.func_code.co_argcount == 0: | |
192 cache = [] | |
193 def f(): | |
194 if len(cache) == 0: | |
195 cache.append(func()) | |
196 return cache[0] | |
197 return f | |
191 cache = {} | 198 cache = {} |
192 if func.func_code.co_argcount == 1: | 199 if func.func_code.co_argcount == 1: |
193 # we gain a small amount of time because | 200 # we gain a small amount of time because |
194 # we don't need to pack/unpack the list | 201 # we don't need to pack/unpack the list |
195 def f(arg): | 202 def f(arg): |