From 8ec7986353cb16764a0fd3ab2dee54b17c7ddb35 Mon Sep 17 00:00:00 2001 From: Ryan McCue Date: Mon, 13 Dec 2010 20:34:17 +1000 Subject: [PATCH] Only pass the part of the dict we need to the chunk renderer --- chunk.py | 14 +++++++------- world.py | 19 ++++++++++++++----- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/chunk.py b/chunk.py index 6af295c..8701fc4 100644 --- a/chunk.py +++ b/chunk.py @@ -112,12 +112,12 @@ def iterate_chunkblocks(xoff,yoff): transparent_blocks = set([0, 6, 8, 9, 18, 20, 37, 38, 39, 40, 44, 50, 51, 52, 53, 59, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 74, 75, 76, 77, 78, 79, 81, 83, 85]) -def render_and_save(chunkfile, cachedir, worldobj, cave=False, queue=None): +def render_and_save(chunkfile, cachedir, worldobj, cached, cave=False, queue=None): """Used as the entry point for the multiprocessing workers (since processes can't target bound methods) or to easily render and save one chunk Returns the image file location""" - a = ChunkRenderer(chunkfile, cachedir, worldobj, queue) + a = ChunkRenderer(chunkfile, cachedir, worldobj, cached, queue) try: return a.render_and_save(cave) except ChunkCorrupt: @@ -140,7 +140,7 @@ class ChunkCorrupt(Exception): pass class ChunkRenderer(object): - def __init__(self, chunkfile, cachedir, worldobj, queue): + def __init__(self, chunkfile, cachedir, cached, worldobj, queue): """Make a new chunk renderer for the given chunkfile. chunkfile should be a full path to the .dat file to process cachedir is a directory to save the resulting chunk images to @@ -169,7 +169,7 @@ class ChunkRenderer(object): moredirs, dir2 = os.path.split(destdir) _, dir1 = os.path.split(moredirs) self.cachedir = os.path.join(cachedir, dir1, dir2) - self.dirbits = (dir1, dir2) + self.cached = cached if self.world.useBiomeData: @@ -304,9 +304,9 @@ class ChunkRenderer(object): def find_oldimage(self, cave): # Get the name of the existing image. oldimg = oldimg_path = None - key = ".".join((self.dirbits[0], self.dirbits[1], self.blockid, "cave" if cave else "nocave")) - if key in self.world.cached: - oldimg_path = self.world.cached[key] + key = ".".join((self.blockid, "cave" if cave else "nocave")) + if key in self.cached: + oldimg_path = self.cached[key] _, oldimg = os.path.split(oldimg_path) logging.debug("Found cached image {0}".format(oldimg)) return oldimg, oldimg_path diff --git a/world.py b/world.py index 17b9ef6..c0aaa41 100644 --- a/world.py +++ b/world.py @@ -36,6 +36,7 @@ and for extracting information about available worlds """ base36decode = functools.partial(int, base=36) +cached = collections.defaultdict(dict) def _convert_coords(chunks): @@ -108,7 +109,6 @@ class WorldRenderer(object): textures.prepareBiomeData(worlddir) self.chunklist = chunklist - self.cached = collections.defaultdict(dict) # In order to avoid having to look up the cache file names in # ChunkRenderer, get them all and store them here @@ -119,8 +119,9 @@ class WorldRenderer(object): dirname, dir_b = os.path.split(root) _, dir_a = os.path.split(dirname) _, x, z, cave, _ = filename.split('.', 4) - bits = '.'.join((dir_a, dir_b, x, z, cave)) - self.cached[bits] = os.path.join(root, filename) + dir = '/'.join((dir_a, dir_b)) + bits = '.'.join((x, z, cave)) + cached[dir][bits] = os.path.join(root, filename) # stores Points Of Interest to be mapped with markers @@ -294,7 +295,11 @@ class WorldRenderer(object): results[(col, row)] = imgpath continue - result = chunk.render_and_save(chunkfile, self.cachedir, self, cave=self.caves, queue=q) + moredirs, dir2 = os.path.split(os.path.dirname(self.chunkfile)) + dir1 = os.path.basename(moredirs) + cachename = '/'.join(dir1, dir2) + + result = chunk.render_and_save(chunkfile, self.cachedir, self, cached[chunkname], queue=q) results[(col, row)] = result if i > 0: try: @@ -320,8 +325,12 @@ class WorldRenderer(object): results[(col, row)] = imgpath continue + moredirs, dir2 = os.path.split(os.path.dirname(self.chunkfile)) + dir1 = os.path.basename(moredirs) + cachename = '/'.join(dir1, dir2) + result = pool.apply_async(chunk.render_and_save, - args=(chunkfile,self.cachedir,self), + args=(chunkfile,self.cachedir,self, cached[chunkname]), kwds=dict(cave=self.caves, queue=q)) asyncresults.append((col, row, result))