Introduced map util and now the Loading does not create new String object.

This commit is contained in:
Leonardo
2021-09-11 15:56:46 +02:00
parent 1395e32a50
commit bdcc4c7755
6 changed files with 149 additions and 64 deletions
@@ -10,11 +10,6 @@ public interface LevelContainer
{
public static final char VERTICAL_DATA_DELIMITER = '\t';
public static final char DATA_DELIMITER = ',';
public static final ConcurrentMap<String,long[]> threadAddDataMap = new ConcurrentHashMap();
public static final ConcurrentMap<String,long[]> threadGetDataMap = new ConcurrentHashMap();
public static final ConcurrentMap<String,long[]> threadSingleUpdateMap = new ConcurrentHashMap();
public static final ConcurrentMap<String,long[][][]> threadVerticalUpdateMap = new ConcurrentHashMap();
public static final ConcurrentMap<String,int[]> threadVerticalIndexesMap = new ConcurrentHashMap();
/**With this you can add data to the level container
*
* @param data actual data to add in a array of long format.
@@ -25,10 +25,7 @@ import java.util.concurrent.Executors;
import com.seibel.lod.config.LodConfig;
import com.seibel.lod.enums.DistanceGenerationMode;
import com.seibel.lod.handlers.LodDimensionFileHandler;
import com.seibel.lod.util.DetailDistanceUtil;
import com.seibel.lod.util.LevelPosUtil;
import com.seibel.lod.util.LodThreadFactory;
import com.seibel.lod.util.LodUtil;
import com.seibel.lod.util.*;
import com.seibel.lod.wrappers.MinecraftWrapper;
import net.minecraft.util.math.ChunkPos;
@@ -446,12 +443,7 @@ public class LodDimension
LodRegion region = getRegion(regionPosX, regionPosZ);
if (region == null)
return false;
;
if(!LevelContainer.threadAddDataMap.containsKey(Thread.currentThread().getName()) || (LevelContainer.threadAddDataMap.get(Thread.currentThread().getName()) == null))
{
LevelContainer.threadAddDataMap.put(Thread.currentThread().getName(), new long[10]);
}
long[] dataArray = LevelContainer.threadAddDataMap.get(Thread.currentThread().getName());
long[] dataArray = ThreadMapUtil.getSingleAddDataArray();
dataArray[0] = lodDataPoint;
boolean nodeAdded = region.addData(detailLevel, posX, posZ, dataArray, serverQuality);
// only save valid LODs to disk
@@ -2,10 +2,7 @@ package com.seibel.lod.objects;
import com.seibel.lod.builders.LodBuilder;
import com.seibel.lod.enums.DistanceGenerationMode;
import com.seibel.lod.util.DataPointUtil;
import com.seibel.lod.util.DetailDistanceUtil;
import com.seibel.lod.util.LevelPosUtil;
import com.seibel.lod.util.LodUtil;
import com.seibel.lod.util.*;
public class SingleLevelContainer implements LevelContainer
{
@@ -35,20 +32,16 @@ public class SingleLevelContainer implements LevelContainer
data[posX][posZ] = newData;
return true;
}
public long[] getData(int posX, int posZ){
if(!LevelContainer.threadGetDataMap.containsKey(Thread.currentThread().getName()) || (LevelContainer.threadGetDataMap.get(Thread.currentThread().getName()) == null))
{
LevelContainer.threadGetDataMap.put(Thread.currentThread().getName(), new long[1]);
}
long[] dataArray = LevelContainer.threadGetDataMap.get(Thread.currentThread().getName());
long[] dataArray = ThreadMapUtil.getSingleGetDataArray();
posX = LevelPosUtil.getRegionModule(detailLevel, posX);
posZ = LevelPosUtil.getRegionModule(detailLevel, posZ);
//Improve this using a thread map to long[]
dataArray[0] = data[posX][posZ];
return dataArray;
}
private long getSingleData(int posX, int posZ){
posX = LevelPosUtil.getRegionModule(detailLevel, posX);
posZ = LevelPosUtil.getRegionModule(detailLevel, posZ);
@@ -66,23 +59,35 @@ public class SingleLevelContainer implements LevelContainer
public SingleLevelContainer(String inputString)
{
int tempIndex;
int shift = 0;
int index = 0;
int lastIndex = 0;
index = inputString.indexOf(DATA_DELIMITER, 0);
detailLevel = (byte) Integer.parseInt(inputString.substring(0, index));
int digit;
char currentChar;
long newData;
currentChar = inputString.charAt(index);
digit = Character.digit(currentChar,16);
detailLevel = (byte) digit;
size = (int) Math.pow(2, LodUtil.REGION_DETAIL_LEVEL - detailLevel);
this.data = new long[size][size];
for (int x = 0; x < size; x++)
{
for (int z = 0; z < size; z++)
{
lastIndex = index;
index = inputString.indexOf(DATA_DELIMITER, lastIndex + 1);
data[x][z] = Long.parseLong(inputString.substring(lastIndex + 1, index), 16);
newData = 0;
for(tempIndex = 0; tempIndex < 16; tempIndex++)
{
currentChar = inputString.charAt(index+tempIndex);
if(currentChar == ','){
break;
}
shift = (15-tempIndex)*4;
digit = Character.digit(currentChar,16);
newData += ((long) (digit & 0xf)) << shift;
}
newData = newData >>> (shift);
data[x][z] = newData;
index = index + tempIndex;
}
}
}
@@ -90,11 +95,7 @@ public class SingleLevelContainer implements LevelContainer
public void updateData(LevelContainer lowerLevelContainer, int posX, int posZ)
{
//We reset the array
if(!LevelContainer.threadGetDataMap.containsKey(Thread.currentThread().getName()) || (LevelContainer.threadGetDataMap.get(Thread.currentThread().getName()) == null))
{
LevelContainer.threadGetDataMap.put(Thread.currentThread().getName(), new long[4]);
}
long[] dataToMerge = LevelContainer.threadGetDataMap.get(Thread.currentThread().getName());
long[] dataToMerge = ThreadMapUtil.getSingleUpdateArray();
int childPosX;
int childPosZ;
@@ -1,9 +1,9 @@
package com.seibel.lod.objects;
/*
import com.seibel.lod.util.DataPointUtil;
import com.seibel.lod.util.LevelPosUtil;
import com.seibel.lod.util.LodUtil;
/*
public class VerticalLevelContainer implements LevelContainer
{
@@ -75,6 +75,33 @@ public class VerticalLevelContainer implements LevelContainer
return new SingleLevelContainer((byte) (getDetailLevel() - 1));
}
public void updateData(LevelContainer lowerLevelContainer, int posX, int posZ)
{
//We reset the array
if(!LevelContainer.threadGetDataMap.containsKey(Thread.currentThread().getName()) || (LevelContainer.threadGetDataMap.get(Thread.currentThread().getName()) == null))
{
LevelContainer.threadGetDataMap.put(Thread.currentThread().getName(), new long[4]);
}
long[] dataToMerge = LevelContainer.threadGetDataMap.get(Thread.currentThread().getName());
int childPosX;
int childPosZ;
long data = 0;
posX = LevelPosUtil.getRegionModule(detailLevel, posX);
posZ = LevelPosUtil.getRegionModule(detailLevel, posZ);
for (int x = 0; x <= 1; x++)
{
for (int z = 0; z <= 1; z++)
{
childPosX = 2 * posX + x;
childPosZ = 2 * posZ + z;
dataToMerge[2*z + x] = lowerLevelContainer.getData(childPosX, childPosZ)[0];
}
}
data = DataPointUtil.mergeSingleData(dataToMerge);
addData(data,posX,posZ);
}
public void updateData(LevelContainer lowerLevelContainer, int posX, int posZ)
{
long[][][] updateTemps;