Botany Pots

Botany Pots

49M Downloads

[FEEDBACK] Botany plants should be caching the recipe and not call it every tick

wolfieboy09 opened this issue ยท 2 comments

commented

Minecraft Version

1.18.2

Mod Version

8.1.29

Mod Loader

Forge

What environment are you running the mod in?

Don't Know / Not Applicable

Issue Description

When running a spark profile thingy, this mod is calling the recipe stuff every tick

The pots should cache the result and not have to do this

Offending code:

public static Crop findCrop(Level level, BlockPos pos, BlockEntityBotanyPot pot, ItemStack stack) {
Crop result = null;
if (level != null && !stack.isEmpty()) {
for (final Crop crop : level.getRecipeManager().getAllRecipesFor(CROP_TYPE.get())) {
if (crop.matchesLookup(level, pos, pot, stack)) {
result = crop;
break;
}
}
}
return EVENT_DISPATCHER.postCropLookup(level, pos, pot, stack, result);

Thing: https://spark.lucko.me/9DZIZySIOE (stoneblock-3 modified)

commented

Something that could help speed things up is using a weak hash map to do an initial lookup using the Item ID + pot soil as the key.
If the combination of them exists, check if the crop matches the lookup. If either they don't exist on the map or if the crop does not match the lookup, then you do the more exhaustive lookup through the recipe manager.

Using a weak hash map would allow the garbage collector to clean up the hashmap as it sees fit without creating a memory leak.

commented

Additionally, this section could be optimized by simply using TreeSets.

public boolean canGrowCrop(Level level, BlockPos pos, BlockEntityBotanyPot pot, Crop crop) {
for (final String soilCategory : this.getCategories(level, pos, pot)) {
for (final String cropCategory : crop.getCategories(level, pos, pot)) {
if (soilCategory.equalsIgnoreCase(cropCategory)) {
return true;
}
}
}
return false;

You could get both categories, loop over whichever is smaller, and if the larger set contains an element in the smaller one, return true.
The TreeSet would allow you to do a case-insensitive contains check.