Skip to content

Conversation

@bschoenmaeckers
Copy link
Member

@bschoenmaeckers bschoenmaeckers commented Oct 6, 2025

Allow extracting a Path and OsStr without copying when it is a valid utf8 string.

@bschoenmaeckers bschoenmaeckers marked this pull request as draft October 6, 2025 17:44
@codspeed-hq
Copy link

codspeed-hq bot commented Oct 6, 2025

CodSpeed Performance Report

Merging #5497 will not alter performance

Comparing bschoenmaeckers:extract_cow_path_osstr (cf72c95) with main (4906a61)

Summary

✅ 98 untouched

@bschoenmaeckers bschoenmaeckers marked this pull request as ready for review October 6, 2025 18:47
Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

Good idea, with possibly more to go 🤔

Comment on lines +159 to +160
if let Ok(s) = obj.extract::<&str>() {
return Ok(Cow::Borrowed(s.as_ref()));
}

obj.extract::<OsString>().map(Cow::Owned)
Copy link
Member

Choose a reason for hiding this comment

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

Based on the above comment about &OsStr, I think we might be able to make this always not copy on unix platforms?

@bschoenmaeckers
Copy link
Member Author

bschoenmaeckers commented Oct 7, 2025

Good idea, with possibly more to go 🤔

I'm unsure if this comment is correct. PyUnicode_EncodeFSDefault creates a new owned reference in Linux as well. So we cannot borrow from the original object.

@davidhewitt
Copy link
Member

Ah right, makes sense. Willing to delete that comment in this PR perhaps? Otherwise LGTM!

Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

Actually sorry quick suggestion for the tests.

}

// Test extracting both valid UTF-8 and non-UTF-8 strings
test_extract(py, "Hello\0\n🐍");
Copy link
Member

Choose a reason for hiding this comment

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

Actually shall we assert here that this is the borrowed form as expected, and similar for the path tests?

Will need a cfg but not too bad.

Copy link
Member Author

Choose a reason for hiding this comment

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

No problem! Will add this to the test!

@davidhewitt davidhewitt disabled auto-merge October 8, 2025 07:31
@bschoenmaeckers bschoenmaeckers force-pushed the extract_cow_path_osstr branch 3 times, most recently from f9d1676 to 3f5e731 Compare October 9, 2025 11:05
@bschoenmaeckers bschoenmaeckers added this pull request to the merge queue Oct 9, 2025
Merged via the queue into PyO3:main with commit d9e2c50 Oct 9, 2025
43 checks passed
@bschoenmaeckers bschoenmaeckers deleted the extract_cow_path_osstr branch October 9, 2025 12:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants