Commit Graph

128 Commits

Author SHA1 Message Date
Karoy Lorentey
ff3cf98a2d [stdlib] ClosedRange.contains: Leave this force-inlined, not transparent
Making it transparent evidently induces new retain/release traffic in Array’s subscript, even though I can find no indication that `ClosedRange.contains` is ever called by that code path. Oh well.
2025-06-05 19:42:20 -07:00
Karoy Lorentey
d231213987 [stdlib] On second thought, making ClosedRange.contains(Range) transparent is a bad idea 2025-06-04 17:02:48 -07:00
Karoy Lorentey
7a5dbef390 [stdlib] Mark contains methods on Range/ClosedRange transparent
These are really popular shorthands for implementing bounds checks, but currently they aren’t even marked `@inline(__always)`, so in larger functions they tend to remain outlined, defeating optimizations that would otherwise happen.

The corresponding variants on the partial range types are `@_transparent`; that helps unoptimized performance, so let’s apply the same attribute here.

rdar://151177326
2025-06-04 17:02:45 -07:00
Guillaume Lessard
dfb2e2f12e [stdlib] annotate uses of Range.init(_uncheckedBounds:) 2025-03-05 18:52:11 -08:00
Guillaume Lessard
eed9c46116 [stdlib] mark _uncheckedBounds range inits as unsafe 2025-03-05 18:51:27 -08:00
Nate Cook
e12e968570 Add contains(_:) methods to (Closed)Range (#76891)
The _StringProcessing module provides a generic, collection-based
`contains` method that performs poorly for ranges and closed ranges.
This addresses the primary issue by providing concrete overloads
for Range and ClosedRange which match the expected performance for
these operations.

This change also fixes an issue with the existing range overlap tests.
The generated `(Closed)Range.overlap` tests are ignoring the "other"
range type when generating ranges for testing, so all overlap tests
are only being run against ranges of the same type. This fixes things
so that heterogeneous testing is included.
2024-11-12 11:47:24 -08:00
Doug Gregor
af49172b55 Mark Range.init(uncheckedBounds:) and ClosedRange.init(uncheckedBounds:) as @unsafe 2024-09-30 15:20:36 -07:00
Kuba Mracek
92b075b518 [embedded] Drop several #ifs and fatalError()s by adding an unavailable ExpressibleByStringInterpolation String conformance 2023-09-16 13:40:25 -07:00
Kuba Mracek
ae2e903574 [embedded] Build an initial embedded Swift standard library
This isn't a "complete" port of the standard library for embedded Swift, but
something that should serve as a starting point for further iterations on the
stdlib.

- General CMake logic for building a library as ".swiftmodule only" (ONLY_SWIFTMODULE).
- CMake logic in stdlib/public/core/CMakeLists.txt to start building the embedded stdlib for a handful of hardcoded target triples.
- Lots of annotations throughout the standard library to make types, functions, protocols unavailable in embedded Swift (@_unavailableInEmbedded).
- Mainly this is about stdlib functionality that relies on existentials, type erasure, metatypes, reflection, string interpolations.
- We rely on function body removal of unavailable functions to eliminate the actual problematic SIL code (existentials).
- Many .swift files are not included in the compilation of embedded stdlib at all, to simplify the scope of the annotations.
- EmbeddedStubs.swift is used to stub out (as unavailable and fatalError'd) the missing functionality.
2023-09-16 12:38:46 -07:00
Kuba (Brecka) Mracek
404badb49a Introduce SWIFT_ENABLE_REFLECTION to turn on/off the support for Mirrors and reflection (#33617) 2021-09-08 13:08:13 -07:00
Doug Gregor
9579390024 [SE-0304] Rename ConcurrentValue to Sendable 2021-03-18 22:48:20 -07: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
d1ef4c4386 [stdlib] Use [Closed]Range.init(_uncheckedBounds:) in more places
When the stdlib says not to check, it’s a good idea to actually not have any checking, so that we leave existing code paths unchanged. (And because we do trust that the stdlib is responsible about such things.)
2020-12-04 19:27:19 -08:00
Karoy Lorentey
c5c27c7c69 [stdlib] Add a missing debug precondition to [Closed]Range.init(uncheckedBounds:)
Unchecked APIs must still perform checks in debug builds to ensure that invariants aren’t violated.
(I.e., these aren’t a license to perform invalid operations — they just let us get rid of the checks when we know for sure they are unnecessary.)
2020-12-04 02:06:22 -08:00
Stephen Canon
457b9990e9 Add checks that the endpoints of partial ranges are not-NaN. (#33378)
We can't actually check for NaN (because the notion doesn't exist for Comparable), but we can require that the endpoint is non-exceptional by checking if it equals itself.
2020-08-10 14:06:46 -04: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
Patrick Pijnappel
87f02b173b [stdlib] Refactor range overlaps for performance 2019-05-04 09:15:21 +10:00
Ben Cohen
4363f61dec Mark ClosedRange->Range conversion inlinable 2019-01-18 17:27:27 -08:00
Max Moiseev
3b6c6cc0a5 Merge pull request #19532 from dlbuckley/SR-8649_ranges_codable
SR-8649: Range types conform to Codable
2019-01-14 14:38:32 -08:00
Dale Buckley
6ef7cca298 Added decodable error handling and changed from operator constructors to uncheckedBounds constructor 2018-12-03 08:44:04 +00:00
Ben Rimmington
c5997491da [stdlib] Remove Strideable closed range operator 2018-11-27 12:58:22 +00:00
Dale Buckley
15e8fd2b0f Split Range Codable extensions down to Encodable and Decodable extensions 2018-11-12 14:58:11 +00:00
Dale Buckley
36319599ce Changed keyed archive mechanics to unkeyed archive mechanics 2018-10-30 09:57:14 +00:00
Dale Buckley
62f8042206 FIxed typo 2018-09-25 17:00:27 +01:00
Dale Buckley
cca8bfba65 Renamed CodingKey raw names for better consistency 2018-09-25 16:31:27 +01:00
Dale Buckley
ac16b20a08 Codable cleanup 2018-09-25 14:45:23 +01:00
Dale Buckley
c45cdb7b7b Added Codable conformance for Range, ClosedRange, PartialRangeUpTo, PartialRangeThrough and PartialRangeFrom 2018-09-25 13:29:24 +01:00
Nate Cook
efb0415a61 [stdlib] More documentation revisions (#18263)
- Fix error in `last(where:)` example
- Improve MemoryLayout, UnsafePointer, and integer operator discussions
- Clean up ranges and random APIs
- Revisions to overflow operators and the SignedNumeric requirements
- Standardize on 'nonoptional' in remaining uses
2018-08-08 00:25:09 -05:00
Nate Cook
3d7dfc232b [stdlib] Update complexity docs for seq/collection algorithms (#17254)
* [stdlib] Update complexity docs for seq/collection algorithms

This corrects and standardizes the complexity documentation for Sequence
and Collection methods. The use of constants is more consistent, with `n`
equal to the length of the target collection, `m` equal to the length of
a collection passed in as a parameter, and `k` equal to any other passed
or calculated constant.

* Apply notes from @brentdax about complexity nomenclature

* Change `n` to `distance` in `index(_:offsetBy:)`

* Use equivalency language more places; sync across array types

* Use k instead of n for parameter names

* Slight changes to index(_:offsetBy:) discussion.

* Update tests with new parameter names
2018-07-24 01:01:34 -05:00
Ben Cohen
4694310e51 [stdlib] Some minor cleanup (#18130)
* Remove case destructuring to _

* Remove some Iterator.Element

* Which idiot wrote this? Oh.

* Switch NibbleSort to just use default impls... shouldn't change perf
2018-07-21 17:29:57 -07:00
Ben Cohen
a6952decab [stdlib] Remove inlineable annotation from transparent functions (#17800)
* Remove inlineable annotation from transparent functions
2018-07-07 08:47:02 -07:00
Ben Cohen
422ff83907 Inlineable: trivial implementation 2018-07-06 12:05:30 -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
Karoy Lorentey
07c1b74cc4 [stdlib] Audit inlinability of Hashable implementations
As a general rule, it is safe to mark the implementation of hash(into:) and _rawHashValue(seed:) for @_fixed_layout structs as inlinable.

However, some structs (like String guts, Character, KeyPath-related types) have complicated enough hashing that it seems counterproductive to inline them. Mark these with @effects(releasenone) instead.
2018-05-31 18:24:59 -07:00
Nate Cook
7a4e0a32f6 [stdlib] Revise documentation
This includes various revisions to the APIs landing in Swift 4.2, including:
- Random and other randomness APIs
- Hashable changes
- MemoryLayout.offset(of:)
2018-05-18 11:31:54 -05:00
Ben Cohen
f421ec6082 [stdlib] Undeprecate Countable*Range (#16360)
* Undeprecate Countable*Range

* Remove check for deprecation warnings
2018-05-07 09:52:52 -07:00
Karoy Lorentey
0342ce3b96 [SE-0206][stdlib] Remove obsolete hashValue implementations
These are now synthesized by the compiler.
(Inlinability will be different, but that seems fine.)
2018-04-30 10:17:09 +01:00
Karoy Lorentey
0fb3e88110 [SE-0206][stdlib] De-underscore Hashable.hash(into:) 2018-04-24 17:42:42 +01:00
Karoy Lorentey
45cb8b7123 [SE-0206][stdlib] De-underscore Hasher 2018-04-24 17:42:42 +01:00
Nate Cook
b1ab7d89db [stdlib] Implement sequence/collection methods for searching from the end (#13337)
This implements the new last(where:), and lastIndex(of/where:) methods as
extensions on `BidirectionalCollection`, which partially implements SE-204.
The protocol requirements for `Sequence` and `Collection` as described
in the proposal need to wait until there's a solution for picking up the
specialized versions in types that conditionally conform to `BidirectionalCollection`.
2018-04-23 20:47:26 -05:00
Karoy Lorentey
ccdc218cbd [stdlib] _Hasher: append => combine 2018-04-18 14:18:44 +01:00
Ben Cohen
ca7aea02e9 [stdlib] Add compatibility typealias for CountablePartialRangeFrom (#15989)
* Add compatibility typealias for CountablePartialRangeFrom
2018-04-17 15:48:54 -07:00
Slava Pestov
e1f50b2d36 SE-0193: Rename @_inlineable to @inlinable, @_versioned to @usableFromInline 2018-03-30 21:55:30 -07:00
Karoy Lorentey
e485c1506b Merge pull request #15382 from lorentey/conditional-hashable2
[SE-0143][stdlib] Conditionally conform stdlib types to Hashable
2018-03-28 21:45:02 +01:00
Doug Gregor
e0d423a506 Add appropriate bounds to CountableClosedRange and CountableRange.
Previously, both CountableRange and CountableClosedRange required
their bound types to be Strideable with a Stride type that conforms to
SignedInteger. Those constraints were not present on the generic
typealiases that replaced these structs. Add them back now, which
fixes the GRDB source compatibility regression.

Fixes https://bugs.swift.org/browse/SR-6907 / rdar://problem/29066394.
2018-03-26 09:04:08 -07:00
Karoy Lorentey
08c3f8a610 [stdlib] Use _Hasher in conditional Hashable implementations
Implement _hash(into:) rather than hashValue.
2018-03-23 19:09:28 +00:00
Xiaodi Wu
fd0c387ea8 [stdlib] Mix lowerBound a little in Range.hashValue and ClosedRange.hashValue 2018-03-23 19:09:28 +00:00