Commit Graph

135 Commits

Author SHA1 Message Date
Karoy Lorentey
a3fdcfa43f Reword note on wrapping arithmetic 2022-02-03 11:39:44 -08:00
Cory Benfield
b1683c89be Remove as much checked math as possible from buffer pointers.
This patch removes as much checked math as I was able to find from the
unsafe buffer pointers types. In particular, I removed two kinds of
checked math:

1. Checked math that was provably unable to trap due to prior operations
   done in the same function, or in a function that must have been
   called before the current one. For example, in
   index(_:offsetBy:limitedBy:), when we do the final index addition we
   have already validated that it must be in-bounds. Therefore, it
   cannot overflow.
2. Checked math that can only fail if the user is misusing the indices.
   As previously discussed with Karoy and Andrew, the unsafe raw buffer
   types are not obligated to crash if you misuse their indices, they
   are free to invoke undefined behaviour. In these cases, I added
   defensive overflow checks in debug mode.

The result of this change should be reductions in code size in almost
all usage sites of the raw buffer types.
2021-05-21 09:25:32 +01:00
Doug Gregor
9579390024 [SE-0304] Rename ConcurrentValue to Sendable 2021-03-18 22:48:20 -07:00
Karoy Lorentey
0836707303 [stdlib] Deprecate MutableCollection._withUnsafeMutableBufferPointerIfSupported (#36003)
* [stdlib] Deprecate MutableCollection._withUnsafeMutableBufferPointerIfSupported

In Swift 5.0, [SE-0237] introduced the public `MutableCollection.withContiguousMutableStorageIfAvailable` method. It’s time we migrated off the old, underscored variant and deprecated it.

The default `MutableCollection.sort` and `.partition(by:)` implementations are currently calling this hidden method rather than the documented interface, preventing custom Collection implementations from achieving good performance, even if they have contiguous storage.

[SE-0237]: https://github.com/apple/swift-evolution/blob/master/proposals/0237-contiguous-collection.md

* [test] Update tests for stdlib behavior changes

* Update stdlib/private/StdlibCollectionUnittest/CheckMutableCollectionType.swift

Co-authored-by: Nate Cook <natecook@apple.com>

* Update stdlib/private/StdlibCollectionUnittest/CheckMutableCollectionType.swift

Co-authored-by: Nate Cook <natecook@apple.com>

* Apply suggestions from code review

Co-authored-by: Nate Cook <natecook@apple.com>

* [test] LoggingMutableCollection: Fix logging targets

* [stdlib] Fix warning by restoring original workaround

Co-authored-by: Nate Cook <natecook@apple.com>
2021-02-26 11:32:27 -08:00
Doug Gregor
1a1f79c0de Introduce safety checkin for ConcurrentValue conformance.
Introduce checking of ConcurrentValue conformances:
- For structs, check that each stored property conforms to ConcurrentValue
- For enums, check that each associated value conforms to ConcurrentValue
- For classes, check that each stored property is immutable and conforms
  to ConcurrentValue

Because all of the stored properties / associated values need to be
visible for this check to work, limit ConcurrentValue conformances to
be in the same source file as the type definition.

This checking can be disabled by conforming to a new marker protocol,
UnsafeConcurrentValue, that refines ConcurrentValue.
UnsafeConcurrentValue otherwise his no specific meaning. This allows
both "I know what I'm doing" for types that manage concurrent access
themselves as well as enabling retroactive conformance, both of which
are fundamentally unsafe but also quite necessary.

The bulk of this change ended up being to the standard library, because
all conformances of standard library types to the ConcurrentValue
protocol needed to be sunk down into the standard library so they
would benefit from the checking above. There were numerous little
mistakes in the initial pass through the stsandard library types that
have now been corrected.
2021-02-04 03:45:09 -08:00
Karoy Lorentey
94a7eeebe5 [stdlib] Document sources of code bloat 2020-12-11 18:31:00 -08:00
Karoy Lorentey
1f92df093c [stdlib] Add an unsafe U[M]BP initializer to work around some inliner test failures 2020-12-09 19:31:28 -08:00
Karoy Lorentey
282a1408de [stdlib] U[R]BP: Restore compile-time condition lost with removed precondition 2020-12-08 22:25:16 -08:00
Karoy Lorentey
27978d16b9 [stdlib] Eliminate an overflow check in U[M]BP.distance 2020-12-04 02:06:22 -08:00
Karoy Lorentey
d52cd8243a [stdlib] Add a missing debug mode check to U*BP.init(rebasing:)
`Slice` does not technically guarantee that its indices are valid in its base, so these initializers accidentally allowed the creation of obviously out-of-bounds buffers.
2020-12-04 02:06:22 -08:00
Karl
74a8254d40 Make UBP nil/count checks debug-mode only
Every other Unsafe(Mutable)BufferPointer precondition in this file is debug-mode only.

This information is already part of the documentation for this function, and it's really hard to get rid of these branches and traps otherwise.
2020-12-04 02:06:21 -08:00
Cory Benfield
300f4da1bb [stdlib] Simplify buffer pointer initialization.
UnsafeRawBufferPointer was not made self-slicing, but is instead sliced
by Slice<UnsafeRawBufferPointer>. When working with
UnsafeRawBufferPointer objects it is quite common to want to transform
back into the underlying collection, which is why the
UnsafeRawBufferPointer provides a constructor from its subsequence:
UnsafeRawBufferPointer.init(rebasing:).

Unfortunately for an initializer on this extremely low-level type, this
initializer is surprisingly expensive. When disassembled on Linux it
emits 7 separate traps and 11 branches. This relative heft means this
method often gets outlined, which is a shame, as several of the branches
could often be eliminated due to checks elsewhere in functions where
this initializer is used.

Almost all of these branches and almost all of the traps are
unnecessary. We can remove them by noting that it is impossible to
construct a Slice whose endIndex is earlier than its startIndex. All
Slice inits are constructed with bounds expressed as Range, and Range
enforces ordering on its endpoints.

For this reason, we can do unchecked arithmetic on the start and end
index of the slice, and remove 5 traps in one fell swoop. This greatly
cheapens the cost of the initializer, improving its odds of being
inlined and having even more of its branches optimised away.

For what it's worth, I also considered trying to remove the last two
preconditions. Unfortunately I concluded I couldn't confidently do that.
I wanted to remove them based on the premise that a valid Slice must
have indices that were in-bounds for its parent Collection, and so we
could safely assume that if the parent base address was nil the count
would have to be zero, and that adding start index to the base address
would definitely not overflow. However, the existence of the
Slice.init(base:bounds:) constructor led me to be a bit suspicions of
removing those checks. Given that
UnsafeRawBufferPointer.init(start:count:) enforces those invariants, I
decided it was safest for us to continue to do that.
2020-11-27 16:57:50 +00:00
Alex Martini
710897852a Remove stray backtick in doc comment.
Fixes <rdar://problem/63195315>
2020-05-13 13:11:29 -07:00
Hamish Knight
1bc56dcc11 [stdlib] Mark some parameters @_nonEphemeral
These include the pointer-to-pointer and pointer-to-buffer-pointer
initialiser parameters amongst a couple of others, such as
`Unmanaged.fromOpaque`, and the source for the `move[...]` family of
methods.
2019-11-03 08:42:26 -08:00
Paul Hudson
06f82a53b5 Replaced the majority of ' : ' with ': '. 2019-07-18 20:46:07 +01:00
Ben Cohen
e9d4687e31 De-underscore @frozen, apply it to structs (#24185)
* De-underscore @frozen for enums

* Add @frozen for structs, deprecate @_fixed_layout for them

* Switch usage from _fixed_layout to frozen
2019-05-30 17:55:37 -07:00
Andrew Trick
28a529ceed Fix crashes in Unsafe[Raw]BufferPointer with nil baseAddress.
This fix updates various initializers to handle incoming empty buffers
that happen to have a nil base. They should simply create another
buffer with nil base rather than crashing!

It is valid for an Unsafe[Raw]BufferPointer can have a nil base
address. This allows round-tripping with C code that takes a
pointer/length pair and uses `0` as the pointer value.

The original design wrongly assumed that we would use a sentinel value
for empty buffers and was never updated for or tested with the current
design.

Fixes <rdar://problem/47946984> Regression in Foundation.Data's
UnsafeBufferPointer constructor.
2019-02-11 13:38:48 -08:00
Andrew Trick
a557976f95 Remove useless optional unwrap from Unsafe[Raw]BufferPointer subscript.
It is unfortunate that `Unsafe[Raw]BufferPointer._pointer` and
`baseAddress` are declared Optional. This leaves extra runtime checks
in the code in the most performance critical paths. Contrast this with
Array, which uses an sentinal pointer for the empty representation.

This forces us to use _unsafelyUnwrappedUnchecked whenever we just
want to dereference a non-empty buffer pointer.

Fixes SR-9809: swiftc appears to make some sub-optimal optimization choices
2019-02-03 21:25:41 -08:00
Andrew Trick
0b5fa792e1 Force manual allocation (via Unsafe*Pointer) to use >= 16 alignment.
This fixes the Windows platform, where the aligned allocation path is
not malloc-compatible. It won't have any observable difference on
Darwin or Linux, aside from manually allocated memory on Linux now
being consistently 16-byte aligned (heap objects will still be 8-byte
aligned on Linux).

It is unfortunate that we can't guarantee Swift-allocated memory via
Unsafe*Pointer is malloc compatible on Windows. It would have been
nice for that to be a cross platform guarantee since it's normal to
allocate in C and deallocate in Swift or vice-versa. Now we have to
tell developers to always use _aligned_malloc/_aligned_free when
transitioning between Swift/C if they expect their code to work on
Windows.

Even though this fix isn't required today on Darwin/Linux, it makes
good sense to guarantee that the allocation/deallocation paths are
consistent.

This is done by specifying a constant that stdlib can use to round up
alignment, _swift_MinAllocationAlignment. The runtime asserts that
this constant is greater than MALLOC_ALIGN_MASK for all platforms.
This way, manually allocated buffers will always use the aligned
allocation path. If users specify an alignment less than m

round up so users don't need
to pass the same alignment to deallocate the buffer). This constant
does not need to be ABI.

Alternatives are:

1. Require users of Unsafe*Pointer to specify the same alignment
   during deallocation. This is obviously madness.

2. Introduce new runtime entry points:
   swift_alignedAlloc/swift_alignedDealloc, introduce corresponding
   new builtins, and have Unsafe*Pointer always call those. This would
   make the runtime API a little more obvious but would introduce
   complexity in other areas of the compiler and it doesn't have any
   other significant benefit. Less than 16-byte alignment of manually
   allocated buffers on Linux is a non-goal.
2019-01-03 12:35:51 -08:00
Ben Cohen
ad50a39b12 [stdlib] Add withContiguous{Mutable}StorageIfAvailable (#21092)
* Add MutableCollection.withContiguousMutableStorageIfAvailable

* Add withContiguousMutableStorageIfAvailable impls

* Add tests on concrete types

* Add Sequence.withContiguousStorageIfAvailable

* Implement withContiguousStorageIfAvailable in concrete types
2018-12-07 10:01:18 -08:00
Michael Ilseman
3a0ac0270d [stdlib] Unchecked subscript on UnsafeBufferPointer
Add a use an unchecked subscript on UnsafeBufferPointer, which skips
debugPrecondition checks (in case we're not inlined) as well as a
force-unwrap check.
2018-11-16 11:12:29 -08:00
Ben Cohen
1673c12d78 [stdlib] Replace "sanityCheck" with "internalInvariant" (#20616)
* Replace "sanityCheck" with "internalInvariant"
2018-11-15 20:50:22 -08:00
Nate Cook
e5c1567957 [stdlib] Switch to a stable sort algorithm (#19717)
This switches the standard library's sort algorithm from an in-place
introsort to use a modified timsort, a stable, adaptive sort that
merges runs using a temporary buffer. This implementation performs
straight merges instead of adopting timsort's galloping strategy.

In addition to maintaining the relative order of equal/non-comparable
elements, this algorithm outperforms the introsort on data with any
intrinsic structure, such as runs of ascending or descending elements
or a significant number of equality collisions.
2018-11-07 00:05:04 -06:00
Ben Cohen
6a0fd51beb Switch UnsafeMutableBufferPointer.subscript to use _modify (#19771) 2018-10-08 16:23:01 -07:00
Ben Cohen
eeb6721fb6 Inlineable: unsafe performance 2018-07-06 12:05:29 -07:00
Ben Cohen
a4230ab2ad [stdlib] Update stdlib to 4.0 and reorganize compatibility shims (#17580)
* Update stdlib to 4.0 and move all compatibility shims into a dedicated source file
2018-06-29 06:26:52 -07:00
Ben Cohen
92b6d8cb8f Remove inlineability from mirrors (#17476) 2018-06-25 19:54:13 -07:00
Erik Eckstein
72038b58a3 stdlib: change the representation of UnsafeBufferPointer from start+end pointers to start-pointer + count.
This saves a few instructions for some operations, like getting the count.
Also, it avoids the check for unwrapping the optional end pointer. For example, iterating over an unsafe buffer now has no overhead.

Also remove the _unboundedStartingAt initializer, which is not needed anymore.
2018-05-29 11:01:03 -07:00
eeckstein
c2cc3ee6cf stdlib: replace an obsolete initialize function in UnsafeBufferPointer.swapAt with the new one. (#16270)
This brings back performance on stdlib sort because the obsoleted function in UnsafePointer is not inlinable.
2018-05-01 10:48:32 -07:00
Nate Cook
58933d88c5 [stdlib] Rename index(...) methods to firstIndex(...)
A la SE-204.
2018-04-21 18:07:25 -05:00
Slava Pestov
e1f50b2d36 SE-0193: Rename @_inlineable to @inlinable, @_versioned to @usableFromInline 2018-03-30 21:55:30 -07:00
Andrew Trick
afbcbfdfa7 Merge pull request #13071 from glessard/umbp-nonmutating-swapat
nonmutating swapAt implementations for UnsafeMutable(Raw)BufferPointer
2018-03-15 16:35:47 -07:00
swift-ci
9bda1e5c55 Merge pull request #13340 from glessard/umbp-failearlyrangecheck 2018-03-08 05:38:39 -08:00
Guillaume Lessard
0c95f902f0 restore debug-mode bounds checking to *BufferPointer.swapAt 2018-03-06 09:04:17 -07:00
Guillaume Lessard
116de2b3b2 non-mutating swapAt implementations for UnsafeMutable(Raw)BufferPointer 2018-03-06 09:04:16 -07:00
swift-ci
abbaa47614 Merge pull request #12504 from glessard/umbp-nonmutating-setter 2018-03-06 06:37:58 -08:00
Ben Cohen
9ee856f386 [stdlib][WIP] Eliminate (Closed)CountableRange using conditional conformance (#13342)
* Make Range conditionally a Collection

* Convert ClosedRange to conditionally a collection

* De-gyb Range/ClosedRange, refactoring some methods.

* Remove use of Countable{Closed}Range from stdlib

* Remove Countable use from Foundation

* Fix test errors and warnings resulting from Range/CountableRange collapse

* fix prespecialize test for new mangling

* Update CoreAudio use of CountableRange

* Update SwiftSyntax use of CountableRange

* Restore ClosedRange.Index: Hashable conformance

* Move fixed typechecker slowness test for array-of-ranges from slow to fast, yay

* Apply Doug's patch to loosen test to just check for error
2018-02-01 20:59:28 -08:00
Nate Cook
0782b482b3 [stdlib] Documentation improvements
- Revise Equatable and Hashable for synthesized requirements
- Complete Strideable and stride(from:...:by:) documentation
- Revise DoubleWidth type docs
- Add complexity notes for Set.index(of:) and .contains(_:)
- Fix typos in Set.formUnion docs
- Add missing axioms for SetAlgebra (SR-6319)
- Improve guidance for description and debugDescription
- Add note about the result of passing duplicate keys to
  Dictionary(uniqueKeysWithValues:)
- Fix typo in BinaryInteger docs
- Update Substring docs with better conversion example
- Improve docs for withMemoryRebound and isKnownUniquelyReferenced
- Add missing docs not propagated from protocols
2018-01-05 17:06:44 -06:00
Ben Cohen
9361a6b66f [stdlib] Nest Iterator and Index types for various stdlib types (#13489)
* Nest various top-level Iterator and Index types, and flatten extensions.

* Fix tests from nesting iterator

* Nest Unsafe*BufferPointer.Iterator, extensionify UnsafeBufferPointer

* Degyb LazyCollection

* Nest Flatten iterator and index
2017-12-18 15:27:35 -08:00
Guillaume Lessard
68e50d3b6a [stdlib] add debug-mode checks in _failEarlyRangeCheck 2017-12-15 16:48:37 -07:00
Guillaume Lessard
894585f1bd a non-mutating range subscript setter for UnsafeMutableBufferPointer 2017-12-08 14:14:52 -07:00
Ben Cohen
4ddac3fbbd [stdlib] Eradicate IndexDistance associated type (#12641)
* Eradicate IndexDistance associated type, replacing with Int everywhere

* Consistently use Int for ExistentialCollection’s IndexDistance type.

* Fix test for IndexDistance removal

* Remove a handful of no-longer-needed explicit types

* Add compatibility shims for non-Int index distances

* Test compatibility shim

* Move IndexDistance typealias into the Collection protocol
2017-12-08 12:00:23 -08:00
Ben Cohen
c4f0b5fe94 [stdlib] Adopt conditional conformance for Indices, Slice, ReversedCollection (#12913)
* Refactor Indices and Slice to use conditional conformance

* Replace ReversedRandomAccessCollection with a conditional extension

* Refactor some types into struct+extensions

* Revise Slice documentation

* Fix test cases for adoption of conditional conformances.

* [RangeReplaceableCollection] Eliminate unnecessary slicing subscript operator.

* Add -enable-experimental-conditional-conformances to test.

* Gruesome workaround for crasher in MutableSlice tests
2017-11-30 09:10:22 -08:00
taylor swift
c85880899d implement SE 184: add allocation methods to Unsafe buffer pointers, drop all parameters from deallocation, adjust namings, and add repeated-value assignment methods 2017-11-17 21:28:03 -08:00
Nate Cook
0b62b0608d [stdlib] Doc revisions
- Add missing docs & parameter lists
- Remove deprecated `characters` usage in examples
- Revise documentation for Mirror and CVarArg
- Revise documentation for swap(_:_:)
- Various typo and grammar fixes
2017-11-07 11:11:23 -06:00
Guillaume Lessard
515b455aa1 fix a typo 2017-10-19 04:22:36 -06:00
Doug Gregor
42968b2069 Eliminate a number of uses of the *Indexable protocols. 2017-10-01 15:08:23 -07:00
Max Moiseev
53b8419279 [stdlib] Make all the stdlib APIs @_inlineable
This change in theory should allow us to remove a special stdlib-only
sil-serialize-all compilation mode.

<rdar://problem/34138683>
2017-09-29 11:26:56 -07:00
Maxim Moiseev
ee5fb33656 [stdlib] Remove the Grand Renaming artifacts of Swift 3 era 2017-08-28 15:54:11 -07:00
Nate Cook
781f6326bd [stdlib] Various documentation revisions and fixes
- Revisions to unsafeDowncast and withVaList
- Fix the Int64/UInt64 discussion
- Buffer pointer revisions
- Fix Optional example to use new integer methods
- Revise and correct some UnsafeRawBufferPointer docs
- Fix symmetricDifference examples
- Fix wording in FloatingPoint.nextDown
- Update ImplicitlyUnwrappedOptional
- Clarify elementsEqual
- Minor integer doc fixes
- Comment for _AppendKeyPath
- Clarification re collection indices
- Revise RangeExpression.relative(to:)
- Codable revisions
2017-07-31 10:56:53 -05:00