Some performance or memory optimizations

Replaced String concat in loops with StringBuilder (performance)
Replaced for with keySet + Map.get() through for with entrySet (perf)
Changed inner classes to static classes, mostly struct like (memory)
This commit is contained in:
yueh
2014-09-30 22:59:49 +02:00
parent d8da97d2af
commit 7fa7e43c29
25 changed files with 86 additions and 80 deletions
@@ -31,10 +31,11 @@ public class ShapedRecipe implements IRecipe, IRecipeBakeable
return !disable;
}
public ShapedRecipe(ItemStack result, Object... recipe) {
public ShapedRecipe(ItemStack result, Object... recipe)
{
output = result.copy();
String shape = "";
StringBuilder shape = new StringBuilder();
int idx = 0;
if ( recipe[idx] instanceof Boolean )
@@ -57,7 +58,7 @@ public class ShapedRecipe implements IRecipe, IRecipeBakeable
for (String s : parts)
{
width = s.length();
shape += s;
shape.append( s );
}
height = parts.length;
@@ -67,7 +68,7 @@ public class ShapedRecipe implements IRecipe, IRecipeBakeable
while (recipe[idx] instanceof String)
{
String s = (String) recipe[idx++];
shape += s;
shape.append( s );
width = s.length();
height++;
}
@@ -75,13 +76,13 @@ public class ShapedRecipe implements IRecipe, IRecipeBakeable
if ( width * height != shape.length() )
{
String ret = "Invalid shaped ore recipe: ";
StringBuilder ret = new StringBuilder( "Invalid shaped ore recipe: " );
for (Object tmp : recipe)
{
ret += tmp + ", ";
ret.append( tmp + ", " );
}
ret += output;
throw new RuntimeException( ret );
ret.append( output );
throw new RuntimeException( ret.toString() );
}
HashMap<Character, Object> itemMap = new HashMap<Character, Object>();
@@ -97,19 +98,19 @@ public class ShapedRecipe implements IRecipe, IRecipeBakeable
}
else
{
String ret = "Invalid shaped ore recipe: ";
StringBuilder ret = new StringBuilder( "Invalid shaped ore recipe: " );
for (Object tmp : recipe)
{
ret += tmp + ", ";
ret.append( tmp + ", " );
}
ret += output;
throw new RuntimeException( ret );
ret.append( output );
throw new RuntimeException( ret.toString() );
}
}
input = new Object[width * height];
int x = 0;
for (char chr : shape.toCharArray())
for (char chr : shape.toString().toCharArray())
{
input[x++] = itemMap.get( chr );
}
@@ -24,7 +24,8 @@ public class ShapelessRecipe implements IRecipe, IRecipeBakeable
return !disable;
}
public ShapelessRecipe(ItemStack result, Object... recipe) {
public ShapelessRecipe(ItemStack result, Object... recipe)
{
output = result.copy();
for (Object in : recipe)
{
@@ -34,13 +35,13 @@ public class ShapelessRecipe implements IRecipe, IRecipeBakeable
}
else
{
String ret = "Invalid shapeless ore recipe: ";
StringBuilder ret = new StringBuilder( "Invalid shapeless ore recipe: " );
for (Object tmp : recipe)
{
ret += tmp + ", ";
ret.append( tmp + ", " );
}
ret += output;
throw new RuntimeException( ret );
ret.append( output );
throw new RuntimeException( ret.toString() );
}
}
}
@@ -64,21 +64,21 @@ public class Shaped implements ICraftHandler, IWebsiteSerializer
for (int y = 0; y < rows; y++)
{
String row = "";
StringBuilder row = new StringBuilder();
for (int x = 0; x < cols; x++)
{
if ( inputs.get( y ).get( x ).isAir() )
row = row + " ";
row.append( " " );
else
{
row = row + first;
row.append( first );
args.add( first );
args.add( inputs.get( y ).get( x ) );
first++;
}
}
args.add( y, row );
args.add( y, row.toString() );
}
ItemStack outIS = output.getItemStack();
@@ -57,9 +57,10 @@ public class Shapeless implements ICraftHandler, IWebsiteSerializer
throw new RegistrationError( "Error while adding shapeless recipe." );
}
}
@Override
public boolean canCraft(ItemStack reqOutput) throws RegistrationError, MissingIngredientError {
public boolean canCraft(ItemStack reqOutput) throws RegistrationError, MissingIngredientError
{
for (IIngredient i : inputs)
{
@@ -74,26 +75,27 @@ public class Shapeless implements ICraftHandler, IWebsiteSerializer
}
}
}
return Platform.isSameItemPrecise( output.getItemStack(),reqOutput );
return Platform.isSameItemPrecise( output.getItemStack(), reqOutput );
}
@Override
public String getPattern( RecipeHandler h) {
String o = "shapeless "+output.getQty()+"\n";
public String getPattern(RecipeHandler h)
{
StringBuilder o = new StringBuilder( "shapeless " + output.getQty() + "\n" );
o += h.getName(output)+"\n";
for ( int y = 0; y < inputs.size(); y++ )
o.append( h.getName( output ) + "\n" );
for (int y = 0; y < inputs.size(); y++)
{
IIngredient i = inputs.get(y);
IIngredient i = inputs.get( y );
if ( i.isAir() )
o += "air"+( y +1 == inputs.size() ? "\n" : " " );
o.append( "air" + (y + 1 == inputs.size() ? "\n" : " ") );
else
o += h.getName(i)+( y +1 == inputs.size() ? "\n" : " " );
o.append( h.getName( i ) + (y + 1 == inputs.size() ? "\n" : " ") );
}
return o.trim();
return o.toString().trim();
}
}