Skip to content

Commit 1e8cd99

Browse files
Revert lazy children performance improvement (#35393)
1 parent fb69048 commit 1e8cd99

File tree

7 files changed

+91
-103
lines changed

7 files changed

+91
-103
lines changed

stdlib/public/core/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ set(SWIFTLIB_ESSENTIAL
6060
DictionaryVariant.swift
6161
DropWhile.swift
6262
Dump.swift
63-
EitherSequence.swift
6463
EmptyCollection.swift
6564
Equatable.swift
6665
ErrorType.swift

stdlib/public/core/DebuggerSupport.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public enum _DebuggerSupport {
110110

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

116116
private static func shouldExpand(
@@ -119,7 +119,7 @@ public enum _DebuggerSupport {
119119
isRoot: Bool
120120
) -> Bool {
121121
if isRoot || collectionStatus.isCollection { return true }
122-
if !mirror._children.isEmpty { return true }
122+
if !mirror.children.isEmpty { return true }
123123
if mirror.displayStyle == .`class` { return true }
124124
if let sc = mirror.superclassMirror { return ivarCount(mirror: sc) > 0 }
125125
return true
@@ -161,7 +161,7 @@ public enum _DebuggerSupport {
161161
}
162162
let willExpand = isNonClass || isCustomReflectable
163163

164-
let count = mirror._children.count
164+
let count = mirror.children.count
165165
let bullet = isRoot && (count == 0 || !willExpand) ? ""
166166
: count == 0 ? "- "
167167
: maxDepth <= 0 ? "" : ""
@@ -209,7 +209,7 @@ public enum _DebuggerSupport {
209209
target: &target)
210210
}
211211

212-
for (optionalName,child) in mirror._children {
212+
for (optionalName,child) in mirror.children {
213213
let childName = optionalName ?? "\(printedElements)"
214214
if maxItemCounter <= 0 {
215215
print(String(repeating: " ", count: indent+4), terminator: "", to: &target)

stdlib/public/core/Dump.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ internal func _dump_unlocked<TargetStream: TextOutputStream>(
9999
for _ in 0..<indent { target.write(" ") }
100100

101101
let mirror = Mirror(reflecting: value)
102-
let count = mirror._children.count
102+
let count = mirror.children.count
103103
let bullet = count == 0 ? "-"
104104
: maxDepth <= 0 ? "" : ""
105105
target.write(bullet)
@@ -149,7 +149,7 @@ internal func _dump_unlocked<TargetStream: TextOutputStream>(
149149
visitedItems: &visitedItems)
150150
}
151151

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

170-
let (name, child) = mirror._children[currentIndex]
171-
mirror._children.formIndex(after: &currentIndex)
170+
let (name, child) = mirror.children[currentIndex]
171+
mirror.children.formIndex(after: &currentIndex)
172172
_dump_unlocked(
173173
child,
174174
to: &target,
@@ -196,7 +196,7 @@ internal func _dumpSuperclass_unlocked<TargetStream: TextOutputStream>(
196196

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

199-
let count = mirror._children.count
199+
let count = mirror.children.count
200200
let bullet = count == 0 ? "-"
201201
: maxDepth <= 0 ? "" : ""
202202
target.write(bullet)
@@ -216,7 +216,7 @@ internal func _dumpSuperclass_unlocked<TargetStream: TextOutputStream>(
216216
visitedItems: &visitedItems)
217217
}
218218

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

237-
let (name, child) = mirror._children[currentIndex]
238-
mirror._children.formIndex(after: &currentIndex)
237+
let (name, child) = mirror.children[currentIndex]
238+
mirror.children.formIndex(after: &currentIndex)
239239
_dump_unlocked(
240240
child,
241241
to: &target,

stdlib/public/core/GroupInfo.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@
6464
"RandomAccessCollection.swift",
6565
"MutableCollection.swift",
6666
"CollectionAlgorithms.swift",
67-
"EitherSequence.swift",
6867
"EmptyCollection.swift",
6968
"Stride.swift",
7069
"Repeat.swift",

stdlib/public/core/Mirror.swift

Lines changed: 62 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,25 @@ public struct Mirror {
100100
/// `superclassMirror` property is `nil`.
101101
case suppressed
102102
}
103-
104103

105-
/// The static type of the subject being reflected.
104+
/// Creates a mirror that reflects on the given instance.
106105
///
107-
/// This type may differ from the subject's dynamic type when this mirror
108-
/// is the `superclassMirror` of another mirror.
109-
public let subjectType: Any.Type
106+
/// If the dynamic type of `subject` conforms to `CustomReflectable`, the
107+
/// resulting mirror is determined by its `customMirror` property.
108+
/// Otherwise, the result is generated by the language.
109+
///
110+
/// If the dynamic type of `subject` has value semantics, subsequent
111+
/// mutations of `subject` will not observable in `Mirror`. In general,
112+
/// though, the observability of mutations is unspecified.
113+
///
114+
/// - Parameter subject: The instance for which to create a mirror.
115+
public init(reflecting subject: Any) {
116+
if case let customized as CustomReflectable = subject {
117+
self = customized.customMirror
118+
} else {
119+
self = Mirror(internalReflecting: subject)
120+
}
121+
}
110122

111123
/// An element of the reflected instance's structure.
112124
///
@@ -130,26 +142,6 @@ public struct Mirror {
130142
/// }
131143
public typealias Children = AnyCollection<Child>
132144

133-
internal typealias _Children
134-
= _EitherCollection<ReflectedChildren,AnyCollection<Child>>
135-
136-
internal var _children: _Children
137-
138-
/// A collection of `Child` elements describing the structure of the
139-
/// reflected subject.
140-
public var children: Children { AnyCollection(_children) }
141-
142-
/// A suggested display style for the reflected subject.
143-
public let displayStyle: DisplayStyle?
144-
145-
/// A mirror of the subject's superclass, if one exists.
146-
public var superclassMirror: Mirror? {
147-
return _makeSuperclassMirror()
148-
}
149-
150-
internal let _makeSuperclassMirror: () -> Mirror?
151-
internal let _defaultDescendantRepresentation: _DefaultDescendantRepresentation
152-
153145
/// A suggestion of how a mirror's subject is to be interpreted.
154146
///
155147
/// Playgrounds and the debugger will show a representation similar
@@ -160,25 +152,6 @@ public struct Mirror {
160152
case dictionary, `set`
161153
}
162154

163-
/// Creates a mirror that reflects on the given instance.
164-
///
165-
/// If the dynamic type of `subject` conforms to `CustomReflectable`, the
166-
/// resulting mirror is determined by its `customMirror` property.
167-
/// Otherwise, the result is generated by the language.
168-
///
169-
/// If the dynamic type of `subject` has value semantics, subsequent
170-
/// mutations of `subject` will not observable in `Mirror`. In general,
171-
/// though, the observability of mutations is unspecified.
172-
///
173-
/// - Parameter subject: The instance for which to create a mirror.
174-
public init(reflecting subject: Any) {
175-
if case let customized as CustomReflectable = subject {
176-
self = customized.customMirror
177-
} else {
178-
self = Mirror(internalReflecting: subject)
179-
}
180-
}
181-
182155
internal static func _noSuperclassMirror() -> Mirror? { return nil }
183156

184157
@_semantics("optimize.sil.specialize.generic.never")
@@ -250,7 +223,7 @@ public struct Mirror {
250223
self._makeSuperclassMirror = Mirror._superclassIterator(
251224
subject, ancestorRepresentation)
252225

253-
self._children = _Children(children)
226+
self.children = Children(children)
254227
self.displayStyle = displayStyle
255228
self._defaultDescendantRepresentation
256229
= subject is CustomLeafReflectable ? .suppressed : .generated
@@ -295,7 +268,7 @@ public struct Mirror {
295268

296269
let lazyChildren =
297270
unlabeledChildren.lazy.map { Child(label: nil, value: $0) }
298-
self._children = _Children(lazyChildren)
271+
self.children = Children(lazyChildren)
299272

300273
self.displayStyle = displayStyle
301274
self._defaultDescendantRepresentation
@@ -342,12 +315,33 @@ public struct Mirror {
342315
subject, ancestorRepresentation)
343316

344317
let lazyChildren = children.lazy.map { Child(label: $0.0, value: $0.1) }
345-
self._children = _Children(lazyChildren)
318+
self.children = Children(lazyChildren)
346319

347320
self.displayStyle = displayStyle
348321
self._defaultDescendantRepresentation
349322
= subject is CustomLeafReflectable ? .suppressed : .generated
350323
}
324+
325+
/// The static type of the subject being reflected.
326+
///
327+
/// This type may differ from the subject's dynamic type when this mirror
328+
/// is the `superclassMirror` of another mirror.
329+
public let subjectType: Any.Type
330+
331+
/// A collection of `Child` elements describing the structure of the
332+
/// reflected subject.
333+
public let children: Children
334+
335+
/// A suggested display style for the reflected subject.
336+
public let displayStyle: DisplayStyle?
337+
338+
/// A mirror of the subject's superclass, if one exists.
339+
public var superclassMirror: Mirror? {
340+
return _makeSuperclassMirror()
341+
}
342+
343+
internal let _makeSuperclassMirror: () -> Mirror?
344+
internal let _defaultDescendantRepresentation: _DefaultDescendantRepresentation
351345
}
352346

353347
/// A type that explicitly supplies its own mirror.
@@ -384,6 +378,14 @@ extension Int: MirrorPath {}
384378
extension String: MirrorPath {}
385379

386380
extension Mirror {
381+
internal struct _Dummy: CustomReflectable {
382+
internal init(mirror: Mirror) {
383+
self.mirror = mirror
384+
}
385+
internal var mirror: Mirror
386+
internal var customMirror: Mirror { return mirror }
387+
}
388+
387389
/// Returns a specific descendant of the reflected subject, or `nil` if no
388390
/// such descendant exists.
389391
///
@@ -431,28 +433,24 @@ extension Mirror {
431433
public func descendant(
432434
_ first: MirrorPath, _ rest: MirrorPath...
433435
) -> Any? {
434-
435-
func fetch(_ path: MirrorPath, in children: _Children) -> Any? {
436-
let position: _Children.Index?
437-
switch path {
438-
case let label as String:
439-
position = children.firstIndex { $0.label == label }
440-
case let offset as Int:
436+
var result: Any = _Dummy(mirror: self)
437+
for e in [first] + rest {
438+
let children = Mirror(reflecting: result).children
439+
let position: Children.Index
440+
if case let label as String = e {
441+
position = children.firstIndex { $0.label == label } ?? children.endIndex
442+
}
443+
else if let offset = e as? Int {
441444
position = children.index(children.startIndex,
442445
offsetBy: offset,
443-
limitedBy: children.endIndex)
444-
default:
446+
limitedBy: children.endIndex) ?? children.endIndex
447+
}
448+
else {
445449
_preconditionFailure(
446450
"Someone added a conformance to MirrorPath; that privilege is reserved to the standard library")
447451
}
448-
return position.map { children[$0].value }
449-
}
450-
451-
guard var result = fetch(first, in: _children) else { return nil }
452-
for path in rest {
453-
guard let next = fetch(path, in: Mirror(reflecting: result)._children)
454-
else { return nil }
455-
result = next
452+
if position == children.endIndex { return nil }
453+
result = children[position].value
456454
}
457455
return result
458456
}

stdlib/public/core/OutputStream.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -290,15 +290,15 @@ internal func _adHocPrint_unlocked<T, TargetStream: TextOutputStream>(
290290
if let displayStyle = mirror.displayStyle {
291291
switch displayStyle {
292292
case .optional:
293-
if let child = mirror._children.first {
293+
if let child = mirror.children.first {
294294
_debugPrint_unlocked(child.1, &target)
295295
} else {
296296
_debugPrint_unlocked("nil", &target)
297297
}
298298
case .tuple:
299299
target.write("(")
300300
var first = true
301-
for (label, value) in mirror._children {
301+
for (label, value) in mirror.children {
302302
if first {
303303
first = false
304304
} else {
@@ -319,7 +319,7 @@ internal func _adHocPrint_unlocked<T, TargetStream: TextOutputStream>(
319319
printTypeName(mirror.subjectType)
320320
target.write("(")
321321
var first = true
322-
for (label, value) in mirror._children {
322+
for (label, value) in mirror.children {
323323
if let label = label {
324324
if first {
325325
first = false
@@ -345,7 +345,7 @@ internal func _adHocPrint_unlocked<T, TargetStream: TextOutputStream>(
345345
// If the case name is garbage, just print the type name.
346346
printTypeName(mirror.subjectType)
347347
}
348-
if let (_, value) = mirror._children.first {
348+
if let (_, value) = mirror.children.first {
349349
if Mirror(reflecting: value).displayStyle == .tuple {
350350
_debugPrint_unlocked(value, &target)
351351
} else {
@@ -450,19 +450,19 @@ internal func _dumpPrint_unlocked<T, TargetStream: TextOutputStream>(
450450
// count
451451
switch displayStyle {
452452
case .tuple:
453-
let count = mirror._children.count
453+
let count = mirror.children.count
454454
target.write(count == 1 ? "(1 element)" : "(\(count) elements)")
455455
return
456456
case .collection:
457-
let count = mirror._children.count
457+
let count = mirror.children.count
458458
target.write(count == 1 ? "1 element" : "\(count) elements")
459459
return
460460
case .dictionary:
461-
let count = mirror._children.count
461+
let count = mirror.children.count
462462
target.write(count == 1 ? "1 key/value pair" : "\(count) key/value pairs")
463463
return
464464
case .`set`:
465-
let count = mirror._children.count
465+
let count = mirror.children.count
466466
target.write(count == 1 ? "1 member" : "\(count) members")
467467
return
468468
default:

stdlib/public/core/ReflectionMirror.swift

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -128,25 +128,17 @@ internal func _getClassPlaygroundQuickLook(
128128
#endif
129129

130130
extension Mirror {
131-
internal struct ReflectedChildren: RandomAccessCollection {
132-
let subject: Any
133-
let subjectType: Any.Type
134-
var startIndex: Int { 0 }
135-
var endIndex: Int { _getChildCount(subject, type: subjectType) }
136-
subscript(index: Int) -> Child {
137-
getChild(of: subject, type: subjectType, index: index)
138-
}
139-
}
140-
141-
internal init(
142-
internalReflecting subject: Any,
143-
subjectType: Any.Type? = nil,
144-
customAncestor: Mirror? = nil
145-
) {
131+
internal init(internalReflecting subject: Any,
132+
subjectType: Any.Type? = nil,
133+
customAncestor: Mirror? = nil)
134+
{
146135
let subjectType = subjectType ?? _getNormalizedType(subject, type: type(of: subject))
147136

148-
self._children = _Children(
149-
ReflectedChildren(subject: subject, subjectType: subjectType))
137+
let childCount = _getChildCount(subject, type: subjectType)
138+
let children = (0 ..< childCount).lazy.map({
139+
getChild(of: subject, type: subjectType, index: $0)
140+
})
141+
self.children = Children(children)
150142

151143
self._makeSuperclassMirror = {
152144
guard let subjectClass = subjectType as? AnyClass,

0 commit comments

Comments
 (0)