Skip to content

stdlib: extract the common code of _NativeDictionary.resize and copyAndResize into a separate function #29381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 24, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 28 additions & 24 deletions stdlib/public/core/NativeDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -187,45 +187,49 @@ extension _NativeDictionary { // Low-level lookup operations
}

extension _NativeDictionary { // ensureUnique
@inlinable
internal mutating func resize(capacity: Int) {
@_alwaysEmitIntoClient
@inline(never)
internal mutating func _copyOrMoveAndResize(
capacity: Int,
moveElements: Bool
) {
let capacity = Swift.max(capacity, self.capacity)
let newStorage = _DictionaryStorage<Key, Value>.resize(
original: _storage,
capacity: capacity,
move: true)
move: moveElements)
let result = _NativeDictionary(newStorage)
if count > 0 {
for bucket in hashTable {
let key = (_keys + bucket.offset).move()
let value = (_values + bucket.offset).move()
let key: Key
let value: Value
if moveElements {
key = (_keys + bucket.offset).move()
value = (_values + bucket.offset).move()
} else {
key = self.uncheckedKey(at: bucket)
value = self.uncheckedValue(at: bucket)
}
result._unsafeInsertNew(key: key, value: value)
}
// Clear out old storage, ensuring that its deinit won't overrelease the
// elements we've just moved out.
_storage._hashTable.clear()
_storage._count = 0
if moveElements {
// Clear out old storage, ensuring that its deinit won't overrelease the
// elements we've just moved out.
_storage._hashTable.clear()
_storage._count = 0
}
}
_storage = result._storage
}

@inlinable
@_semantics("optimize.sil.specialize.generic.size.never")
internal mutating func resize(capacity: Int) {
_copyOrMoveAndResize(capacity: capacity, moveElements: true)
}

@inlinable
internal mutating func copyAndResize(capacity: Int) {
let capacity = Swift.max(capacity, self.capacity)
let newStorage = _DictionaryStorage<Key, Value>.resize(
original: _storage,
capacity: capacity,
move: false)
let result = _NativeDictionary(newStorage)
if count > 0 {
for bucket in hashTable {
result._unsafeInsertNew(
key: self.uncheckedKey(at: bucket),
value: self.uncheckedValue(at: bucket))
}
}
_storage = result._storage
_copyOrMoveAndResize(capacity: capacity, moveElements: false)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the @_semantics attribute here be applied to the new function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A good question. I don't think so, because it's an alwaysEmitIntoClient function. Maybe it's better to remove it at all.
I'll experiment a bit.

}

@inlinable
Expand Down