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

[pitch] Span-providing properties in the standard library #2620

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

glessard
Copy link
Contributor

Continuing the work of adding Span, this is a proposal to add the ability to use Span as computed properties on standard library types, including Array.

var bytes: RawSpan { get }
}

extension String.UTF8View {
Copy link
Contributor

Choose a reason for hiding this comment

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

Q: What is the behavior (and what are the performance characteristics) of this operation when a String has storage other than contiguous UTF-8? Would it be desirable to return an optional, or alternatively require the user to know or have called makeContiguousUTF8() on the String as a precondition?

Copy link
Contributor Author

@glessard glessard Nov 19, 2024

Choose a reason for hiding this comment

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

Behind the scenes, we are implementing a new "lazy eager bridging" behaviour for bridged Array and bridged String. They will always succeed, but they will sometimes entail an allocation and copy (i.e. "usually O(1), sometimes O(n)".) An initial step is here. For native non-bridged instances, always O(1).

Copy link
Contributor

Choose a reason for hiding this comment

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

Cool! The implementation details would of course be out of scope for this text, but might be worth calling out here the performance implications for calling storage and bytes on these types, pros and cons for usability vs predictability, etc. (given that this is supposed to be a safe, performant API).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I appended a paragraph to the detailed design to spell this out.

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