Skip to content

Reduce rail signal-state churn in the simulation hot path - #33

Open
A-BenLi06 wants to merge 1 commit into
Minecraft-Transit-Railway:masterfrom
A-BenLi06:perf/simulation-hot-path
Open

Reduce rail signal-state churn in the simulation hot path#33
A-BenLi06 wants to merge 1 commit into
Minecraft-Transit-Railway:masterfrom
A-BenLi06:perf/simulation-hot-path

Conversation

@A-BenLi06

Copy link
Copy Markdown

Summary

Reduce per-rail allocation and copying in the once-per-second simulation hot path without changing signal reservation semantics.

Problem

For every rail and every simulation tick, the current implementation:

  • creates a separate client-array snapshot;
  • compares reservation key sets through generic collection helpers;
  • clears and copies four AVL maps;
  • uses a stream to test blocked vehicle IDs.

The same client set is therefore materialized R times for R rails, and unchanged reservation maps still incur copying.

Change

  • Snapshot clients once in Simulator.tick() and pass the array to every rail.
  • Rotate current/previous reservation maps instead of copying their contents.
  • Track key-set changes while reservations are written and retain size checks for removals.
  • Replace the boxed/streamed value scan with a primitive iterator.
  • Add a regression test proving reservation state survives map rotation.

The unavoidable client update scan remains O(R × C), but temporary client arrays fall from O(R × C) references to O(C), and reservation history rotation becomes O(1) rather than O(entries).

Validation

  • ./gradlew test --no-daemon
  • Full suite passed (113 tests).

Part of #32.

@A-BenLi06
A-BenLi06 marked this pull request as ready for review August 21, 2026 11:19
@A-BenLi06
A-BenLi06 force-pushed the perf/simulation-hot-path branch from fb32e7b to e2eb5b5 Compare August 22, 2026 16:37
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