Skip to content

Optimize autocrafter with timestamp-based slot caching#40

Open
Distolfix wants to merge 1 commit intoSongoda-Plugins:masterfrom
Distolfix:fix/autocrafter-slot-caching-v2
Open

Optimize autocrafter with timestamp-based slot caching#40
Distolfix wants to merge 1 commit intoSongoda-Plugins:masterfrom
Distolfix:fix/autocrafter-slot-caching-v2

Conversation

@Distolfix
Copy link
Copy Markdown

Summary

This PR optimizes the autocrafter module by adding timestamp-based caching to remember the last output slot used. This prevents inventory fragmentation and improves performance.

Problem

The autocrafter was iterating through all inventory slots on every single craft operation to find where to place the output item. This had two issues:

  1. Performance: O(n) iteration on every craft, even when crafting the same item repeatedly
  2. Inventory fragmentation: Same item type scattered across multiple slots (e.g., diamond blocks appearing in slots 0, 1, and 4 instead of stacking together)

Solution

Implemented a slot caching mechanism with timestamp-based validation:

  • Cache structure: Map<Hopper, LastOutputSlot> storing slot index, material type, and timestamp
  • TTL: 60-second timeout to handle material changes or slot reorganization
  • Fast path: O(1) cached slot check before falling back to iteration
  • Two-pass fallback: First searches for existing stacks of same type, then for empty slots

Technical Details

The caching logic:

  1. Check if we have a cached slot for this hopper and material (timestamp < 60s)
  2. If valid and slot still has space → use it directly (no iteration!)
  3. If invalid → two-pass search:
    • First pass: find existing stacks of same item to merge with
    • Second pass: find empty slots
  4. Update cache with whichever slot was used

Benefits

  • Performance: Eliminates repeated iteration for consecutive crafts of same item
  • Clean inventory: Items stack properly in same slot instead of fragmenting
  • Backwards compatible: Falls back gracefully when cache invalid

Testing

Tested with 10,000 diamonds being auto-crafted into diamond blocks:

  • ✅ All blocks stack in same slot until full
  • ✅ No performance degradation with continuous crafting
  • ✅ Cache properly invalidates after 60s or material change

Add timestamp-based caching to remember last output slot used, preventing
inventory fragmentation and improving performance.

Previous behavior iterated through all slots every craft to find where
to place the output, often resulting in the same item type scattered
across multiple slots (e.g., diamond blocks in slots 0, 1, and 4).

Now caches the last used slot per hopper with a 60-second TTL. On
subsequent crafts, it checks the cached slot first (O(1) instead of
O(n)). If valid, uses it directly. If not, falls back to two-pass
search: first looking for existing stacks of the same type, then
for empty slots.

This keeps inventory organized and reduces iteration overhead on
every craft operation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant