diff --git a/src/ray/common/task/scheduling_resources.h b/src/ray/common/task/scheduling_resources.h index 95f4b3d6e..90df85a2f 100644 --- a/src/ray/common/task/scheduling_resources.h +++ b/src/ray/common/task/scheduling_resources.h @@ -67,6 +67,11 @@ class FractionalResourceQuantity { /// GPUs, and custom labels. class ResourceSet { public: + static std::shared_ptr Nil() { + static auto nil = std::make_shared(); + return nil; + } + /// \brief empty ResourceSet constructor. ResourceSet(); diff --git a/src/ray/common/task/task_spec.cc b/src/ray/common/task/task_spec.cc index 176cf55c5..bdc30015f 100644 --- a/src/ray/common/task/task_spec.cc +++ b/src/ray/common/task/task_spec.cc @@ -27,27 +27,43 @@ void TaskSpecification::ComputeResources() { if (required_placement_resources.empty()) { required_placement_resources = required_resources; } - required_resources_.reset(new ResourceSet(required_resources)); - required_placement_resources_.reset(new ResourceSet(required_placement_resources)); - // Map the scheduling class descriptor to an integer for performance. - auto sched_cls = std::make_pair(GetRequiredResources(), FunctionDescriptor()); - absl::MutexLock lock(&mutex_); - auto it = sched_cls_to_id_.find(sched_cls); - if (it == sched_cls_to_id_.end()) { - sched_cls_id_ = ++next_sched_id_; - // TODO(ekl) we might want to try cleaning up task types in these cases - if (sched_cls_id_ > 100) { - RAY_LOG(WARNING) << "More than " << sched_cls_id_ - << " types of tasks seen, this may reduce performance."; - } else if (sched_cls_id_ > 1000) { - RAY_LOG(ERROR) << "More than " << sched_cls_id_ - << " types of tasks seen, this may reduce performance."; - } - sched_cls_to_id_[sched_cls] = sched_cls_id_; - sched_id_to_cls_[sched_cls_id_] = sched_cls; + if (required_resources.empty()) { + // A static nil object is used here to avoid allocating the empty object every time. + required_resources_ = ResourceSet::Nil(); } else { - sched_cls_id_ = it->second; + required_resources_.reset(new ResourceSet(required_resources)); + } + + if (required_placement_resources.empty()) { + required_placement_resources_ = ResourceSet::Nil(); + } else { + required_placement_resources_.reset(new ResourceSet(required_placement_resources)); + } + + if (!IsActorTask()) { + // There is no need to compute `SchedulingClass` for actor tasks since + // the actor tasks need not be scheduled. + + // Map the scheduling class descriptor to an integer for performance. + auto sched_cls = std::make_pair(GetRequiredResources(), FunctionDescriptor()); + absl::MutexLock lock(&mutex_); + auto it = sched_cls_to_id_.find(sched_cls); + if (it == sched_cls_to_id_.end()) { + sched_cls_id_ = ++next_sched_id_; + // TODO(ekl) we might want to try cleaning up task types in these cases + if (sched_cls_id_ > 100) { + RAY_LOG(WARNING) << "More than " << sched_cls_id_ + << " types of tasks seen, this may reduce performance."; + } else if (sched_cls_id_ > 1000) { + RAY_LOG(ERROR) << "More than " << sched_cls_id_ + << " types of tasks seen, this may reduce performance."; + } + sched_cls_to_id_[sched_cls] = sched_cls_id_; + sched_id_to_cls_[sched_cls_id_] = sched_cls; + } else { + sched_cls_id_ = it->second; + } } } diff --git a/src/ray/common/task/task_spec.h b/src/ray/common/task/task_spec.h index 94ebae426..1d53aafaf 100644 --- a/src/ray/common/task/task_spec.h +++ b/src/ray/common/task/task_spec.h @@ -177,11 +177,11 @@ class TaskSpecification : public MessageWrapper { private: void ComputeResources(); - /// Field storing required resources. Initalized in constructor. + /// Field storing required resources. Initialized in constructor. /// TODO(ekl) consider optimizing the representation of ResourceSet for fast copies - /// instead of keeping shared ptrs here. + /// instead of keeping shared pointers here. std::shared_ptr required_resources_; - /// Field storing required placement resources. Initalized in constructor. + /// Field storing required placement resources. Initialized in constructor. std::shared_ptr required_placement_resources_; /// Cached scheduling class of this task. SchedulingClass sched_cls_id_;