Skip to content

Use QString::as_slice for String conversions #1323

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 7, 2025

Conversation

jnbooth
Copy link
Contributor

@jnbooth jnbooth commented Aug 1, 2025

Currently, if you have a qstring: QString and call qstring.to_string(), here is what happens:

  1. We allocate a QByteArray.
  2. Qt fills the byte array with qstring's UTF-8 data.
  3. We allocate a String and copy the data into it.
  4. Rust allocates a String of its own.
  5. Rust copies the data from our string into its own string.

That's a lot of work and it's unnecessary. All we have to do is get the UTF-16 data of qstring, which is a constant-time operation, and write that into the formatter. String::from(&qstring) is still a bit more efficient than qstring.to_string() with this approach, but the gap is much smaller than before. Arguably small enough that we could just recommend qstring.to_string() for simplicity's sake. I think the only difference is that String::from(&qstring) reserves as much space as it needs up front.

Copy link

codecov bot commented Aug 1, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (0d38fb2) to head (4a04dbf).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #1323   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           75        75           
  Lines        12772     12772           
=========================================
  Hits         12772     12772           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@LeonMatthesKDAB LeonMatthesKDAB added this pull request to the merge queue Aug 7, 2025
Merged via the queue into KDAB:main with commit 5881415 Aug 7, 2025
16 checks passed
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.

2 participants