Skip to content

Use std::string_view for read-only string parameters (#59887)#60781

Open
MuhammadSaif700 wants to merge 20 commits intoray-project:masterfrom
MuhammadSaif700:use-string-view
Open

Use std::string_view for read-only string parameters (#59887)#60781
MuhammadSaif700 wants to merge 20 commits intoray-project:masterfrom
MuhammadSaif700:use-string-view

Conversation

@MuhammadSaif700
Copy link

Description

Refactors read-only string parameters from const std::string& to std::string_view in src/ray/core_worker/ and src/ray/common/task/ to improve performance by avoiding unnecessary string copies.

Related issues

Fixes #59887

Additional information

Changes:

  • Updated 40+ function signatures across core_worker, reference_counter, and task_spec files
  • Added #include <string_view> headers where needed
  • Added std::string() conversions only where strings are stored or passed to protobuf

Files modified:

  • src/ray/core_worker/core_worker.h/cc
  • src/ray/core_worker/reference_counter_interface.h
  • src/ray/core_worker/reference_counter.h/cc
  • src/ray/common/task/task_spec.h

Backward compatible: Existing code continues to work as std::string_view accepts string literals, std::string, and C-strings.

@MuhammadSaif700 MuhammadSaif700 requested a review from a team as a code owner February 5, 2026 19:51
Copilot AI review requested due to automatic review settings February 5, 2026 19:51
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 is a great performance-oriented refactoring that replaces const std::string& with std::string_view for read-only string parameters. The changes are applied consistently across many files. The explicit conversions back to std::string are correctly handled where necessary for storage or for APIs that have not yet been updated. I found one minor case where an unnecessary string construction is performed for a map lookup, which I've commented on. Overall, this is a valuable improvement.

CoreWorker::GetNamedActorHandleLocalMode(const std::string &name) {
auto it = local_mode_named_actor_registry_.find(name);
CoreWorker::GetNamedActorHandleLocalMode(std::string_view name) { // Changed: read-only param
auto it = local_mode_named_actor_registry_.find(std::string(name)); // Convert for map lookup
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The absl::flat_hash_map supports transparent lookups, allowing you to use std::string_view for lookups on a map with std::string keys without creating a temporary std::string. The explicit conversion to std::string here is unnecessary and introduces a performance regression compared to the original code, which goes against the goal of this PR.

  auto it = local_mode_named_actor_registry_.find(name);  // Convert for map lookup

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors function parameters in core_worker and reference_counter components from const std::string& to std::string_view for read-only string parameters to improve performance by avoiding unnecessary string copies.

Changes:

  • Updated 40+ function signatures to use std::string_view instead of const std::string&
  • Added explicit std::string() conversions where strings are stored in member variables or passed to APIs requiring std::string
  • Added #include <string_view> headers to relevant files

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/ray/core_worker/reference_counter_interface.h Updated interface methods to accept std::string_view for call_site and spilled_url parameters
src/ray/core_worker/reference_counter.h Updated class method declarations to match interface changes
src/ray/core_worker/reference_counter.cc Implemented string_view parameters with proper conversions to std::string for storage and constructor calls
src/ray/core_worker/core_worker.h Updated 20+ method signatures including TaskCounter and CoreWorker methods to use string_view
src/ray/core_worker/core_worker.cc Implemented string_view parameters with proper conversions for storage, protobuf, and map lookups
src/ray/common/task/task_spec.h Updated TaskSpecification constructor to accept string_view with conversion for base class

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 1770 to 1771
std::string_view type, // Changed: read-only param
std::string_view error_message, // Changed: read-only param
Copy link

Copilot AI Feb 5, 2026

Choose a reason for hiding this comment

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

The function signature in the implementation file uses std::string_view for type and error_message parameters, but the declaration in core_worker.h (lines 830-831) still uses const std::string &. This mismatch will cause compilation errors. The header file declaration must be updated to match this implementation.

Suggested change
std::string_view type, // Changed: read-only param
std::string_view error_message, // Changed: read-only param
const std::string &type,
const std::string &error_message,

Copilot uses AI. Check for mistakes.

/// Decrease the local reference count for the ObjectID by one.
///
std::string_view call_site) = 0; // Changed: read-only param
Copy link

Copilot AI Feb 5, 2026

Choose a reason for hiding this comment

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

The documentation comment for the RemoveLocalReference method appears to have been accidentally removed. The comment "Decrease the local reference count for the ObjectID by one" should be preserved above the RemoveLocalReference declaration to maintain API documentation consistency.

Suggested change
std::string_view call_site) = 0; // Changed: read-only param
std::string_view call_site) = 0; // Changed: read-only param
/// Decrease the local reference count for the ObjectID by one.

Copilot uses AI. Check for mistakes.
@ray-gardener ray-gardener bot added the community-contribution Contributed by the community label Feb 6, 2026
MuhammadSaif700 and others added 13 commits February 6, 2026 19:23
- Split long default parameter lines for SubmitTask
- Align CreateActor comment properly
- Reformat SubmitActorTask signature
- Reformat DeserializeAndRegisterActorHandle signature
- Reformat CreateProfileEvent signature
…_spec files

- Reformat function signatures with proper line breaks and parameter alignment
- Fix comment alignment for consistency
- Split long function call arguments properly
- Apply fixes to core_worker.h, core_worker.cc, and task_spec.h
… parameters

- Convert string_view to std::string when calling functions that expect const std::string&
- Fix Disconnect, ForceExit, PushError, IsRuntimeEnvInfoEmpty, cache Put
- Fix SetCommonTaskSpec, SetNormalTaskSpec, SetActorCreationTaskSpec, SetActorTaskSpec
- Fix ActorHandle constructor and ProfileEvent constructor calls
- Split Disconnect() arguments across lines
- Split RequestShutdown() arguments across lines
- Split PushError() arguments across lines
- Split IsRuntimeEnvInfoEmpty() argument across lines
- Split cache Put() arguments across lines
- Split SetNormalTaskSpec() with opening paren break
- Split SetActorTaskSpec() with opening paren break
- Split ProfileEvent constructor arguments across lines
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

MuhammadSaif700 and others added 2 commits February 9, 2026 00:11
…oImpl

Convert string_view to std::string once and reuse, instead of creating
3 separate std::string objects. This follows the 'convert once, reuse'
pattern and reduces heap allocations in the task-submission hot path.

Addresses cursor bot review feedback.
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant