Repository: ray-project/ray
File: src/ray/core_worker/reference_counter.cc (C++)

Code before:
     << " contains: " << nested().contains.size()
     << " stored_in: " << borrow().stored_in_objects.size()
     << " lineage_ref_count: " << lineage_ref_count << "}";
  return ss.str();
}

ReferenceCounter::Reference ReferenceCounter::Reference::FromProto(
    const rpc::ObjectReferenceCount &ref_count) {
  Reference ref;
  ref.owner_address_ = ref_count.reference().owner_address();
  ref.local_ref_count = ref_count.has_local_ref() ? 1 : 0;

  for (const auto &borrower : ref_count.borrowers()) {
    ref.mutable_borrow()->borrowers.insert(borrower);
  }
  for (const auto &object : ref_count.stored_in_objects()) {
    const auto &object_id = ObjectID::FromBinary(object.object_id());
    ref.mutable_borrow()->stored_in_objects.emplace(object_id, object.owner_address());
  }
  for (const auto &id : ref_count.contains()) {
    ref.mutable_nested()->contains.insert(ObjectID::FromBinary(id));
  }
  const auto contained_in_borrowed_ids =
      IdVectorFromProtobuf<ObjectID>(ref_count.contained_in_borrowed_ids());
  ref.mutable_nested()->contained_in_borrowed_ids.insert(
      contained_in_borrowed_ids.begin(), contained_in_borrowed_ids.end());
  return ref;
}

void ReferenceCounter::Reference::ToProto(rpc::ObjectReferenceCount *ref,
                                          const ObjectID &object_id,
                                          bool has_local_ref) const {
  ref->mutable_reference()->set_object_id(object_id.Binary());
  if (owner_address_) {
    ref->mutable_reference()->mutable_owner_address()->CopyFrom(*owner_address_);
  }
  ref->set_has_local_ref(has_local_ref);
  for (const auto &borrower : borrow().borrowers) {
    ref->add_borrowers()->CopyFrom(borrower);
  }
  for (const auto &object : borrow().stored_in_objects) {
    auto ref_object = ref->add_stored_in_objects();
    ref_object->set_object_id(object.first.Binary());
    ref_object->mutable_owner_address()->CopyFrom(object.second);
  }
  for (const auto &contained_in_borrowed_id : nested().contained_in_borrowed_ids) {
    ref->add_contained_in_borrowed_ids(contained_in_borrowed_id.Binary());
  }
  for (const auto &contains_id : nested().contains) {
    ref->add_contains(contains_id.Binary());
  }

Review comment (suggestion):
Found it to be a better separation of concerns to avoid any control flow inside the proto serialization method.

Code after:
     << " submitted_count: " << submitted_task_ref_count
     << " contained_on_owned: " << nested().contained_in_owned.size()
     << " contained_in_borrowed: " << nested().contained_in_borrowed_ids.size()
     << " contains: " << nested().contains.size()
     << " stored_in: " << borrow().stored_in_objects.size()
     << " lineage_ref_count: " << lineage_ref_count << "}";
  return ss.str();
}

ReferenceCounter::Reference ReferenceCounter::Reference::FromProto(
    const rpc::ObjectReferenceCount &ref_count) {
  Reference ref;
  ref.owner_address_ = ref_count.reference().owner_address();
  ref.local_ref_count = ref_count.has_local_ref() ? 1 : 0;

  for (const auto &borrower : ref_count.borrowers()) {
    ref.mutable_borrow()->borrowers.insert(borrower);
  }
  for (const auto &object : ref_count.stored_in_objects()) {
    const auto &object_id = ObjectID::FromBinary(object.object_id());
    ref.mutable_borrow()->stored_in_objects.emplace(object_id, object.owner_address());
  }
  for (const auto &id : ref_count.contains()) {
    ref.mutable_nested()->contains.insert(ObjectID::FromBinary(id));
  }
  const auto contained_in_borrowed_ids =
      IdVectorFromProtobuf<ObjectID>(ref_count.contained_in_borrowed_ids());
  ref.mutable_nested()->contained_in_borrowed_ids.insert(
      contained_in_borrowed_ids.begin(), contained_in_borrowed_ids.end());
  return ref;
}

void ReferenceCounter::Reference::ToProto(rpc::ObjectReferenceCount *ref,
                                          const ObjectID &object_id,
                                          bool has_local_ref) const {
  ref->mutable_reference()->set_object_id(object_id.Binary());
  if (owner_address_) {
    ref->mutable_reference()->mutable_owner_address()->CopyFrom(*owner_address_);
  }
  ref->set_has_local_ref(has_local_ref);
  for (const auto &borrower : borrow().borrowers) {
    ref->add_borrowers()->CopyFrom(borrower);
  }
  for (const auto &object : borrow().stored_in_objects) {
    auto ref_object = ref->add_stored_in_objects();
    ref_object->set_object_id(object.first.Binary());
    ref_object->mutable_owner_address()->CopyFrom(object.second);
  }
  for (const auto &contained_in_borrowed_id : nested().contained_in_borrowed_ids) {
    ref->add_contained_in_borrowed_ids(contained_in_borrowed_id.Binary());
  }