Skip to content

[5.3] Refactor Mirror to reduce metadata allocation #32957

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
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions stdlib/public/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ set(SWIFTLIB_ESSENTIAL
DictionaryVariant.swift
DropWhile.swift
Dump.swift
EitherSequence.swift
EmptyCollection.swift
Equatable.swift
ErrorType.swift
Expand Down
8 changes: 4 additions & 4 deletions stdlib/public/core/DebuggerSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public enum _DebuggerSupport {

private static func ivarCount(mirror: Mirror) -> Int {
let ivars = mirror.superclassMirror.map(ivarCount) ?? 0
return ivars + mirror.children.count
return ivars + mirror._children.count
}

private static func shouldExpand(
Expand All @@ -119,7 +119,7 @@ public enum _DebuggerSupport {
isRoot: Bool
) -> Bool {
if isRoot || collectionStatus.isCollection { return true }
if !mirror.children.isEmpty { return true }
if !mirror._children.isEmpty { return true }
if mirror.displayStyle == .`class` { return true }
if let sc = mirror.superclassMirror { return ivarCount(mirror: sc) > 0 }
return true
Expand Down Expand Up @@ -154,7 +154,7 @@ public enum _DebuggerSupport {
// anyway, so there's that...
let willExpand = mirror.displayStyle != .`class` || value is CustomReflectable?

let count = mirror.children.count
let count = mirror._children.count
let bullet = isRoot && (count == 0 || !willExpand) ? ""
: count == 0 ? "- "
: maxDepth <= 0 ? "▹ " : "▿ "
Expand Down Expand Up @@ -202,7 +202,7 @@ public enum _DebuggerSupport {
target: &target)
}

for (optionalName,child) in mirror.children {
for (optionalName,child) in mirror._children {
let childName = optionalName ?? "\(printedElements)"
if maxItemCounter <= 0 {
print(String(repeating: " ", count: indent+4), terminator: "", to: &target)
Expand Down
16 changes: 8 additions & 8 deletions stdlib/public/core/Dump.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ internal func _dump_unlocked<TargetStream: TextOutputStream>(
for _ in 0..<indent { target.write(" ") }

let mirror = Mirror(reflecting: value)
let count = mirror.children.count
let count = mirror._children.count
let bullet = count == 0 ? "-"
: maxDepth <= 0 ? "▹" : "▿"
target.write(bullet)
Expand Down Expand Up @@ -149,7 +149,7 @@ internal func _dump_unlocked<TargetStream: TextOutputStream>(
visitedItems: &visitedItems)
}

var currentIndex = mirror.children.startIndex
var currentIndex = mirror._children.startIndex
for i in 0..<count {
if maxItemCounter <= 0 {
for _ in 0..<(indent+4) {
Expand All @@ -167,8 +167,8 @@ internal func _dump_unlocked<TargetStream: TextOutputStream>(
return
}

let (name, child) = mirror.children[currentIndex]
mirror.children.formIndex(after: &currentIndex)
let (name, child) = mirror._children[currentIndex]
mirror._children.formIndex(after: &currentIndex)
_dump_unlocked(
child,
to: &target,
Expand Down Expand Up @@ -196,7 +196,7 @@ internal func _dumpSuperclass_unlocked<TargetStream: TextOutputStream>(

for _ in 0..<indent { target.write(" ") }

let count = mirror.children.count
let count = mirror._children.count
let bullet = count == 0 ? "-"
: maxDepth <= 0 ? "▹" : "▿"
target.write(bullet)
Expand All @@ -216,7 +216,7 @@ internal func _dumpSuperclass_unlocked<TargetStream: TextOutputStream>(
visitedItems: &visitedItems)
}

var currentIndex = mirror.children.startIndex
var currentIndex = mirror._children.startIndex
for i in 0..<count {
if maxItemCounter <= 0 {
for _ in 0..<(indent+4) {
Expand All @@ -234,8 +234,8 @@ internal func _dumpSuperclass_unlocked<TargetStream: TextOutputStream>(
return
}

let (name, child) = mirror.children[currentIndex]
mirror.children.formIndex(after: &currentIndex)
let (name, child) = mirror._children[currentIndex]
mirror._children.formIndex(after: &currentIndex)
_dump_unlocked(
child,
to: &target,
Expand Down
197 changes: 197 additions & 0 deletions stdlib/public/core/EitherSequence.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
////===--- _EitherSequence.swift - A sequence type-erasing two sequences -----===//
////
//// This source file is part of the Swift.org open source project
////
//// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
//// Licensed under Apache License v2.0 with Runtime Library Exception
////
//// See https://swift.org/LICENSE.txt for license information
//// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
////
////===----------------------------------------------------------------------===//

// Not public stdlib API, currently used in Mirror.children implementation.

internal enum _Either<Left, Right> {
case left(Left), right(Right)
}

extension _Either {
internal init(_ left: Left, or other: Right.Type) { self = .left(left) }
internal init(_ left: Left) { self = .left(left) }
internal init(_ right: Right) { self = .right(right) }
}

extension _Either: Equatable where Left: Equatable, Right: Equatable {
internal static func == (lhs: Self, rhs: Self) -> Bool {
switch (lhs, rhs) {
case let (.left(l), .left(r)): return l == r
case let (.right(l), .right(r)): return l == r
case (.left, .right), (.right, .left): return false
}
}
}

extension _Either: Comparable where Left: Comparable, Right: Comparable {
internal static func < (lhs: Self, rhs: Self) -> Bool {
switch (lhs, rhs) {
case let (.left(l), .left(r)): return l < r
case let (.right(l), .right(r)): return l < r
case (.left, .right): return true
case (.right, .left): return false
}
}
}

/// A sequence that type erases two sequences. A lighter-weight alternative to
/// AnySequence when more can be statically known, and which is more easily
/// specialized.
///
/// If you only know about one of the types, the second one can be
/// AnySequence, giving you a fast path for the known one.
///
/// If you have 3+ types to erase, you can nest them.
typealias _EitherSequence<L: Sequence, R: Sequence> =
_Either<L,R> where L.Element == R.Element

extension _EitherSequence {
internal struct Iterator {
var left: Left.Iterator?
var right: Right.Iterator?
}
}

extension _Either.Iterator: IteratorProtocol {
internal typealias Element = Left.Element

internal mutating func next() -> Element? {
left?.next() ?? right?.next()
}
}

extension _EitherSequence: Sequence {
internal typealias Element = Left.Element

internal func makeIterator() -> Iterator {
switch self {
case let .left(l):
return Iterator(left: l.makeIterator(), right: nil)
case let .right(r):
return Iterator(left: nil, right: r.makeIterator())
}
}
}

internal typealias _EitherCollection<
T: Collection, U: Collection
> = _EitherSequence<T,U> where T.Element == U.Element

extension _EitherCollection: Collection {
internal typealias Index = _Either<Left.Index, Right.Index>

internal var startIndex: Index {
switch self {
case let .left(s): return .left(s.startIndex)
case let .right(s): return .right(s.startIndex)
}
}

internal var endIndex: Index {
switch self {
case let .left(s): return .left(s.endIndex)
case let .right(s): return .right(s.endIndex)
}
}

internal subscript(position: Index) -> Element {
switch (self,position) {
case let (.left(s),.left(i)): return s[i]
case let (.right(s),.right(i)): return s[i]
default: fatalError("_EitherCollecton: Sequence used with other index type")
}
}

internal func index(after i: Index) -> Index {
switch (self,i) {
case let (.left(s),.left(i)): return .left(s.index(after: i))
case let (.right(s),.right(i)): return .right(s.index(after: i))
default: fatalError("_EitherCollecton: wrong type of index used")
}
}

internal func index(
_ i: Index,
offsetBy distance: Int,
limitedBy limit: Index
) -> Index? {
switch (self,i,limit) {
case let (.left(s),.left(i),.left(limit)):
return s.index(i, offsetBy: distance, limitedBy: limit).map { .left($0) }
case let (.right(s),.right(i),.right(limit)):
return s.index(i, offsetBy: distance, limitedBy: limit).map { .right($0) }
default: fatalError("_EitherCollecton: wrong type of index used")
}
}

internal func index(_ i: Index, offsetBy distance: Int) -> Index {
switch (self,i) {
case let (.left(s),.left(i)): return .left(s.index(i, offsetBy: distance))
case let (.right(s),.right(i)): return .right(s.index(i, offsetBy: distance))
default: fatalError("_EitherCollecton: wrong type of index used")
}
}

internal func distance(from start: Index, to end: Index) -> Int {
switch (self,start,end) {
case let (.left(s),.left(i),.left(j)):
return s.distance(from: i, to: j)
case let (.right(s),.right(i),.right(j)):
return s.distance(from: i, to: j)
default: fatalError("_EitherCollecton: wrong type of index used")
}
}
}

internal typealias _EitherBidirectionalCollection<
L: BidirectionalCollection, R: BidirectionalCollection
> = _Either<L,R> where L.Element == R.Element

extension _EitherBidirectionalCollection: BidirectionalCollection {
internal func index(before i: Index) -> Index {
switch (self,i) {
case let (.left(s),.left(i)): return .left(s.index(before: i))
case let (.right(s),.right(i)): return .right(s.index(before: i))
default: fatalError("_EitherCollecton: wrong type of index used")
}
}
}

internal typealias _EitherRandomAccessCollection<
L: RandomAccessCollection, R: RandomAccessCollection
> = _Either<L,R> where L.Element == R.Element

extension _EitherRandomAccessCollection: RandomAccessCollection { }

extension _Either {
init<T, C: Collection>(
_ collection: C
) where Right == AnyCollection<T>, C.Element == T {
self = .right(AnyCollection(collection))
}
}

extension AnyCollection {
init<L: Collection,R: Collection>(
_ other: _Either<L,R>
) where L.Element == Element, R.Element == Element {
// Strip away the Either and put the actual collection into the existential,
// trying to use the custom initializer from another AnyCollection.
switch other {
case let .left(l as Self): self = .init(l)
case let .right(r as Self): self = .init(r)
case let .left(l): self = .init(l)
case let .right(r): self = .init(r)
}
}
}

1 change: 1 addition & 0 deletions stdlib/public/core/GroupInfo.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"RandomAccessCollection.swift",
"MutableCollection.swift",
"CollectionAlgorithms.swift",
"EitherSequence.swift",
"EmptyCollection.swift",
"Stride.swift",
"Repeat.swift",
Expand Down
Loading