Skip to content

Commit 3a95556

Browse files
committed
Some more performance tweaks
1 parent c6fb0be commit 3a95556

4 files changed

Lines changed: 115 additions & 214 deletions

File tree

DynmapCore/src/main/java/org/dynmap/hdmap/HDBlockModel.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ protected HDBlockModel(DynmapBlockState bstate, BitSet databits, String blockset
1919
for (int i = 0; i < bblk.getStateCount(); i++) {
2020
if (databits.isEmpty() || databits.get(i)) {
2121
DynmapBlockState bs = bblk.getState(i);
22-
HDBlockModel prev = HDBlockModels.models_by_id_data.put(bs.globalStateIndex, this);
22+
HDBlockModel prev = HDBlockModels.models_by_id_data[bs.globalStateIndex];
23+
HDBlockModels.models_by_id_data[bs.globalStateIndex] = this;
2324
if((prev != null) && (prev != this)) {
2425
prev.removed(bs);
2526
}

DynmapCore/src/main/java/org/dynmap/hdmap/HDBlockModels.java

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
*/
4242
public class HDBlockModels {
4343
private static int max_patches;
44-
static HashMap<Integer, HDBlockModel> models_by_id_data = new HashMap<Integer, HDBlockModel>();
44+
static HDBlockModel[] models_by_id_data = new HDBlockModel[0];
4545
static PatchDefinitionFactory pdf = new PatchDefinitionFactory();
4646
static BitSet customModelsRequestingTileData = new BitSet(); // Index by globalStateIndex
4747
static BitSet changeIgnoredBlocks = new BitSet(); // Index by globalStateIndex
@@ -53,17 +53,19 @@ public class HDBlockModels {
5353

5454
/* Reset model if defined by different block set */
5555
public static boolean resetIfNotBlockSet(DynmapBlockState blk, String blockset) {
56-
HDBlockModel bm = models_by_id_data.get(blk.globalStateIndex);
56+
int idx = blk.globalStateIndex;
57+
HDBlockModel bm = (idx < models_by_id_data.length) ? models_by_id_data[idx] : null;
5758
if((bm != null) && (bm.getBlockSet().equals(blockset) == false)) {
5859
Debug.debug("Reset block model for " + blk + " from " + bm.getBlockSet() + " due to new def from " + blockset);
59-
models_by_id_data.remove(blk.globalStateIndex);
60+
models_by_id_data[idx] = null;
6061
return true;
6162
}
6263
return false;
6364
}
6465
/* Get texture count needed for model */
6566
public static int getNeededTextureCount(DynmapBlockState blk) {
66-
HDBlockModel bm = models_by_id_data.get(blk.globalStateIndex);
67+
int idx = blk.globalStateIndex;
68+
HDBlockModel bm = (idx < models_by_id_data.length) ? models_by_id_data[idx] : null;
6769
if(bm != null) {
6870
return bm.getTextureCount();
6971
}
@@ -88,18 +90,12 @@ public static void handleBlockAlias() {
8890

8991
private static void remapModel(String bn, String newbn) {
9092
DynmapBlockState frombs = DynmapBlockState.getBaseStateByName(bn);
91-
DynmapBlockState tobs = DynmapBlockState.getBaseStateByName(bn);
93+
DynmapBlockState tobs = DynmapBlockState.getBaseStateByName(newbn);
9294
int fcnt = frombs.getStateCount();
9395
for (int bs = 0; bs < tobs.getStateCount(); bs++) {
9496
DynmapBlockState tb = tobs.getState(bs);
95-
DynmapBlockState fs = tobs.getState(bs % fcnt);
96-
HDBlockModel m = models_by_id_data.get(fs.globalStateIndex);
97-
if (m != null) {
98-
models_by_id_data.put(tb.globalStateIndex, m);
99-
}
100-
else {
101-
models_by_id_data.remove(tb.globalStateIndex);
102-
}
97+
DynmapBlockState fs = frombs.getState(bs % fcnt);
98+
models_by_id_data[tb.globalStateIndex] = models_by_id_data[fs.globalStateIndex];
10399
customModelsRequestingTileData.set(tb.globalStateIndex, customModelsRequestingTileData.get(fs.globalStateIndex));
104100
changeIgnoredBlocks.set(tb.globalStateIndex, changeIgnoredBlocks.get(fs.globalStateIndex));
105101
}
@@ -113,7 +109,7 @@ private static void remapModel(String bn, String newbn) {
113109
public static final String[] getTileEntityFieldsNeeded(DynmapBlockState blk) {
114110
int idx = blk.globalStateIndex;
115111
if(customModelsRequestingTileData.get(idx)) {
116-
HDBlockModel mod = models_by_id_data.get(idx);
112+
HDBlockModel mod = (idx < models_by_id_data.length) ? models_by_id_data[idx] : null;
117113
if(mod instanceof CustomBlockModel) {
118114
return ((CustomBlockModel)mod).render.getTileEntityFieldsNeeded();
119115
}
@@ -166,7 +162,7 @@ public static void loadModels(DynmapCore core, ConfigurationNode config) {
166162
File datadir = core.getDataFolder();
167163
max_patches = 6; /* Reset to default */
168164
/* Reset models-by-ID-Data cache */
169-
models_by_id_data.clear();
165+
models_by_id_data = new HDBlockModel[DynmapBlockState.getGlobalIndexMax()];
170166
/* Reset scaled models by scale cache */
171167
scaled_models_by_scale.clear();
172168
/* Reset change-ignored flags */
@@ -455,7 +451,7 @@ else if (typeid.equals("rotate")) {
455451
Log.severe("Invalid rotate ID: " + bs + " on line " + lineNum + " of file: " + fname);
456452
continue;
457453
}
458-
HDBlockModel mod = models_by_id_data.get(bs.globalStateIndex);
454+
HDBlockModel mod = models_by_id_data[bs.globalStateIndex];
459455
if (modlist.isEmpty()) {
460456
}
461457
else if ((mod != null) && ((rot%90) == 0) && (mod instanceof HDBlockVolumetricModel)) {
@@ -524,7 +520,7 @@ else if (typeid.equals("patchrotate")) {
524520
Log.severe("Invalid patchrotate ID: " + bs + " on line " + lineNum + "of file: " + fname);
525521
continue;
526522
}
527-
HDBlockModel mod = models_by_id_data.get(bs.globalStateIndex);
523+
HDBlockModel mod = models_by_id_data[bs.globalStateIndex];
528524
if (pmodlist.isEmpty()) {
529525
}
530526
else if ((mod != null) && (mod instanceof HDBlockPatchModel)) {

DynmapCore/src/main/java/org/dynmap/hdmap/HDScaledBlockModels.java

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ public HDScaledBlockModels(int scale) {
2121
newcustom = new CustomBlockModel[DynmapBlockState.getGlobalIndexMax()];
2222
custom = newcustom;
2323
}
24-
for(Integer gidx : HDBlockModels.models_by_id_data.keySet()) {
25-
HDBlockModel m = HDBlockModels.models_by_id_data.get(gidx);
26-
24+
for(int gidx = 0; gidx < HDBlockModels.models_by_id_data.length; gidx++) {
25+
HDBlockModel m = HDBlockModels.models_by_id_data[gidx];
26+
if(m == null) continue;
27+
2728
if(m instanceof HDBlockVolumetricModel) {
2829
HDBlockVolumetricModel vm = (HDBlockVolumetricModel)m;
2930
short[] smod = vm.getScaledMap(scale);
@@ -58,37 +59,34 @@ else if(m instanceof CustomBlockModel) {
5859
}
5960

6061
public final short[] getScaledModel(DynmapBlockState blk) {
61-
short[] m = null;
62-
try {
63-
m = modelvectors[blk.globalStateIndex];
64-
} catch (ArrayIndexOutOfBoundsException aioobx) {
65-
short[][] newmodels = new short[blk.globalStateIndex+1][];
62+
int idx = blk.globalStateIndex;
63+
if(idx >= modelvectors.length) {
64+
short[][] newmodels = new short[idx + 1][];
6665
System.arraycopy(modelvectors, 0, newmodels, 0, modelvectors.length);
6766
modelvectors = newmodels;
67+
return null;
6868
}
69-
return m;
69+
return modelvectors[idx];
7070
}
7171
public PatchDefinition[] getPatchModel(DynmapBlockState blk) {
72-
PatchDefinition[] p = null;
73-
try {
74-
p = patches[blk.globalStateIndex];
75-
} catch (ArrayIndexOutOfBoundsException aioobx) {
76-
PatchDefinition[][] newpatches = new PatchDefinition[blk.globalStateIndex+1][];
72+
int idx = blk.globalStateIndex;
73+
if(idx >= patches.length) {
74+
PatchDefinition[][] newpatches = new PatchDefinition[idx + 1][];
7775
System.arraycopy(patches, 0, newpatches, 0, patches.length);
7876
patches = newpatches;
77+
return null;
7978
}
80-
return p;
79+
return patches[idx];
8180
}
82-
81+
8382
public CustomBlockModel getCustomBlockModel(DynmapBlockState blk) {
84-
CustomBlockModel m = null;
85-
try {
86-
m = custom[blk.globalStateIndex];
87-
} catch (ArrayIndexOutOfBoundsException aioobx) {
88-
CustomBlockModel[] newcustom = new CustomBlockModel[blk.globalStateIndex+1];
83+
int idx = blk.globalStateIndex;
84+
if(idx >= custom.length) {
85+
CustomBlockModel[] newcustom = new CustomBlockModel[idx + 1];
8986
System.arraycopy(custom, 0, newcustom, 0, custom.length);
9087
custom = newcustom;
91-
}
92-
return m;
88+
return null;
89+
}
90+
return custom[idx];
9391
}
9492
}

0 commit comments

Comments
 (0)