Commit Graph

32 Commits

Author SHA1 Message Date
Karoy Lorentey
a2e21667eb Merge pull request #19694 from lorentey/values-isnt-really-a-mutablecollection
[stdlib] Allow native dictionaries to advance Cocoa indices
2018-10-04 11:45:22 +01:00
Karoy Lorentey
6e671b6631 [stdlib] Allow native dictionaries to advance Cocoa indices 2018-10-03 21:05:46 +01:00
Karoy Lorentey
6cb546534e [stdlib] Dictionary: Force-inline native iterators 2018-10-03 13:23:47 +01:00
Karoy Lorentey
d6e31a80f1 [stdlib] Mark _NativeDictionary._delete(at:) non-releasing
This eliminates a retain/release pair around calls to it when it doesn’t get inlined.

_delete is responsible for restoring hash table invariants after a removal. It moves around elements in complicated patterns, but it doesn’t release them.
2018-10-02 15:38:39 +01:00
Karoy Lorentey
2b7ef24990 [stdlib] Dictionary.merge: Don’t leave storage in an inconsistent state when closure throws 2018-10-01 15:40:14 +01:00
Karoy Lorentey
ee4ad7a014 Merge pull request #19589 from lorentey/prepare-for-per-instance-seeds
[stdlib] Set, Dictionary: Prepare for per-instance hash seeds
2018-09-28 11:38:02 +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
e080bab5e5 Merge pull request #19602 from lorentey/dict-iterators
[stdlib] Dictionary.Keys, .Values: Implement custom iterators
2018-09-28 04:18:15 +01:00
Karoy Lorentey
b92c301e99 [stdlib] Set, Dictionary: Clean up storage allocation 2018-09-28 04:04:29 +01:00
Karoy Lorentey
7826c3c01b [stdlib] Dictionary.mapValues: Copy the seed over to the new dictionary 2018-09-28 04:04:29 +01:00
Karoy Lorentey
91c5a310fa [stdlib] Set, Dictionary: Prepare for per-instance hash seeds
Implement the per-instance seeding infrastructure without actually enabling per-instance seeding.
2018-09-28 04:04:28 +01:00
Karoy Lorentey
e5d711c5a1 Merge pull request #19576 from lorentey/aged-indices
[stdlib] Set, Dictionary: Radically improved index validation
2018-09-28 04:00:38 +01:00
Karoy Lorentey
a05a66cdce [stdlib] Dictionary.Keys, .Values: Implement custom iterators 2018-09-28 02:10:17 +01:00
Karoy Lorentey
7ff82b3ba9 [stdlib] Dictionary.updateValue(_:,forKey:): Don’t overwrite the existing key
Replacing the old key with the new is unnecessary and somewhat surprising. It is also harmful to some usecases.

rdar://problem/32144087

# Conflicts:
#	stdlib/public/core/NativeDictionary.swift
2018-09-27 21:20:54 +01:00
Karoy Lorentey
9a0e303ac6 [stdlib] Dictionary: Allow limited use of Cocoa indices in native dictionaries
Affected operations:

subscript(index:)
keys.subscript(index:)
values.subscript(index:) (setter and _modify)
remove(at:)
swapAt(_:, _:)

Note that index(after:) is intentionally not on this list.
2018-09-27 19:11:50 +01:00
Karoy Lorentey
d3178a0394 [stdlib] Tighten up Cocoa index validation for native sets/dictionaries
Like before, allow the use of Cocoa indices to access native sets/dictionaries, but approximate the same mutation count-based check as we do for native indices.

- Ensure that native collections that were converted from Cocoa have an age generated from the address of the original Cocoa object.
- To get the age of a Cocoa index, regenerate one from the object embedded in it.
- Compare self.age to index.age and trap if it doesn’t match.

# Conflicts:
#	stdlib/public/core/HashTable.swift
2018-09-27 19:11:19 +01:00
Karoy Lorentey
125f31ed52 [stdlib] validate(_:) ⟹ validatedBucket(for:)
This makes it a little easier to follow validation logic.
2018-09-27 19:09:49 +01:00
Karoy Lorentey
656685777f [stdlib] Dictionary: Update remove(at:) and Values to allow Cocoa indices 2018-09-27 19:09:43 +01:00
Karoy Lorentey
4aec137789 [stdlib] Set, Dictionary: Use lowercase index in validation error messages
Capital Index seems too nitpicky, somehow
2018-09-27 19:09:33 +01:00
Karoy Lorentey
04d5ab3498 [stdlib] Native Set, Dictionary: Add index validation support for top-level indices
Native sets and (especially!) native dictionaries must support indexing with Cocoa indices — indices must be preserved when a Cocoa set/dictionary is converted to native.

This is especially the case for Dictionaries that were converted because of a mutation restricted to values — such as those done through the Values view.
2018-09-27 19:09:26 +01:00
Karoy Lorentey
6033454201 [stdlib] Dictionary.Values: Specialize swapAt 2018-09-27 01:49:51 +01:00
Karoy Lorentey
720f3a0a04 [stdlib] Set, Dictionary: Validate index received on native index(after:) path 2018-09-27 01:49:51 +01:00
Karoy Lorentey
e5d976de14 [stdlib] Dictionary: Embed & check the mutation count in native indices 2018-09-27 01:49:51 +01:00
Karoy Lorentey
6358294be8 [stdlib] Set, Dictionary: Add a mutation count to storage ivars
The mutation count will allow us to recognize and trap on invalid indices more reliably. (However, it won’t be foolproof — some invalid indices may pass through the checks.)

- Change _scale to Int8 to make space for an extra Int32 without enlarging the storage header.
- Add an _age field inside the new gap.
- Initialize the age to a scrambled version of the storage address.
- Generate a new counter for every newly allocated storage instance, except for identical copy-on-write copies.
- Increment the mutation counter after every removal.
2018-09-27 01:49:50 +01:00
Karoy Lorentey
7cd482ed98 [stdlib] Set, Dictionary: _uncheckedInitialize should take __owned arguments 2018-09-27 00:12:35 +01:00
Karoy Lorentey
afe55f5f26 [stdlib] _NativeDictionary.init(capacity:) make inlinable 2018-09-27 00:12:35 +01:00
Karoy Lorentey
efe6f38d40 [stdlib] _NativeDictionary.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
Karoy Lorentey
b4e27b110a Revert "[stdlib] Dictionary.updateValue(_:,forKey:): Don’t overwrite the existing key" 2018-09-26 16:08:07 +01:00
Karoy Lorentey
1ab367b262 Merge pull request #19500 from lorentey/updateValue-but-not-the-key
[stdlib] Dictionary.updateValue(_:,forKey:): Don’t overwrite the existing key
2018-09-26 14:32:04 +01:00
Karoy Lorentey
a293ce15a6 [stdlib] Dictionary.updateValue(_:,forKey:): Don’t overwrite the existing key
Replacing the old key with the new is unnecessary and somewhat surprising. It is also harmful to some usecases.

rdar://problem/32144087
2018-09-24 15:44:43 +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
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