Skip to content

Optimize Villager food inventory checks - #185

Open
Smorki wants to merge 3 commits into
GaleMC:mainfrom
Smorki:optimize-villager-food-check
Open

Optimize Villager food inventory checks#185
Smorki wants to merge 3 commits into
GaleMC:mainfrom
Smorki:optimize-villager-food-check

Conversation

@Smorki

@Smorki Smorki commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

Motivation

countFoodPointsInInventory() was scanning the entire inventory 4 times (once per food type) using countItem() for each entry in FOOD_POINTS. Replaced with a single pass over inventory slots, checking each item against FOOD_POINTS — same result, one scan instead of four.

Also added a short circuit in canBreed() — if foodLevel is already >= 12, skip the inventory scan entirely since it's not needed.

Changes

  • countFoodPointsInInventory() — single inventory pass instead of 4 separate countItem scans. Added "Local code optimization" category label per Gale convention.
  • canBreed() — short circuit when foodLevel >= 12 to avoid unnecessary inventory scan.

@Dreeam-qwq Dreeam-qwq left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Smorki

Smorki commented Jul 26, 2026

Copy link
Copy Markdown
Contributor Author

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.

@Dreeam-qwq Dreeam-qwq added the type: optimization optimization related PRs label Jul 29, 2026
@Dreeam-qwq
Dreeam-qwq requested a review from Nrleryxx July 29, 2026 13:09
@Smorki

Smorki commented Aug 3, 2026

Copy link
Copy Markdown
Contributor Author

done, sorry for the late response — was away for a bit

@Nrleryxx Nrleryxx left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace the four countItem scans with one pass over the inventory slots using a FOOD_POINTS lookup to get four times less work

@Nrleryxx Nrleryxx left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix the Gale start comment to follow the repo convention with a category label like Local code optimization

@Nrleryxx Nrleryxx left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verify the claim that hungry hasExcessFood and wantsMoreFood call countFoodPointsInInventory because in several versions they only check the foodLevel field

@Nrleryxx Nrleryxx left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@Smorki

Smorki commented Aug 3, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review , working on all four points now.

@Smorki

Smorki commented Aug 3, 2026

Copy link
Copy Markdown
Contributor Author

Done. added foodLevel >= 12 short circuit in canBreed to skip the inventory scan entirely.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: optimization optimization related PRs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants