Checklist
Description
Critical nodes in Autoware Core suffer from excessive mutable state, with some nodes containing 20-25+ member variables including publishers, subscribers, queues, and flags. This hidden mutable state creates spaghetti dependencies, makes testing fragile, and increases the risk of race conditions. This aligns with Anti-pattern 5 in the system performance documentation.
Purpose
The purpose of this task is to:
- Reduce mutable state complexity in critical nodes
- Implement pure function patterns for core algorithms
- Improve testability through explicit state management
- Eliminate race conditions and hidden dependencies
Possible approaches
Approach 1: Pure Function Pattern (Recommended)
// Before: Stateful class with side effects
class Tracker {
std::vector<State> history_; // Hidden mutable state
public:
void update(const Meas& z) {
history_.push_back(f(z, history_.back()));
}
Pose estimate() { return g(history_); } // Non-pure
};
// After: Immutable inputs/outputs
struct TrackerState {
std::vector<State> hist;
};
TrackerState update(const TrackerState& s, const Meas& z) {
TrackerState out = s;
out.hist.push_back(f(z, s.hist.empty() ? State{} : s.hist.back()));
return out;
}
Pose estimate(const TrackerState& s) {
return g(s.hist); // Pure function
}
Approach 2: State Snapshot Pattern
class Node : public rclcpp::Node {
struct StateSnapshot {
Odometry current_odom;
Trajectory current_traj;
// All state in one structure
};
std::mutex state_mutex_;
StateSnapshot state_;
void updateState(StateSnapshot new_state) {
std::lock_guard<std::mutex> lock(state_mutex_);
state_ = std::move(new_state);
}
};
Approach 3: Dependency Injection
// Extract dependencies instead of storing as members
ControlCommand computeControl(
const VehicleInfo& info,
const Odometry& odom,
const Trajectory& traj,
const ControlParams& params) {
// Pure computation, no hidden state
return control;
}
Definition of done
Immediate Refactoring
Short-term Implementation
Medium-term Optimization
Long-term Architecture
Checklist
Description
Critical nodes in Autoware Core suffer from excessive mutable state, with some nodes containing 20-25+ member variables including publishers, subscribers, queues, and flags. This hidden mutable state creates spaghetti dependencies, makes testing fragile, and increases the risk of race conditions. This aligns with Anti-pattern 5 in the system performance documentation.
Purpose
The purpose of this task is to:
Possible approaches
Approach 1: Pure Function Pattern (Recommended)
Approach 2: State Snapshot Pattern
Approach 3: Dependency Injection
Definition of done
Immediate Refactoring
Short-term Implementation
Medium-term Optimization
Long-term Architecture