Skip to content

Conversation

@tianyi-ge
Copy link
Contributor

Description

Replace const std::string & with std::string_view to avoid temporary string for a string literal or substring

Related issues

Closes #59887

Additional information

protobuf ParseFromString only accepts std::string.

rpc::ActorHandle CreateInnerActorHandleFromString(const std::string &serialized) {
rpc::ActorHandle inner;
inner.ParseFromString(serialized);
return inner;
}

Signed-off-by: tianyi-ge <tianyig@outlook.com>
@tianyi-ge tianyi-ge requested a review from a team as a code owner January 7, 2026 04:18
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request replaces const std::string& with std::string_view for read-only string parameters on performance-critical paths. This is a good optimization to avoid unnecessary string allocations. The changes are applied consistently across several core worker components. My review includes suggestions to further optimize the use of std::string_view with protobuf setters to prevent the creation of temporary std::string objects, thereby fully realizing the performance benefits of this change.

TaskSpecBuilder &SetCommonTaskSpec(
const TaskID &task_id,
const std::string name,
std::string_view name,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To fully realize the performance benefits of std::string_view, the protobuf setter in the function body should be updated. message_->set_name(name); will create a temporary std::string. To avoid this, you should use the (const char*, size_t) overload: message_->set_name(name.data(), name.size());.

Comment on lines +149 to +154
std::string_view debugger_breakpoint,
int64_t depth,
const TaskID &submitter_task_id,
const std::string &call_site,
std::string_view call_site,
const std::shared_ptr<rpc::RuntimeEnvInfo> runtime_env_info = nullptr,
const std::string &concurrency_group_name = "",
std::string_view concurrency_group_name = "",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To fully leverage std::string_view and avoid temporary std::string allocations, please use the (const char*, size_t) overload for the protobuf setters in this function's body. For example: message_->set_debugger_breakpoint(debugger_breakpoint.data(), debugger_breakpoint.size());. This applies to call_site and concurrency_group_name as well.

int max_retries,
bool retry_exceptions,
const std::string &serialized_retry_exception_allowlist,
std::string_view serialized_retry_exception_allowlist,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To avoid temporary std::string creation when setting the protobuf field, please use the (const char*, size_t) overload: message_->set_serialized_retry_exception_allowlist(serialized_retry_exception_allowlist.data(), serialized_retry_exception_allowlist.size());

TaskSpecBuilder &SetActorCreationTaskSpec(
const ActorID &actor_id,
const std::string &serialized_actor_handle,
std::string_view serialized_actor_handle,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To avoid temporary std::string creation, please use the (const char*, size_t) overload for the protobuf setter: actor_creation_spec->set_serialized_actor_handle(serialized_actor_handle.data(), serialized_actor_handle.size());

bool is_asyncio = false,
const std::vector<ConcurrencyGroup> &concurrency_groups = {},
const std::string &extension_data = "",
std::string_view extension_data = "",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To avoid temporary std::string creation, please use the (const char*, size_t) overload for the protobuf setter: actor_creation_spec->set_extension_data(extension_data.data(), extension_data.size());

const ObjectID &actor_creation_dummy_object_id,
int max_retries,
bool retry_exceptions,
std::string_view serialized_retry_exception_allowlist,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To avoid temporary std::string creation, please use the (const char*, size_t) overload for the protobuf setter: message_->set_serialized_retry_exception_allowlist(serialized_retry_exception_allowlist.data(), serialized_retry_exception_allowlist.size());

Comment on lines +32 to +35
std::string_view extension_data,
int64_t max_task_retries,
const std::string &name,
const std::string &ray_namespace,
std::string_view name,
std::string_view ray_namespace,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To fully leverage std::string_view and avoid temporary std::string allocations, please use the (const char*, size_t) overload for the protobuf setters in this function's body. For example, inner.set_extension_data(extension_data.data(), extension_data.size());. This applies to name and ray_namespace as well.

Comment on lines +4602 to +4603
std::string_view stdout_path,
std::string_view stderr_path,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To avoid temporary std::string allocations with protobuf setters, please use the (const char*, size_t) overload. For example: task_log_info.set_stdout_file(stdout_path.data(), stdout_path.size());. This also applies to stderr_path.

@ray-gardener ray-gardener bot added core Issues that should be addressed in Ray Core community-contribution Contributed by the community labels Jan 7, 2026
Copy link
Collaborator

@edoakes edoakes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@edoakes edoakes added the go add ONLY when ready to merge, run all tests label Jan 7, 2026
@israbbani
Copy link
Contributor

@tianyi-ge. Thanks for the contribution. I've started some discussion on the original issue. #59887. I'll hold off on reviewing this until we finish the discussion there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Contributed by the community core Issues that should be addressed in Ray Core go add ONLY when ready to merge, run all tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[core] Use std::string_view for read-only string parameters

3 participants