Skip to content

Use constant-time circular timetable arithmetic - #34

Open
A-BenLi06 wants to merge 1 commit into
Minecraft-Transit-Railway:masterfrom
A-BenLi06:perf/circular-time-arithmetic
Open

Use constant-time circular timetable arithmetic#34
A-BenLi06 wants to merge 1 commit into
Minecraft-Transit-Railway:masterfrom
A-BenLi06:perf/circular-time-arithmetic

Conversation

@A-BenLi06

Copy link
Copy Markdown

Summary

Replace iterative circular clamp/difference normalization with constant-time floor-mod arithmetic.

Problem

The existing helpers repeatedly add or subtract one period until a value reaches the requested window. Runtime is O(abs(offset) / period), so very large timetable offsets or persisted timestamps can monopolize the simulation thread. Extreme long values can also overflow while the loop is normalizing.

Change

  • Normalize integral values with Math.floorMod.
  • Normalize floating-point values with a floor-mod helper.
  • Preserve the existing half-period tie direction.
  • Reject non-positive periods instead of entering a non-terminating loop.
  • Preserve NaN propagation for doubles.
  • Add equivalence tests against the previous iterative semantics over representative ranges plus Long.MIN_VALUE/Long.MAX_VALUE cases.

Runtime becomes O(1) for all finite input magnitudes.

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 09:50
@A-BenLi06
A-BenLi06 force-pushed the perf/circular-time-arithmetic branch from 9818376 to d4efc7a 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