Optimize Villager food inventory checks - #185
Conversation
Dreeam-qwq
left a comment
There was a problem hiding this comment.
We can change FOOD_POINTS to 2d array or 2 arrays maybe.
Noted that, FOOD_POINTS is removed since 26.3 snapshot 1, replaced by VILLAGER_FOOD item component tag. Maybe at that time, we can use booleans or mask to cache states if the item has specific component tags, I guess? Not sure whether worth it.
|
Fair point about 26.3 — didn't know FOOD_POINTS was being removed. For the current version though, the main win here is just dropping the stream overhead (lambda, spliterator, boxing) since it runs on every breed check. The map only has 4 entries so the data structure itself doesn't really matter, it's the per-call allocation that adds up. The eatUntilFull hoist is separate from FOOD_POINTS anyway — just caching getInventory() before the loop. Happy to tweak if you want a different approach. |
|
done, sorry for the late response — was away for a bit |
Nrleryxx
left a comment
There was a problem hiding this comment.
Replace the four countItem scans with one pass over the inventory slots using a FOOD_POINTS lookup to get four times less work
Nrleryxx
left a comment
There was a problem hiding this comment.
Fix the Gale start comment to follow the repo convention with a category label like Local code optimization
Nrleryxx
left a comment
There was a problem hiding this comment.
Verify the claim that hungry hasExcessFood and wantsMoreFood call countFoodPointsInInventory because in several versions they only check the foodLevel field
Nrleryxx
left a comment
There was a problem hiding this comment.
Optionally short circuit canBreed to skip the inventory scan when foodLevel is already twelve or more for a much bigger win than the stream removal
|
Thanks for the review , working on all four points now. |
…ix comment labels
|
Done. added foodLevel >= 12 short circuit in canBreed to skip the inventory scan entirely. |
Motivation
countFoodPointsInInventory()was scanning the entire inventory 4 times (once per food type) usingcountItem()for each entry inFOOD_POINTS. Replaced with a single pass over inventory slots, checking each item againstFOOD_POINTS— same result, one scan instead of four.Also added a short circuit in
canBreed()— iffoodLevelis already >= 12, skip the inventory scan entirely since it's not needed.Changes
countFoodPointsInInventory()— single inventory pass instead of 4 separatecountItemscans. Added "Local code optimization" category label per Gale convention.canBreed()— short circuit whenfoodLevel >= 12to avoid unnecessary inventory scan.