Skip to content
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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Copy link
Member

@SamCarlberg SamCarlberg Sep 19, 2024

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.

Copy link
Member

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.

Copy link
Author

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

Copy link
Member

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

}

/**
Expand Down
Loading