more C code fixes. Activated caching.
Also removed some code that I accidentially left in. Also added a traceback printing decorator around get_chunk() because the C code can potentially swallow those exceptions.
This commit is contained in:
@@ -55,6 +55,24 @@ def base36encode(number, alphabet='0123456789abcdefghijklmnopqrstuvwxyz'):
|
||||
return "-" + base36
|
||||
return base36
|
||||
|
||||
def log_other_exceptions(func):
|
||||
"""A decorator that prints out any errors that are not ChunkDoesntExist
|
||||
errors. This decorates get_chunk because the C code is likely to swallow
|
||||
exceptions, so this will at least make them visible.
|
||||
|
||||
"""
|
||||
functools.wraps(func)
|
||||
def newfunc(*args):
|
||||
try:
|
||||
return func(*args)
|
||||
except ChunkDoesntExist:
|
||||
raise
|
||||
except Exception, e:
|
||||
logging.exception("%s raised this exception", func.func_name)
|
||||
raise
|
||||
return newfunc
|
||||
|
||||
|
||||
class World(object):
|
||||
"""Encapsulates the concept of a Minecraft "world". A Minecraft world is a
|
||||
level.dat file, a players directory with info about each player, a data
|
||||
@@ -237,7 +255,7 @@ class RegionSet(object):
|
||||
|
||||
# Caching implementaiton: a simple LRU cache
|
||||
# Decorate the get_chunk method with the cache decorator
|
||||
#self.get_chunk = cache.lru_cache(cachesize)(self.get_chunk)
|
||||
self.get_chunk = cache.lru_cache(cachesize)(self.get_chunk)
|
||||
|
||||
# Re-initialize upon unpickling
|
||||
def __getstate__(self):
|
||||
@@ -247,6 +265,7 @@ class RegionSet(object):
|
||||
def __repr__(self):
|
||||
return "<RegionSet regiondir=%r>" % self.regiondir
|
||||
|
||||
@log_other_exceptions
|
||||
def get_chunk(self, x, z):
|
||||
"""Returns a dictionary object representing the "Level" NBT Compound
|
||||
structure for a chunk given its x, z coordinates. The coordinates are
|
||||
@@ -434,83 +453,3 @@ def get_worlds():
|
||||
ret[info['Data']['LevelName']] = info['Data']
|
||||
|
||||
return ret
|
||||
|
||||
|
||||
def lru_cache(maxsize=100):
|
||||
'''Generalized Least-recently-used cache decorator.
|
||||
|
||||
Arguments to the cached function must be hashable.
|
||||
Cache performance statistics stored in f.hits and f.misses.
|
||||
Clear the cache with f.clear().
|
||||
http://en.wikipedia.org/wiki/Cache_algorithms#Least_Recently_Used
|
||||
|
||||
This snippet is from
|
||||
http://code.activestate.com/recipes/498245-lru-and-lfu-cache-decorators/
|
||||
|
||||
'''
|
||||
maxqueue = maxsize * 10
|
||||
def decorating_function(user_function,
|
||||
len=len, iter=iter, tuple=tuple, sorted=sorted, KeyError=KeyError):
|
||||
cache = {} # mapping of args to results
|
||||
queue = collections.deque() # order that keys have been used
|
||||
refcount = collections.defaultdict(int)# times each key is in the queue
|
||||
sentinel = object() # marker for looping around the queue
|
||||
kwd_mark = object() # separate positional and keyword args
|
||||
|
||||
# lookup optimizations (ugly but fast)
|
||||
queue_append, queue_popleft = queue.append, queue.popleft
|
||||
queue_appendleft, queue_pop = queue.appendleft, queue.pop
|
||||
|
||||
@functools.wraps(user_function)
|
||||
def wrapper(*args, **kwds):
|
||||
# cache key records both positional and keyword args
|
||||
key = args
|
||||
if kwds:
|
||||
key += (kwd_mark,) + tuple(sorted(kwds.items()))
|
||||
|
||||
# record recent use of this key
|
||||
queue_append(key)
|
||||
refcount[key] += 1
|
||||
|
||||
# get cache entry or compute if not found
|
||||
try:
|
||||
result = cache[key]
|
||||
wrapper.hits += 1
|
||||
except KeyError:
|
||||
result = user_function(*args, **kwds)
|
||||
cache[key] = result
|
||||
wrapper.misses += 1
|
||||
|
||||
# purge least recently used cache entry
|
||||
if len(cache) > maxsize:
|
||||
key = queue_popleft()
|
||||
refcount[key] -= 1
|
||||
while refcount[key]:
|
||||
key = queue_popleft()
|
||||
refcount[key] -= 1
|
||||
del cache[key], refcount[key]
|
||||
|
||||
# periodically compact the queue by eliminating duplicate keys
|
||||
# while preserving order of most recent access
|
||||
if len(queue) > maxqueue:
|
||||
refcount.clear()
|
||||
queue_appendleft(sentinel)
|
||||
for key in ifilterfalse(refcount.__contains__,
|
||||
iter(queue_pop, sentinel)):
|
||||
queue_appendleft(key)
|
||||
refcount[key] = 1
|
||||
|
||||
|
||||
return result
|
||||
|
||||
def clear():
|
||||
cache.clear()
|
||||
queue.clear()
|
||||
refcount.clear()
|
||||
wrapper.hits = wrapper.misses = 0
|
||||
|
||||
wrapper.hits = wrapper.misses = 0
|
||||
wrapper.clear = clear
|
||||
return wrapper
|
||||
return decorating_function
|
||||
|
||||
|
||||
Reference in New Issue
Block a user