From 795829beef0ccadb697440f74041c33f4150780e Mon Sep 17 00:00:00 2001 From: oh-yes-0-fps Date: Wed, 18 Sep 2024 23:07:51 -0400 Subject: [PATCH 1/3] Improved `isScheduled` to be more performant when checking a single command --- .../first/wpilibj2/command/CommandScheduler.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/CommandScheduler.java b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/CommandScheduler.java index 7e634cf5c92..1ade0afbb0a 100644 --- a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/CommandScheduler.java +++ b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/CommandScheduler.java @@ -501,11 +501,16 @@ public void cancelAll() { * scheduled by the scheduler; it will not work on commands inside compositions, as the scheduler * does not see them. * - * @param commands the command to query - * @return whether the command is currently scheduled + * @param command a single command to check + * @param commands multiple commands to check + * @return whether all of the commands are currently scheduled */ - public boolean isScheduled(Command... commands) { - return m_scheduledCommands.containsAll(Set.of(commands)); + public boolean isScheduled(Command command, Command... commands) { + if (commands.length == 0) { + return m_scheduledCommands.contains(command); + } else { + return m_scheduledCommands.contains(command) && m_scheduledCommands.containsAll(Set.of(commands)); + } } /** From 51ee9b9fa11af6895f86703792ff317e2cfc65b4 Mon Sep 17 00:00:00 2001 From: oh-yes-0-fps Date: Wed, 18 Sep 2024 23:50:53 -0400 Subject: [PATCH 2/3] switched to overloads to prevent unecessary array allocs on single command checks --- .../wpilibj2/command/CommandScheduler.java | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/CommandScheduler.java b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/CommandScheduler.java index 1ade0afbb0a..e30522bcbc3 100644 --- a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/CommandScheduler.java +++ b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/CommandScheduler.java @@ -501,16 +501,28 @@ public void cancelAll() { * scheduled by the scheduler; it will not work on commands inside compositions, as the scheduler * does not see them. * - * @param command a single command to check * @param commands multiple commands to check * @return whether all of the commands are currently scheduled */ - public boolean isScheduled(Command command, Command... commands) { - if (commands.length == 0) { - return m_scheduledCommands.contains(command); - } else { - return m_scheduledCommands.contains(command) && m_scheduledCommands.containsAll(Set.of(commands)); + public boolean isScheduled(Command... commands) { + for (var cmd : commands) { + if (!isScheduled(cmd)) { + return false; + } } + return true; + } + + /** + * Whether the given commands are running. Note that this only works on commands that are directly + * scheduled by the scheduler; it will not work on commands inside compositions, as the scheduler + * does not see them. + * + * @param command a single command to check + * @return whether all of the commands are currently scheduled + */ + public boolean isScheduled(Command command) { + return m_scheduledCommands.contains(command); } /** From ebb91e8cd87a29f4b2392b0b07d201d0d0f34d78 Mon Sep 17 00:00:00 2001 From: oh-yes-0-fps Date: Thu, 19 Sep 2024 20:38:48 -0400 Subject: [PATCH 3/3] ran fmt --- .../java/edu/wpi/first/wpilibj2/command/CommandScheduler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/CommandScheduler.java b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/CommandScheduler.java index e30522bcbc3..0e80a74c613 100644 --- a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/CommandScheduler.java +++ b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/CommandScheduler.java @@ -522,7 +522,7 @@ public boolean isScheduled(Command... commands) { * @return whether all of the commands are currently scheduled */ public boolean isScheduled(Command command) { - return m_scheduledCommands.contains(command); + return m_scheduledCommands.contains(command); } /**