From a66fb2f00193181068db5390fef14547e216dfed Mon Sep 17 00:00:00 2001 From: Steven Schveighoffer Date: Sun, 19 Apr 2026 17:23:30 -0400 Subject: [PATCH] chore(gc-churn): Change list of Phoenix 6 status signals to an ArrayList instead of a builtin array. Per extracted Phoenix 6 source, the latter is converted to the former, which is a completely needless allocation for the call into the library to update signals. --- .../drive/PhoenixOdometryThread.java | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/template_projects/sources/talonfx_swerve/src/main/java/frc/robot/subsystems/drive/PhoenixOdometryThread.java b/template_projects/sources/talonfx_swerve/src/main/java/frc/robot/subsystems/drive/PhoenixOdometryThread.java index 684ffd17..4e7690f2 100644 --- a/template_projects/sources/talonfx_swerve/src/main/java/frc/robot/subsystems/drive/PhoenixOdometryThread.java +++ b/template_projects/sources/talonfx_swerve/src/main/java/frc/robot/subsystems/drive/PhoenixOdometryThread.java @@ -31,7 +31,7 @@ public class PhoenixOdometryThread extends Thread { private final Lock signalsLock = new ReentrantLock(); // Prevents conflicts when registering signals - private BaseStatusSignal[] phoenixSignals = new BaseStatusSignal[0]; + private ArrayList phoenixSignals = new ArrayList<>(); private final List genericSignals = new ArrayList<>(); private final List> phoenixQueues = new ArrayList<>(); private final List> genericQueues = new ArrayList<>(); @@ -65,10 +65,7 @@ public Queue registerSignal(StatusSignal signal) { signalsLock.lock(); Drive.odometryLock.lock(); try { - BaseStatusSignal[] newSignals = new BaseStatusSignal[phoenixSignals.length + 1]; - System.arraycopy(phoenixSignals, 0, newSignals, 0, phoenixSignals.length); - newSignals[phoenixSignals.length] = signal; - phoenixSignals = newSignals; + phoenixSignals.add(signal); phoenixQueues.add(queue); } finally { signalsLock.unlock(); @@ -110,14 +107,14 @@ public void run() { // Wait for updates from all signals signalsLock.lock(); try { - if (isCANFD && phoenixSignals.length > 0) { + if (isCANFD && phoenixSignals.size() > 0) { BaseStatusSignal.waitForAll(2.0 / Drive.ODOMETRY_FREQUENCY, phoenixSignals); } else { // "waitForAll" does not support blocking on multiple signals with a bus // that is not CAN FD, regardless of Pro licensing. No reasoning for this // behavior is provided by the documentation. Thread.sleep((long) (1000.0 / Drive.ODOMETRY_FREQUENCY)); - if (phoenixSignals.length > 0) BaseStatusSignal.refreshAll(phoenixSignals); + if (phoenixSignals.size() > 0) BaseStatusSignal.refreshAll(phoenixSignals); } } catch (InterruptedException e) { e.printStackTrace(); @@ -136,13 +133,13 @@ public void run() { for (BaseStatusSignal signal : phoenixSignals) { totalLatency += signal.getTimestamp().getLatency(); } - if (phoenixSignals.length > 0) { - timestamp -= totalLatency / phoenixSignals.length; + if (phoenixSignals.size() > 0) { + timestamp -= totalLatency / phoenixSignals.size(); } // Add new samples to queues - for (int i = 0; i < phoenixSignals.length; i++) { - phoenixQueues.get(i).offer(phoenixSignals[i].getValueAsDouble()); + for (int i = 0; i < phoenixSignals.size(); i++) { + phoenixQueues.get(i).offer(phoenixSignals.get(i).getValueAsDouble()); } for (int i = 0; i < genericSignals.size(); i++) { genericQueues.get(i).offer(genericSignals.get(i).getAsDouble());