Various performance and concurrency optimizations

This commit is contained in:
Mike Primm 2011-12-10 14:14:52 +08:00 committed by mikeprimm
parent fd887b47b4
commit 05aa0960f2
6 changed files with 115 additions and 74 deletions

View File

@ -2,6 +2,7 @@ package org.dynmap;
import java.io.File;
import java.io.RandomAccessFile;
import java.util.Arrays;
import java.util.LinkedList;
import java.io.IOException;
import java.util.zip.CRC32;
@ -59,26 +60,34 @@ public class TileHashManager {
}
/* Write to file */
public void writeToFile(File tiledir, byte[] crcbuf) {
RandomAccessFile fd = null;
try {
RandomAccessFile fd = new RandomAccessFile(getHashFile(tiledir), "rw");
fd = new RandomAccessFile(getHashFile(tiledir), "rw");
fd.seek(0);
fd.write(crcbuf);
fd.close();
} catch (IOException iox) {
Log.severe("Error writing hash file - " + getHashFile(tiledir).getPath());
} finally {
if(fd != null) {
try { fd.close(); } catch (IOException iox) {}
}
}
}
/* Read from file */
public void readFromFile(File tiledir, byte[] crcbuf) {
RandomAccessFile fd = null;
try {
RandomAccessFile fd = new RandomAccessFile(getHashFile(tiledir), "r");
fd = new RandomAccessFile(getHashFile(tiledir), "r");
fd.seek(0);
fd.read(crcbuf);
fd.close();
} catch (IOException iox) {
Arrays.fill(crcbuf, (byte)0xFF);
writeToFile(tiledir, crcbuf);
} finally {
if(fd != null) {
try { fd.close(); } catch (IOException iox) {}
}
}
}
/* Read CRC */
@ -100,8 +109,8 @@ public class TileHashManager {
private static final int MAX_CACHED_TILEHASHFILES = 25;
private Object lock = new Object();
private LRULinkedHashMap<TileHashFile, byte[]> tilehash = new LRULinkedHashMap<TileHashFile, byte[]>(MAX_CACHED_TILEHASHFILES);
private CRC32 crc32 = new CRC32();
private byte[] crcworkbuf = new byte[8192];
private LinkedList<byte[]> crcworkbufs = new LinkedList<byte[]>();
private LinkedList<CRC32> crcs = new LinkedList<CRC32>();
public TileHashManager(File tileroot, boolean enabled) {
tiledir = tileroot;
@ -130,22 +139,41 @@ public class TileHashManager {
if(!enabled) {
return 0; /* Return value that doesn't match */
}
CRC32 crc32;
byte[] crcworkbuf;
synchronized(lock) {
if(crcworkbuf.length < (4*newbuf.length)){
if(crcworkbufs.isEmpty()) {
crcworkbuf = new byte[4*newbuf.length];
}
for(int i = 0, off = 0; i < newbuf.length; i++) {
int v = newbuf[i];
crcworkbuf[off++] = (byte)v;
crcworkbuf[off++] = (byte)(v>>8);
crcworkbuf[off++] = (byte)(v>>16);
crcworkbuf[off++] = (byte)(v>>24);
else {
crcworkbuf = crcworkbufs.removeFirst();
}
if(crcs.isEmpty()) {
crc32 = new CRC32();
}
else {
crc32 = crcs.removeFirst();
crc32.reset();
}
/* Calculate CRC-32 for buffer */
crc32.reset();
crc32.update(crcworkbuf, 0, 4*newbuf.length);
return crc32.getValue();
}
if(crcworkbuf.length < (4*newbuf.length)){
crcworkbuf = new byte[4*newbuf.length];
}
for(int i = 0, off = 0; i < newbuf.length; i++) {
int v = newbuf[i];
crcworkbuf[off++] = (byte)v;
crcworkbuf[off++] = (byte)(v>>8);
crcworkbuf[off++] = (byte)(v>>16);
crcworkbuf[off++] = (byte)(v>>24);
}
/* Calculate CRC-32 for buffer */
crc32.update(crcworkbuf, 0, 4*newbuf.length);
long v = crc32.getValue();
synchronized(lock) {
crcworkbufs.addFirst(crcworkbuf);
crcs.addFirst(crc32);
}
return v;
}
/* Update hashcode for given tile */
public void updateHashCode(String key, String subtype, int tx, int ty, long newcrc) {

View File

@ -21,8 +21,8 @@ public class DefaultHDLighting implements HDLighting {
/* Apply lighting to given pixel colors (1 outcolor if normal, 2 if night/day) */
public void applyLighting(HDPerspectiveState ps, HDShaderState ss, Color incolor, Color[] outcolor) {
for(Color oc: outcolor)
oc.setColor(incolor);
for(int i = 0; i < outcolor.length; i++)
outcolor[i].setColor(incolor);
}
/* Test if Biome Data is needed for this renderer */

View File

@ -117,8 +117,8 @@ public class DefaultHDShader implements HDShader {
* Reset renderer state for new ray
*/
public void reset(HDPerspectiveState ps) {
for(Color c: color)
c.setTransparent();
for(int i = 0; i < color.length; i++)
color[i].setTransparent();
pixelodd = (ps.getPixelX() & 0x3) + (ps.getPixelY()<<1);
}
@ -169,8 +169,8 @@ public class DefaultHDShader implements HDShader {
lighting.applyLighting(ps, this, c, tmpcolor);
/* If we got alpha from subblock model, use it instead */
if(subalpha >= 0) {
for(Color clr : tmpcolor)
clr.setAlpha(Math.max(subalpha,clr.getAlpha()));
for(int j = 0; j < tmpcolor.length; j++)
tmpcolor[j].setAlpha(Math.max(subalpha,tmpcolor[j].getAlpha()));
}
/* Blend color with accumulated color (weighted by alpha) */
if(!transparency) { /* No transparency support */

View File

@ -145,7 +145,8 @@ public class IsoHDPerspective implements HDPerspective {
BlockStep [] steps = { BlockStep.Y_PLUS, BlockStep.X_MINUS, BlockStep.X_PLUS,
BlockStep.Z_MINUS, BlockStep.Z_PLUS };
emitlevel = skylevel = 0;
for(BlockStep s : steps) {
for(int i = 0; i < steps.length; i++) {
BlockStep s = steps[i];
mapiter.stepPosition(s);
int v = mapiter.getBlockEmittedLight();
if(v > emitlevel) emitlevel = v;
@ -448,7 +449,8 @@ public class IsoHDPerspective implements HDPerspective {
mapiter.getBlockTypeIDAt(BlockStep.X_MINUS) }; /* To north */
int flags = 0;
for(int i = 0; i < 4; i++)
if(ids[i] == REDSTONE_BLKTYPEID) flags |= (1<<i);
if(ids[i] == REDSTONE_BLKTYPEID)
flags |= (1<<i);
switch(flags) {
case 0: /* Nothing nearby */
case 15: /* NSEW */
@ -517,8 +519,8 @@ public class IsoHDPerspective implements HDPerspective {
return blockdata;
}
private final boolean containsID(int id, int[] linkids) {
for(int lid: linkids)
if(id == lid)
for(int i = 0; i < linkids.length; i++)
if(id == linkids[i])
return true;
return false;
}
@ -640,16 +642,8 @@ public class IsoHDPerspective implements HDPerspective {
if(visit_block(mapiter, shaderstate, shaderdone)) {
return;
}
/* If X step is next best */
if((t_next_x <= t_next_y) && (t_next_x <= t_next_z)) {
x += x_inc;
t = t_next_x;
t_next_x += dt_dx;
laststep = stepx;
mapiter.stepPosition(laststep);
}
/* If Y step is next best */
else if((t_next_y <= t_next_x) && (t_next_y <= t_next_z)) {
if((t_next_y <= t_next_x) && (t_next_y <= t_next_z)) {
y += y_inc;
t = t_next_y;
t_next_y += dt_dy;
@ -658,6 +652,14 @@ public class IsoHDPerspective implements HDPerspective {
/* If outside 0-127 range */
if((y & (~0x7F)) != 0) return;
}
/* If X step is next best */
else if((t_next_x <= t_next_y) && (t_next_x <= t_next_z)) {
x += x_inc;
t = t_next_x;
t_next_x += dt_dx;
laststep = stepx;
mapiter.stepPosition(laststep);
}
/* Else, Z step is next best */
else {
z += z_inc;

View File

@ -123,8 +123,8 @@ public class TexturePackHDShader implements HDShader {
* Reset renderer state for new ray
*/
public void reset(HDPerspectiveState ps) {
for(Color c: color)
c.setTransparent();
for(int i = 0; i < color.length; i++)
color[i].setTransparent();
lastblkid = 0;
}

View File

@ -4,6 +4,7 @@ import java.io.File;
import java.io.RandomAccessFile;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.awt.image.BufferedImage;
import java.awt.image.DirectColorModel;
import java.awt.image.WritableRaster;
@ -141,7 +142,7 @@ public class FileLockManager {
}
private static final int MAX_WRITE_RETRIES = 6;
private static ByteArrayOutputStream baos = new ByteArrayOutputStream();
private static LinkedList<ByteArrayOutputStream> baoslist = new LinkedList<ByteArrayOutputStream>();
private static Object baos_lock = new Object();
/**
* Wrapper for IOImage.write - implements retries for busy files
@ -150,45 +151,55 @@ public class FileLockManager {
int retrycnt = 0;
boolean done = false;
byte[] rslt;
ByteArrayOutputStream baos;
synchronized(baos_lock) {
baos.reset();
ImageIO.setUseCache(false); /* Don't use file cache - too small to be worth it */
if(fmt.getFileExt().equals("jpg")) {
WritableRaster raster = img.getRaster();
WritableRaster newRaster = raster.createWritableChild(0, 0, img.getWidth(),
img.getHeight(), 0, 0, new int[] {0, 1, 2});
DirectColorModel cm = (DirectColorModel)img.getColorModel();
DirectColorModel newCM = new DirectColorModel(cm.getPixelSize(),
cm.getRedMask(), cm.getGreenMask(), cm.getBlueMask());
// now create the new buffer that is used ot write the image:
BufferedImage rgbBuffer = new BufferedImage(newCM, newRaster, false, null);
// Find a jpeg writer
ImageWriter writer = null;
Iterator iter = ImageIO.getImageWritersByFormatName("jpg");
if (iter.hasNext()) {
writer = (ImageWriter)iter.next();
}
if(writer == null) {
Log.severe("No JPEG ENCODER - Java VM does not support JPEG encoding");
return;
}
ImageWriteParam iwp = writer.getDefaultWriteParam();
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwp.setCompressionQuality(fmt.getQuality());
ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
writer.setOutput(ios);
writer.write(null, new IIOImage(rgbBuffer, null, null), iwp);
writer.dispose();
rgbBuffer.flush();
if(baoslist.isEmpty()) {
baos = new ByteArrayOutputStream();
}
else {
ImageIO.write(img, fmt.getFileExt(), baos); /* Write to byte array stream - prevent bogus I/O errors */
baos = baoslist.removeFirst();
baos.reset();
}
rslt = baos.toByteArray();
}
ImageIO.setUseCache(false); /* Don't use file cache - too small to be worth it */
if(fmt.getFileExt().equals("jpg")) {
WritableRaster raster = img.getRaster();
WritableRaster newRaster = raster.createWritableChild(0, 0, img.getWidth(),
img.getHeight(), 0, 0, new int[] {0, 1, 2});
DirectColorModel cm = (DirectColorModel)img.getColorModel();
DirectColorModel newCM = new DirectColorModel(cm.getPixelSize(),
cm.getRedMask(), cm.getGreenMask(), cm.getBlueMask());
// now create the new buffer that is used ot write the image:
BufferedImage rgbBuffer = new BufferedImage(newCM, newRaster, false, null);
// Find a jpeg writer
ImageWriter writer = null;
Iterator iter = ImageIO.getImageWritersByFormatName("jpg");
if (iter.hasNext()) {
writer = (ImageWriter)iter.next();
}
if(writer == null) {
Log.severe("No JPEG ENCODER - Java VM does not support JPEG encoding");
return;
}
ImageWriteParam iwp = writer.getDefaultWriteParam();
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwp.setCompressionQuality(fmt.getQuality());
ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
writer.setOutput(ios);
writer.write(null, new IIOImage(rgbBuffer, null, null), iwp);
writer.dispose();
rgbBuffer.flush();
}
else {
ImageIO.write(img, fmt.getFileExt(), baos); /* Write to byte array stream - prevent bogus I/O errors */
}
rslt = baos.toByteArray();
synchronized(baos_lock) {
baoslist.addFirst(baos);
}
while(!done) {
RandomAccessFile f = null;