Skip to content
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

[SR-10225] String methods provided by the Foundation overlay can result in non-UTF8-backed strings #3503

Open
lilyball mannequin opened this issue Mar 28, 2019 · 0 comments

Comments

@lilyball
Copy link
Mannequin

lilyball mannequin commented Mar 28, 2019

Previous ID SR-10225
Radar None
Original Reporter @lilyball
Type Bug
Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Bug
Assignee None
Priority Medium

md5: 523fca5765ccad40e49e24dfab888b49

Issue Description:

Now that String's native storage is UTF-8, ending up with strings backed by UTF-16 storage can be more of a performance penalty (in particular, code that is optimizing for speed is now going to be optimized around a UTF-8 representation instead of a UTF-16 representation). As such, we should try to reduce the amount of UTF-16–backed strings we work with in general.

Unfortunately, a lot of core String functionality is provided by the Foundation overlay, and these methods all end up bridging to NSString in order to invoke the existing Foundation methods. Not only does this incur the bridging cost when going to Obj-C, but any strings created from these methods will be backed by NSString and thus will be UTF-16 if not ASCII.

I recognize that a number of these methods have complex behavior that would be a lot of effort to reproduce (in particular, anything that relies on the locale), but there's also basic methods that we could easily reimplement ourselves, such as components(separatedBy:).

As a trivial example, the following returns a UTF-16–backed string:

"héllo world".components(separatedBy: " ")[0]
@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

0 participants