-
Notifications
You must be signed in to change notification settings - Fork 611
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[cmd] Improve isScheduled
to be more performant when checking a single command
#7096
[cmd] Improve isScheduled
to be more performant when checking a single command
#7096
Conversation
This PR modifies commands. Please open a corresponding PR in Python Commands and include a link to this PR. |
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)); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A simple loop over the array would be fine (this also avoids the Set allocation)
public boolean isScheduled(Command... commands) {
for (var command : commands) {
if (!m_scheduledCommands.contains(command)) {
return false;
}
}
return true;
}
Wrapping in Set.of
is useful only if the same command object is passed in more than once, which should be rare or even nonexistent.
Note that a variadic argument will always create a new array, even if no objects are passed in; ie, isScheduled()
effectively compiles to isScheduled(new Command[0])
, so this approach doesn't prevent array allocations. If you want to avoid array object allocations, overloads for some number of predetermined parameter counts would be necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That loop would make it match the C++ version too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it worth doing the predetermined overloads? multi-command schedule checks are already uncommon
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A single-command overload would probably be fine with varargs as a fallback
/format |
@oh-yes-0-fps you'll need to update with main to resolve the failing checks |
No description provided.