Commit Graph

53 Commits

Author SHA1 Message Date
eeckstein
7b12c2efbf Merge pull request #29353 from eeckstein/dict-code-size
stdlib: move Dictionary's find functions into __RawDictionaryStorage.
2020-01-24 22:37:14 +01:00
Erik Eckstein
78728d8df5 stdlib: extract the common code of _NativeDictionary.resize and copyAndResize into a separate function
For code size reduction.
2020-01-24 10:40:41 +01:00
Erik Eckstein
ef007519b1 stdlib: move Dictionary's find functions into __RawDictionaryStorage.
The find functions do not require the generic Value parameter. Moving them to __RawDictionaryStorage allows to define them with only one generic parameter: the Key.
This allows the optimizer to share specializations for dictionaries which have the same Key, but a different Value.
Also, prevent inlining of the find-functions to save some additional code size.
2020-01-22 15:29:32 +01:00
Karoy Lorentey
d7e57347ee [stdlib] Unify _NativeDictionary.subscript._modify’s two yields to work around SR-10604 2019-08-07 17:15:04 -07: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
Karoy Lorentey
1ddf99a9da [stdlib] Set, Dictionary: Unify casting operations 2019-04-02 13:10:55 -07:00
Karoy Lorentey
2bd5da539e [stdlib] Fix Set/Dictionary casting issues
- Fix Set/Dictionary up/downcasting with String keys.
- Improve error handling.
2019-03-29 19:15:09 -07:00
David Smith
bf32e78737 Make bridged empty NSDictionary and NSSet instances bridge to the empty singletons 2019-02-11 15:54:19 -08:00
David Smith
2a9aa16b3f Make empty dictionary literals use the singleton 2019-02-11 15:38:22 -08: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
Karoy Lorentey
9d61bad1e5 [stdlib] Keep the first duplicate key instead of the last 2018-12-18 09:49:58 -08:00
Karoy Lorentey
f1f5e0f2bf [stdlib] Dictionary: Add unsafe bulk-loading initializer
Interface inspired by this Array pitch:
https://forums.swift.org/t/array-initializer-with-access-to-uninitialized-buffer/13689
2018-12-18 09:49:58 -08:00
Erik Eckstein
aae60ff271 stdlib: Code size improvements for Dictionary for -Osize
The first change is to remove some @inline(__always) attributes. Those were added before we had the guaranteed-by-default calling convention. They are not necessary anymore.

The second change is to not specialize some slow-path functions. This results that no specialization code for these functions is generated at the client side. Instead those functions are directly called in the libSwiftCore.
Note that Key-related hash and equality comparisons are still specialized, because otherwise the performance hit for Osize would be too big.

Some Dictionary benchmarks regress a bit with -Osize, but the code size wins are big.

rdar://problem/46534453
2018-12-13 16:59:19 -08: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
Karoy Lorentey
d32fd283b0 [stdlib] Refactor Dictionary.subscript._modify for better layering
This may also reduce ARC traffic by a little bit.
2018-11-30 17:47:08 +00:00
Ben Cohen
1673c12d78 [stdlib] Replace "sanityCheck" with "internalInvariant" (#20616)
* Replace "sanityCheck" with "internalInvariant"
2018-11-15 20:50:22 -08:00
Maxim Moiseev
cbf83ac04f [NFC][stdlib] Add FIXME markers to simplify audit 2018-11-14 11:58:42 -08:00
Slava Pestov
f6c2caf64b stdlib: Add @inlinable to @inline(__always) declarations
These should be audited since some might not actually need to be
@inlinable, but for now:

- Anything public and @inline(__always) is now also @inlinable
- Anything @usableFromInline and @inline(__always) is now @inlinable
2018-11-13 15:15:07 -05:00
Karoy Lorentey
bb2cb064ae [stdlib] Eliminate more overflow checks
This is a tiny code size improvement.
2018-10-05 12:33:25 +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
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
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