Skip to content

🙅🏼‍♂️ [stdlib] Add method for accessing full storage capacity of array #17389

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

Closed

Conversation

natecook1000
Copy link
Member

@natecook1000 natecook1000 commented Jun 21, 2018

This is an implementation of a withUnsafeMutableBufferPointerToStorage method and init(unsafeUninitializedCapacity:initializingWith:) for arrays. Under review here: https://forums.swift.org/t/se-0223-accessing-an-arrays-uninitialized-buffer/15194

To do:

  • Check inlining annotations
  • Add @_semantics attributes?
  • Revise documentation
  • Duplicate onto ContiguousArray

Resolves #45677.

@natecook1000 natecook1000 added the swift evolution pending discussion Flag → feature: A feature that has a Swift evolution proposal currently in review label Jun 21, 2018
@natecook1000 natecook1000 force-pushed the nc-array-uninitialized branch from 27e313f to 365b5ec Compare June 21, 2018 06:27
@natecook1000
Copy link
Member Author

@swift-ci Please smoke test

@natecook1000
Copy link
Member Author

natecook1000 commented Jun 21, 2018

Ugh — it looks like adding init(minimumCapacity: Int) to RangeReplaceableCollection changes the overload selection in [1: 10, 2: 20].mapValues(String.init) so that the new initializer is being chosen instead of the integer-to-string-converting initializer. That's really unfortunate.

let problem = [1: 10, 2: 20].mapValues(String.init)
// old: [1: "10", 2: "20"]
// new: [1: "", 2: ""]

@natecook1000
Copy link
Member Author

@swift-ci Please smoke test

inoutBufferPointer.count == capacity,
"""
Array withFullCapacityUnsafeMutableBufferPointer: \
replacing the buffer is not allowed
Copy link
Contributor

Choose a reason for hiding this comment

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

Then why is it being passed inout?

Copy link
Member Author

Choose a reason for hiding this comment

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

It looks like I'm cargo-culting the rationale for the buffer being inout in withUnsafeMutableBufferPointer, which is a very old behavior (introduced in c5c17e2). It looks like that was probably because the type used to access the elements then (UnsafeMutableArray) needed to be mutable to access its subscript setter, unlike UnsafeMutableBufferPointer.

Copy link
Member Author

Choose a reason for hiding this comment

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

This should probably prefer the correct semantics (non-inout) over matching the existing method, esp. since you can't pass the same closure to both anyway. Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

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

Ahhh right, you do need mutation if you want to do collectiony things on the UnsafeMutableBufferPointer. But in this case it's not safe to do collectiony things unless you take a slice anyway, at which point you can use var.

@natecook1000 natecook1000 force-pushed the nc-array-uninitialized branch from 74bcc36 to 6292c4b Compare August 6, 2018 20:56
@natecook1000
Copy link
Member Author

@swift-ci Please smoke test

@natecook1000
Copy link
Member Author

Closing until there's a new SE pitch.

@natecook1000 natecook1000 deleted the nc-array-uninitialized branch August 29, 2019 19:36
@AnthonyLatsis AnthonyLatsis removed the swift evolution pending discussion Flag → feature: A feature that has a Swift evolution proposal currently in review label Apr 18, 2023
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.

3 participants