From efe0b17b9f0ed19f8f8bd86b65c47866f5e6eb3c Mon Sep 17 00:00:00 2001
From: Kaya <95276965+kytpbs@users.noreply.github.com>
Date: Mon, 16 Dec 2024 15:35:40 +0100
Subject: [PATCH] Better comments and variable names

---
 .../wpi/first/wpilibj2/command/Command.java   | 22 ++++++++-----------
 1 file changed, 9 insertions(+), 13 deletions(-)

diff --git a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/Command.java b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/Command.java
index fada98a1618..b9aa08f94b6 100644
--- a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/Command.java
+++ b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/Command.java
@@ -15,7 +15,6 @@
 import java.util.Collection;
 import java.util.HashSet;
 import java.util.Set;
-import java.util.concurrent.atomic.AtomicInteger;
 import java.util.function.BooleanSupplier;
 
 /**
@@ -403,12 +402,8 @@ public RepeatCommand repeatedly() {
   }
 
   /**
-   * Decorates this command to run repeatedly, restarting until the command runs for the given times
-   * amount. The decorated command can still be canceled.
-   *
-   * <p>This is just syntactic sugar for {@code this.finallyDo(() ->
-   * counter[0]++).repeatedly().until(() -> counter[0] >= times)} which just means: runs the
-   * command, then increments the counter, repeatedly until the counter saturates.
+   * Decorates this command to run repeatedly, restarting until the command runs for the given
+   * number of times. The decorated command can still be canceled.
    *
    * <p>Note: This decorator works by adding this command to a composition. The command the
    * decorator was called on cannot be scheduled independently or be added to a different
@@ -416,14 +411,15 @@ public RepeatCommand repeatedly() {
    * commands with {@link CommandScheduler#removeComposedCommand(Command)}. The command composition
    * returned from this method can be further decorated without issue.
    *
-   * @param times the count of times to run the command (inclusively).
+   * @param repetitions the count of times to run the command.
    * @return the decorated command
    */
-  public ParallelRaceGroup repeatedly(int times) {
-    AtomicInteger counter = new AtomicInteger(0);
-    return this.finallyDo(counter::getAndIncrement)
-        .repeatedly()
-        .until(() -> counter.get() >= times);
+  public ParallelRaceGroup repeatedly(int repetitions) {
+    // use an array so that it stays in the heap instead of stack.
+    // We use an array with a size of 1 instead of `AtomicInteger` because of the performance
+    // overhead
+    int[] counter = new int[1];
+    return this.finallyDo(() -> counter[0]++).repeatedly().until(() -> counter[0] >= repetitions);
   }
 
   /**