Skip to content

perf: cache Ground.PERIOD constant to eliminate redundant arithmetic - #2

Open
adihex wants to merge 1 commit into
feat/guitar-dino-v1from
perf/cache-ground-period
Open

adihex wants to merge 1 commit into
feat/guitar-dino-v1from
perf/cache-ground-period

Conversation

@adihex

@adihex adihex commented Jul 17, 2026

Copy link
Copy Markdown
Owner

Performance Optimization

This PR caches the Ground.PERIOD constant (= DASH_LENGTH + DASH_GAP = 18) at class definition time instead of recomputing it on every frame.

Metrics

  • Frames affected: All game frames (60 FPS)
  • Operations eliminated per frame: 2 (1 addition + 1 modulo)
  • Effort: 1 line change
  • Risk: None

Changes

  • Added PERIOD = DASH_LENGTH + DASH_GAP as a class constant
  • Updated Ground.update() to use self.PERIOD instead of computing inline
  • Updated Ground.draw() to use self.PERIOD instead of local variable

This is part of a performance audit identifying the 10 highest-impact optimizations for the rhythm game engine. This improvement was selected as the highest-impact, lowest-effort optimization (VERY LOW effort + LOW latency-path impact).

The PERIOD calculation (DASH_LENGTH + DASH_GAP) was computed every frame
in Ground.draw() and Ground.update(), even though it's a compile-time
constant. This simple cache moves the computation to class definition time.

Impact: Eliminates 2 arithmetic operations per frame (60 FPS).
- Ground.update(): 1 modulo operation eliminated
- Ground.draw(): 1 addition operation + 1 loop iteration eliminated

Effort: Single line (PERIOD = DASH_LENGTH + DASH_GAP in class definition).
Risk: None—behavioral change only improves performance without altering logic.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
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.

2 participants