Prompt
Prioritize algorithmic efficiency and avoid unnecessary computational overhead in performance-critical code paths. Look for opportunities to reduce time complexity and eliminate redundant calculations.
Key optimization strategies:
- Use squared distance instead of actual distance when only comparison is needed:
prefer distanceSqoverdistance()for performance-critical geometric calculations - Avoid nested loops when a single pass is sufficient: instead of O(rules × workspaces) complexity, optimize to O(rules) by tracking only changed elements
- Eliminate redundant calculations by caching or restructuring computation order
- Choose appropriate STL algorithms: use
std::erase_ifdirectly instead of thestd::ranges::remove_if+erasepattern when possible
Example from distance calculations:
// Instead of:
if (m_vBeginDragXY.distance(mousePos) <= *PDRAGTHRESHOLD)
// Use:
if (m_vBeginDragXY.distanceSq(mousePos) <= (*PDRAGTHRESHOLD * *PDRAGTHRESHOLD))
Example from workspace checking:
// Instead of checking all workspaces against all rules O(n*m):
for (auto& workspace : workspaces) {
for (auto& rule : rules) { /* check */ }
}
// Optimize to O(n) by checking only the changed workspace:
recheckPersistent(); // Only checks current workspace against rules
This approach reduces CPU cycles in frequently called functions and improves overall application responsiveness.