Skip to content

Use into_owned() instead of to_string() on Cow<str>#152

Merged
MegaRedHand merged 1 commit intomainfrom
cleanup-cow-into-owned
Feb 27, 2026
Merged

Use into_owned() instead of to_string() on Cow<str>#152
MegaRedHand merged 1 commit intomainfrom
cleanup-cow-into-owned

Conversation

@pablodeymo
Copy link
Collaborator

Summary

  • Replace .to_string() with .into_owned() on Cow<str> returned by String::from_utf8_lossy() in the req/resp codec
  • Avoids going through the Display trait for the String conversion
  • Two instances in crates/net/p2p/src/req_resp/codec.rs (lines 176 and 230)

Test plan

  • cargo check passes

…_utf8_lossy

Avoids going through the Display trait for the String conversion.
@github-actions
Copy link

🤖 Kimi Code Review

The changes look good and are performance-oriented micro-optimizations:

  1. Line 176: String::from_utf8_lossy(&message).into_owned() avoids an extra allocation compared to .to_string() which creates a String and then immediately converts it to another String.

  2. Line 230: Same optimization as above for the error message handling.

Both changes are correct and idiomatic Rust. The .into_owned() method is the proper way to get an owned String from a Cow<str> returned by from_utf8_lossy, avoiding the double allocation that .to_string() would incur.

These are safe, low-risk optimizations that improve performance without affecting correctness.


Automated review by Kimi (Moonshot AI) · custom prompt

@github-actions
Copy link

🤖 Codex Code Review

No issues found in this diff. The change from to_string() to into_owned() on Cow<str> is idiomatic and avoids an extra clone in the non-borrowed case. This is a safe micro-optimization and does not alter behavior. crates/net/p2p/src/req_resp/codec.rs:173, crates/net/p2p/src/req_resp/codec.rs:227.

I didn’t run tests.


Automated review by OpenAI Codex · custom prompt

@github-actions
Copy link

🤖 Claude Code Review

The change is small, correct, and well-motivated. Here's the review:


PR #152 Review: Use into_owned() instead of to_string() on Cow<str>

Verdict: Approve — clean micro-optimization, no concerns.

What the change does

String::from_utf8_lossy(&bytes) returns a Cow<str>:

  • Cow::Borrowed(&str) when the bytes are valid UTF-8 (zero-copy borrow)
  • Cow::Owned(String) when invalid UTF-8 is found (replaces bad sequences with U+FFFD)

.to_string() on a Cow<str> routes through the Display trait and always allocates a fresh String, even when the Cow is already Owned — resulting in two allocations in the invalid-UTF-8 case.

.into_owned() is the semantically correct method here:

  • Cow::Borrowed → clones once (same as before)
  • Cow::Owned → moves the inner String directly, zero additional allocation

Analysis

Both changed sites (codec.rs:176 and codec.rs:230) are in error-response handling paths, so they only fire when a peer sends a non-SUCCESS response code. The optimization avoids a redundant allocation in the Cow::Owned case (invalid UTF-8 in the error message bytes).

The logic and behavior are otherwise identical — no correctness risk.

The fallback on line 231 (.unwrap_or_else(|_| "<invalid error message>".to_string())) is unrelated to this PR and is fine as-is since .to_string() on a string literal is the idiomatic way to produce an owned String there.

Minor observation (not blocking)

The error_str variable at line 176 is computed and logged but the function then returns Response::error(code, message) using the raw message bytes — error_str is only used for the trace! log. This is existing behavior and not introduced by this PR, but worth noting if someone later wonders why both exist.


Clean, correct, and idiomatic. Good change.


Automated review by Claude (Anthropic) · custom prompt

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Feb 26, 2026

Greptile Summary

Optimized String conversion from Cow<str> by replacing .to_string() with .into_owned() in error message handling code. This change avoids unnecessary allocations when the Cow is already owned, improving performance in error response paths.

  • Replaced .to_string() with .into_owned() on two instances where String::from_utf8_lossy() is called (lines 176 and 230)
  • More idiomatic and efficient for converting Cow<str> to String
  • No behavior change, purely a performance optimization

Confidence Score: 5/5

  • This PR is safe to merge with no risk
  • This is a straightforward optimization that replaces .to_string() with the more efficient .into_owned() method on Cow<str>. The change is idiomatic, correct, and only affects two lines. No behavior is modified, only performance is improved.
  • No files require special attention

Important Files Changed

Filename Overview
crates/net/p2p/src/req_resp/codec.rs Replaced .to_string() with .into_owned() on Cow<str> for more efficient String conversion in error handling paths

Last reviewed commit: 785cd22

Copy link

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 optimizes string conversions in the request/response codec by replacing .to_string() with .into_owned() when converting Cow<str> (returned by String::from_utf8_lossy()) to owned String. This avoids the overhead of going through the Display trait and directly obtains an owned string.

Changes:

  • Replace .to_string() with .into_owned() on two Cow<str> conversions in error message handling

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

@MegaRedHand MegaRedHand merged commit b0fc303 into main Feb 27, 2026
11 checks passed
@MegaRedHand MegaRedHand deleted the cleanup-cow-into-owned branch February 27, 2026 00:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants