EnigmaMachineCore 0.1.0
A modular Enigma Machine simulation in C++20
Loading...
Searching...
No Matches
ADR 005: Modulo Optimization (Conditional Subtraction)

Status

Accepted

Context

The Enigma Machine's signal path and rotor stepping logic frequently use the modulo operator (% TRANSFORMER_SIZE) to ensure values stay within the [0, 25] range. On modern CPUs, integer division (which modulo requires) is a significantly more expensive instruction than simple addition or subtraction.

Decision

We will replace the modulo operator with Conditional Subtractions in the engine's "hot path" (transformation and rotation).

// Before
position = (position + rotationCount) % TRANSFORMER_SIZE;
// After
position = (position + rotationCount);
if (position >= TRANSFORMER_SIZE) {
position -= TRANSFORMER_SIZE;
}
constexpr int TRANSFORMER_SIZE
Size of the alphabet/transformer (Standard Enigma is 26).
Definition config.hpp:10
  1. CPU Cycle Efficiency: Replaces a high-latency division instruction with a low-latency addition/subtraction and a highly predictable branch.
  2. Range Guarantee: This optimization is safe because the intermediate sum (position + rotationCount) is guaranteed to be less than 2 * TRANSFORMER_SIZE.
  3. Measured Gain: Benchmarks show more than 10% increase in overall throughput and a ~50% reduction in rotor rotation latency.

Consequences

  • Code Verbosity: Slightly increases the number of lines in the transformation logic.
  • Arithmetic Constraints: The optimization relies on the fact that input values are already bounded. If the coordinate system is changed to allow values significantly larger than 2 * TRANSFORMER_SIZE, this optimization would become invalid.
  • Maintainability: Developers must ensure that braces are used for these if statements to maintain readability and satisfy static analysis (linter) rules.