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-14491] UnsafeMutableBufferPointer.initialize(from:) is slow with slices #56846

Closed
karwa opened this issue Apr 15, 2021 · 3 comments
Closed
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. performance standard library Area: Standard library umbrella

Comments

@karwa
Copy link
Contributor

karwa commented Apr 15, 2021

Previous ID SR-14491
Radar rdar://problem/76728166
Original Reporter @karwa
Type Bug
Status Resolved
Resolution Done
Environment

Swift 5.4

Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Bug
Assignee @glessard
Priority Medium

md5: 993949c9c0f9f481d20f2523e3b0a27a

Issue Description:

This very short, very simple demonstration shows what I mean:

Godbolt.org

Notice that the first function, "copy", gets turned in to an enormous generic specialization which iterates through every position in the slice and copies each byte one-by-one.

The second function, "copy2", uses "withContiguousStorageIfAvailable", and gets turned in to a memcpy.

In my testing, using wCSIA to initialize a destination buffer from slices of a source buffer can be around 8-10% faster than using `initialize(from: )`, depending on the length of the content.

Unfortunately, UMBP does not expose a function which uses wCSIA and otherwise falls-back to naive element-by-element copying. Worse - the current function's signature returns an iterator from the source collection, so the implementation can't be adjusted to make use of it. We need a new function on UMBP.

@typesanitizer
Copy link

@swift-ci create

@glessard
Copy link
Contributor

glessard commented Jun 9, 2021

It seems like the solution to SR-11957 (28883) may be applicable to this. initialize(from: ) calls into the stdlib function _copyContents(), but Slice does not have its own version of that. Array and ArraySlice's versions of _copyContents effectively have a fast path through the pointer-to-pointer initialization function. With a little pointer arithmetic, Slice might be able to use its base's withContiguousStorageIfAvailable.

@glessard
Copy link
Contributor

#38677

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. performance standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

4 participants