-
Notifications
You must be signed in to change notification settings - Fork 273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inefficient Usages of Java Collections #1125
base: dev
Are you sure you want to change the base?
Changes from all commits
9462974
f9b07bb
0ad4781
3f8d177
52e524f
e5ec61f
fbfb06b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,8 @@ | |
import org.bukkit.World; | ||
|
||
import java.util.Random; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class RedwoodTree extends GenericTree { | ||
|
||
|
@@ -45,6 +47,7 @@ protected final void setLeavesHeight(int leavesHeight) { | |
|
||
@Override | ||
public boolean canPlace(int baseX, int baseY, int baseZ, World world) { | ||
Set<Material> overridableSet = new HashSet<>(overridables); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should convert the base class' overridables to a HashSet, to prevent allocations every call. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can redesign the class to achieve better optimization. Currently, such allocation also improves the efficiency of the single function indeed. |
||
for (int y = baseY; y <= baseY + 1 + height; y++) { | ||
// Space requirement | ||
int radius; // default radius if above first block | ||
|
@@ -59,7 +62,7 @@ public boolean canPlace(int baseX, int baseY, int baseZ, World world) { | |
if (y >= 0 && y < 256) { | ||
// we can overlap some blocks around | ||
Material type = blockTypeAt(x, y, z, world); | ||
if (!overridables.contains(type)) { | ||
if (!overridableSet.contains(type)) { | ||
return false; | ||
} | ||
} else { // height out of range | ||
|
@@ -81,6 +84,8 @@ public boolean generate(World world, Random random, int blockX, int blockY, int | |
int radius = random.nextInt(2); | ||
int peakRadius = 1; | ||
int minRadius = 0; | ||
Set<Material> overridableSet = new HashSet<>(overridables); | ||
|
||
for (int y = blockY + height; y >= blockY + leavesHeight; y--) { | ||
// leaves are built from top to bottom | ||
for (int x = blockX - radius; x <= blockX + radius; x++) { | ||
|
@@ -107,7 +112,7 @@ && blockTypeAt(x, y, z, world) == Material.AIR) { | |
// generate the trunk | ||
for (int y = 0; y < height - random.nextInt(3); y++) { | ||
Material type = blockTypeAt(blockX, blockY + y, blockZ, world); | ||
if (overridables.contains(type)) { | ||
if (overridableSet.contains(type)) { | ||
delegate.setType(world, blockX, blockY + y, | ||
blockZ, logType); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,6 @@ | |
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import java.util.Set; | ||
import java.util.TreeMap; | ||
import java.util.TreeSet; | ||
mastercoms marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
|
@@ -59,7 +58,7 @@ public final class GlowHelpMap implements HelpMap { | |
*/ | ||
public GlowHelpMap(GlowServer server) { | ||
this.server = server; | ||
helpTopics = new TreeMap<>(NAME_COMPARE); | ||
helpTopics = new HashMap<>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is iterated upon in Bukkit (a Maven dependency of ours, see net.glowstone.glowkit), in HelpCommand. |
||
defaultTopic | ||
= new IndexHelpTopic("Index", null, null, indexTopics, "Use /help [n] to get page" | ||
+ " n of help."); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
import org.apache.maven.artifact.versioning.ComparableVersion; | ||
import org.jetbrains.annotations.NonNls; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
|
@@ -157,8 +157,7 @@ public static Library fromConfigMap(Map<?, ?> configMap) { | |
* @return A map that is able to be serialized into a config. | ||
*/ | ||
public Map<?, ?> toConfigMap() { | ||
// Using LinkedHashMap to keep the props in order when written into the config file. | ||
Map<String, Object> configMap = new LinkedHashMap<>(); | ||
Map<String, Object> configMap = new HashMap<>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't called within Glowstone (yet?), but the interface guarantees ordering for a config file. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah. Our tool finds that the order is actually not used because the map object is not traversed. Of course, you can maintain it as a LinkedHashMap if you want to assure the extensibility of the program. For the current version, the HashMap is a better choice. |
||
configMap.put(GROUP_ID_KEY, libraryKey.getGroupId()); | ||
configMap.put(ARTIFACT_ID_KEY, libraryKey.getArtifactId()); | ||
configMap.put(VERSION_KEY, version); | ||
|
@@ -168,7 +167,7 @@ public static Library fromConfigMap(Map<?, ?> configMap) { | |
} | ||
|
||
if (checksumType != null && checksumValue != null) { | ||
Map<String, Object> checksumMap = new LinkedHashMap<>(); | ||
Map<String, Object> checksumMap = new HashMap<>(); | ||
checksumMap.put(CHECKSUM_TYPE_KEY, checksumType.getName()); | ||
checksumMap.put(CHECKSUM_VALUE_KEY, checksumValue); | ||
configMap.put(CHECKSUM_KEY, checksumMap); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would much rather have the MATERIALS variable be directly converted to a HashSet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. I agree with you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EnumSet instead of HashSet?