diff mercurial/util.py @ 9102:bbc78cb1bf15

Merge with stable
author Matt Mackall <mpm@selenic.com>
date Thu, 09 Jul 2009 19:49:02 -0500
parents 6cf043b1aa14 431462bd8478
children 54eb3782d32f
line wrap: on
line diff
--- a/mercurial/util.py	Sat Jul 04 14:18:15 2009 +0100
+++ b/mercurial/util.py	Thu Jul 09 19:49:02 2009 -0500
@@ -115,6 +115,33 @@
 
     return f
 
+def lrucachefunc(func):
+    '''cache most recent results of function calls'''
+    cache = {}
+    order = []
+    if func.func_code.co_argcount == 1:
+        def f(arg):
+            if arg not in cache:
+                if len(cache) > 20:
+                    del cache[order.pop(0)]
+                cache[arg] = func(arg)
+            else:
+                order.remove(arg)
+            order.append(arg)
+            return cache[arg]
+    else:
+        def f(*args):
+            if args not in cache:
+                if len(cache) > 20:
+                    del cache[order.pop(0)]
+                cache[args] = func(*args)
+            else:
+                order.remove(args)
+            order.append(args)
+            return cache[args]
+
+    return f
+
 class propertycache(object):
     def __init__(self, func):
         self.func = func