Commit Graph

121 Commits

Author SHA1 Message Date
Doug Gregor
353daabf8d Replace UnsafeSendable with @unchecked Sendable in the standard library. 2021-11-12 07:56:10 -08:00
Karoy Lorentey
af06df90b6 [stdlib] Optimize Set.subtracting even more 2021-11-02 20:16:35 -07:00
Karoy Lorentey
2e3e88cab8 [stdlib] Optimize Set.intersection(_:)
Use a temporary bitset to speed up the `Sequence` variant by roughly a factor of ~4-6, and the set/set variant by a factor of ~1-4, depending on the ratio of overlapping elements.
2021-11-01 21:38:10 -07:00
Karoy Lorentey
db80c78809 [stdlib] Optimize Set.filter(_:)
This works the same way as `Set.subtracting<S>(_:)`, and has similar performance benefits.
2021-11-01 21:38:10 -07:00
Karoy Lorentey
80296bb606 [stdlib] Optimize Set.subtracting(_:)
Use a temporary bitset to avoid hashing elements more than once, and to prevent rehashings during the creation of the result set.

This leads to a speedup of about 0-4x, depending on the number of elements removed.
2021-11-01 21:38:10 -07:00
Karoy Lorentey
8612f2f960 [stdlib] Add a fast path to Set.isDisjoint<S>(with: S)
Have the generic variant call out to the specialized overload if the argument happens to be a `Set`.
2021-11-01 21:38:10 -07:00
Karoy Lorentey
a2540c27b7 [stdlib] Optimize Set.isStrictSuperset(of:)
- Use a temporary bitset to speed up the `Sequence` variant by roughly a factor of 4.
- Fix a logic error causing the `a == b` case for the set variant to be O(n) instead of O(1).
2021-11-01 21:38:10 -07:00
Karoy Lorentey
725ee55fbc [stdlib] Optimize Set.isSuperset<S>(of: S)
Call into the specialized overload if the argument happens to be a `Set`.
2021-11-01 21:38:10 -07:00
Karoy Lorentey
9043c71bd7 [stdlib] Optimize Set.isStrictSubset<S>(of: S)
Use a temporary bitset to speed up the Sequence variant by roughly a factor of 3.
2021-11-01 21:38:09 -07:00
Karoy Lorentey
5e642d2b8d [stdlib] Optimize Set.isSubset<S>(of: S)
Use a temporary bitset to speed up the Sequence variant by roughly a factor of 3.
2021-11-01 21:38:09 -07:00
LucianoAlmeida
00cb1c5330 [stdlib] Improve set isDisjoint by iterating on smaller set 2021-09-14 17:28:45 -03: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
Benzi
4e0c6f99b8 [stdlib] Set.intersection iterate over smaller set (#36678)
* [stdlib] Set.intersection iterate over smaller set

When intersecting two sets, it is beneficial to iterate over the smaller sized set of the two, and check membership on the other. This speeds up runtime dramatically for cases where the current set is significantly larger than the set you are intersecting against.

* Review comments - variable names, implicit swap
2021-04-11 11:42:10 -04: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
Valeriy Van
d57cbce3c0 Fixes example snippets in Set.swift 2020-05-15 06:55:07 +02: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
Mike Ash
fa5888fb3f [Stdlib][Overlays] Rename various classes to avoid conflicting ObjC names.
Old Swift and new Swift runtimes and overlays need to coexist in the same process. This means there must not be any classes which have the same ObjC runtime name in old and new, because the ObjC runtime doesn't like name collisions.

When possible without breaking source compatibility, classes were renamed in Swift, which results in a different ObjC name.

Public classes were renamed only on the ObjC side using the @_objcRuntimeName attribute.

This is similar to the work done in pull request #19295. That only renamed @objc classes. This renames all of the others, since even pure Swift classes still get an ObjC name.

rdar://problem/46646438
2019-01-15 12:21:20 -05:00
Luciano Almeida
2bc5623bdf [stdlib] Resolving some FIXME comments on Set type. (#20631)
* Fixing some fixmes on stdlib Set

* Adding @inline attr

* Fixing spaces

* Adding isEmpty as fast path in other places where is possible.

* Quotes on variable name on comment.

* Update stdlib/public/core/Set.swift

Co-Authored-By: LucianoPAlmeida <passos.luciano@outlook.com>

* Adding benchmark for isDisjoint Set method

* Adding empty sets to benchmark

* Fixing the factor on benchmarks and naming warnings for empty 5 words
2018-12-14 22:11:12 -06:00
Karoy Lorentey
ce96f1e528 [stdlib] _modify: Use defer to ensure invariants are restored when yield throws
_modify mustn’t leave Collection storage in an inconsistent state when the code to which we’re yielding happens to throw. In practice this means that any cleanup code must happen in defer blocks before the yield.

Fix Dictionary to do just that and add tests to cover this functionality (as well as some other aspects of _modify).
2018-12-05 17:35:24 +00:00
Ben Cohen
1673c12d78 [stdlib] Replace "sanityCheck" with "internalInvariant" (#20616)
* Replace "sanityCheck" with "internalInvariant"
2018-11-15 20:50:22 -08:00
Karoy Lorentey
4f8eeae64e [stdlib] Make _NSSetCore, _NSSet internal
These shadow protocols don’t add any real type safety, and exposing them in the ABI seems unnecessary.
2018-11-15 09:54:25 +00:00
Ben Cohen
dbc2e21522 Remove customization points from Sequence and Collection (#19995) 2018-10-31 18:58:00 -07:00
Karoy Lorentey
803a43c537 [stdlib] Set, Dictionary: Change second word in Cocoa indices to an integer offset
_BridgeObject turned out not to be a great choice, because it doesn’t support tagged values on 32-bit platforms. Extracting the offset from index storage is a good idea anyway!
2018-10-27 01:11:59 +01:00
Karoy Lorentey
697ad69d24 [stdlib] Set, Dictionary: Reorganize equality checks to fix Linux builds 2018-10-05 12:33:25 +01:00
Karoy Lorentey
64d19f44db [stdlib] Set, Dictionary: Switch to using a _BridgeObject instead of an enum 2018-10-05 12:33:25 +01:00
Karoy Lorentey
e24f2ba6e5 [stdlib] Set, Dictionary: Flatten switch statements
The optimizer dislikes nested switch statements; flatten them out to simplify optimization and to hopefully speed things up a little.
2018-10-05 12:33:25 +01:00
Karoy Lorentey
2f0f43a869 [stdlib] Dictionary, Set: Force-inline hidden Sequence/Collection customization points
This should eliminate a branch, which should probably lead to a tiny overall code size improvement, as well as a tiny performance boost.
2018-10-03 13:14:48 +01:00
Karoy Lorentey
1529d37301 [stdlib] Implement formIndex(after:) in all hashed collections 2018-09-29 02:30:08 +01:00
Karoy Lorentey
526162f706 [stdlib] Set, Dictionary: Fix uniqueness checking for Cocoa indices 2018-09-29 02:29:32 +01:00
Karoy Lorentey
41bedbe42b [stdlib] Set, Dictionary: Remove inappropriate @_effects from Index hashing 2018-09-28 20:15:55 +01:00
Karoy Lorentey
eb69682f77 [stdlib] Turn _CocoaSet.Index into a COW value type with opaque storage
This enables making the struct @_fixed_layout, which should win us back some of the performance lost by hiding Cocoa indexing internals.
2018-09-28 17:57:20 +01:00
Karoy Lorentey
d477420ee4 [stdlib] Set, Dictionary: Make Cocoa indices resilient for now 2018-09-28 15:18:15 +01:00
Karoy Lorentey
7a8b34a499 [stdlib] Set, Dictionary: Make reserveCapacity non-inlinable
This allows us to record the reserved capacity in storage later, which will enable removals to resize the hash table.
2018-09-28 04:59:33 +01:00
Karoy Lorentey
46475d03d0 [stdlib] Supplying a native index to a bridged set/dictionary is not an internal error 2018-09-27 19:11:57 +01:00
Karoy Lorentey
e687464154 [stdlib] Set: Allow limited use of Cocoa indices in native sets
Affected operations:

subscript(index:)
remove(at:)

Note that index(after:) is intentionally not on this list.
2018-09-27 19:11:43 +01:00
Karoy Lorentey
c765b59b2a [stdlib] Set: Embed & check the mutation count in native indices 2018-09-27 01:49:51 +01:00
Karoy Lorentey
231fbc6218 [stdlib] Fix platforms with a non-ObjC runtime 2018-09-27 00:31:02 +01:00
Karoy Lorentey
e78635119a [stdlib] _NativeSet.Index: Rename to “Bucket”
Update code to reflect the new name, including in method and variable names.
2018-09-27 00:12:20 +01:00
swift-ci
aca00505d9 Merge pull request #19496 from lorentey/hashing-consuming-audit 2018-09-25 11:27:59 -07:00
Ben Cohen
df995de0d6 [stdlib] Make KeyValuePairs fully inlinable (#19502)
* Move KVP into its own Swift file. Make it fully inlineable.

* Make _makeCollectionDescription an extension. Add KeyValue equivalent.
2018-09-24 17:46:32 -07:00
Karoy Lorentey
547cfa44ca [stdlib] Mark Set.isSubset<S>(of:) and similar queries as having a borrowed argument
This matches SetAlgebra, and seems to make the most sense, intuitively.
2018-09-24 16:51:34 +01:00
Karoy Lorentey
f59fd89e3c [stdlib] SetAlgebra: __consuming/__owned audit
Subtraction and intersection don’t need to consume their input parameter — the result value will only include elements from self.
2018-09-24 14:31:35 +01:00
Karoy Lorentey
c3bc84e680 [stdlib] Set, Dictionary: __consuming/__owned audit
Add __consuming and __owned to Set and Dictionary members where applicable.

Ignore compiler intrinsics for casting for now — their ARC behavior is covered by unit tests that need to be updated.
2018-09-24 13:46:40 +01:00
Karoy Lorentey
6d81663716 [stdlib] Make Hashable._rawHashValue(seed:) safer and more convenient
- Don’t expose the raw execution seed to _rawHashValue.
- Change the type of _rawHashValue’s seed from (UInt64,UInt64) to a single Int. Working with a pair of UInt64s is unwieldy, and overkill in practice. Int as a seed also integrates nicely with Int as a hash value.
- Remove _HasherCore._generateSeed(). Instead, simply call finalize() on a copy of the hasher to get a seed suitable for _rawHashValue.
- Update Set and Dictionary to store a single Int as the seed value.

Note that this doesn’t affect the core hasher, which still mixes in the actual 128-bit execution seed during its initialization. To reduce the potential of confusion, use the name “rawSeed” to refer to an actual 128-bit seed value.
2018-09-24 13:30:05 +01:00
Karoy Lorentey
e9186000d0 [stdlib][NFC] Break Set/Dictionary implementation into smaller files
This has long been overdue; splitting the Dictionary.swift/Set.swift monoliths makes for a far less confusing editing experience.
2018-09-23 01:38:37 +01:00
Karoy Lorentey
ff882f4a06 [stdlib] Set, Dictionary: Sanitize @objc-ness for NSEnumerator members 2018-09-22 13:36:05 +01:00
Karoy Lorentey
430a9f3064 [stdlib] _BridgingHashBuffer: Fix memory management
- Have the hash buffer include a reference to the original hash storage instance, along with a copy of its _HashTable, so that its lifetime can be independent from the deferred bridging object.
- Convert _BridgingHashBuffer to a ManagedBuffer so that we can easily put reference-counted properties in its header.
2018-09-22 13:36:05 +01:00
Karoy Lorentey
b2d4e801a4 [stdlib] Discourage CoW copying / rehashing from getting inlined 2018-09-22 02:04:11 +01:00
Karoy Lorentey
4e64931b77 [stdlib] Set.Iterator.next(): Work around compiler crash in non-ObjC builds 2018-09-22 02:04:10 +01:00