Skip to content

Update GraphReuse #269

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
May 6, 2025
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
9 changes: 5 additions & 4 deletions Sources/OpenSwiftUICore/Graph/GraphInputs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,14 @@ public struct _GraphInputs {

private mutating func recordReusableInput<T>(_ input: T.Type) where T: GraphInput, T.Value: GraphReusable {
let filter = BloomFilter(type: input)
let reusableInputs = customInputs[ReusableInputs.self]
if reusableInputs.stack.top == T.self {
let inputs = customInputs[ReusableInputs.self]
let stack = inputs.stack
guard stack.top != T.self else {
return
}
customInputs[ReusableInputs.self] = ReusableInputStorage(
filter: reusableInputs.filter.union(filter),
stack: .node(value: T.self, next: reusableInputs.stack)
filter: inputs.filter.union(filter),
stack: .node(value: T.self, next: stack)
)
}

Expand Down
156 changes: 122 additions & 34 deletions Sources/OpenSwiftUICore/Graph/GraphReuse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,37 @@
// OpenSwiftUICore
//
// Audited for iOS 18.0
// Status: Blocked by _GraphInputs
// Status: Complete
// ID: 3E2D3733C4CBF57EC1EA761D02CE8317 (SwiftUICore)

import Foundation
package import OpenGraphShims
#if OPENSWIFTUI_SWIFT_LOG
import Logging
#else
import os.log
#endif

// MARK: - IndirectAttributeMap

public final class IndirectAttributeMap {
package final class IndirectAttributeMap {
package final let subgraph: Subgraph

package final var map: [AnyAttribute: AnyAttribute]

package init(subgraph: Subgraph) {
self.subgraph = subgraph
self.map = [:]
}
}

// MARK: - GraphReusable

package protocol GraphReusable {
static var isTriviallyReusable: Bool { get }

mutating func makeReusable(indirectMap: IndirectAttributeMap)

func tryToReuse(by other: Self, indirectMap: IndirectAttributeMap, testOnly: Bool) -> Bool
}

Expand All @@ -28,6 +42,8 @@ extension GraphReusable {
package static var isTriviallyReusable: Bool { false }
}

// MARK: - _GraphValue + GraphReusable

extension _GraphValue: GraphReusable {
package mutating func makeReusable(indirectMap: IndirectAttributeMap) {
value.makeReusable(indirectMap: indirectMap)
Expand All @@ -41,65 +57,137 @@ extension _GraphValue where Value: GraphReusable {
package static var isTriviallyReusable: Bool { Value.isTriviallyReusable }
}

//extension _GraphInputs : GraphReusable {
// package mutating func makeReusable(indirectMap: IndirectAttributeMap)
// package func tryToReuse(by other: _GraphInputs, indirectMap: IndirectAttributeMap, testOnly: Bool) -> Bool
//}
// MARK: - _GraphInputs + GraphReusable [WIP]

extension _GraphInputs: GraphReusable {
package mutating func makeReusable(indirectMap: IndirectAttributeMap) {
time.makeReusable(indirectMap: indirectMap)
phase.makeReusable(indirectMap: indirectMap)
changedDebugProperties.insert(.phase)
environment.makeReusable(indirectMap: indirectMap)
detachEnvironmentInputs()
changedDebugProperties.insert(.environment)
transaction.makeReusable(indirectMap: indirectMap)
func project<Input>(_ type: Input.Type) where Input: GraphInput {
guard !Input.isTriviallyReusable else {
return
}
var value = self[Input.self]
Input.makeReusable(indirectMap: indirectMap, value: &value)
self[Input.self] = value
}
let stack = customInputs[ReusableInputs.self].stack
for value in stack {
project(value)
}
}

package func tryToReuse(by other: _GraphInputs, indirectMap: IndirectAttributeMap, testOnly: Bool) -> Bool {
guard time.tryToReuse(by: other.time, indirectMap: indirectMap, testOnly: testOnly),
phase.tryToReuse(by: other.phase, indirectMap: indirectMap, testOnly: testOnly),
environment.tryToReuse(by: other.environment, indirectMap: indirectMap, testOnly: testOnly),
transaction.tryToReuse(by: other.transaction, indirectMap: indirectMap, testOnly: testOnly)
else {
Log.graphReuse("Reuse failed: standard inputs")
return false
}
return reuseCustomInputs(by: other, indirectMap: indirectMap, testOnly: testOnly)
}

private func reuseCustomInputs(by other: _GraphInputs, indirectMap: IndirectAttributeMap, testOnly: Bool) -> Bool {
let reusableInputs = customInputs[ReusableInputs.self]
let otherReusableInputs = other.customInputs[ReusableInputs.self]
guard reusableInputs.filter == otherReusableInputs.filter else {
return false
}
var reusableInputsArray: [ObjectIdentifier] = []
for value in reusableInputs.stack {
reusableInputsArray.append(ObjectIdentifier(value))
}
var otherReusableInputsArray: [ObjectIdentifier] = []
for value in otherReusableInputs.stack {
otherReusableInputsArray.append(ObjectIdentifier(value))
}
guard reusableInputsArray == otherReusableInputsArray else {
Log.graphReuse("Reuse failed: custom inputs type mismatch")
return false
}
var ignoredTypes = reusableInputsArray + [ObjectIdentifier(ReusableInputs.self)]
guard !customInputs.mayNotBeEqual(to: other.customInputs, ignoredTypes: &ignoredTypes) else {
Log.graphReuse("Reuse failed: custom inputs plist equality")
return false
}
func project<Input>(_ type: Input.Type) -> Bool where Input: GraphInput {
guard let index = ignoredTypes.firstIndex(of: ObjectIdentifier(type)) else {
return true
}
let lastIndex = ignoredTypes.count - 1
ignoredTypes.swapAt(index, lastIndex)
guard !Input.isTriviallyReusable else {
return true
}
guard Input.tryToReuse(self[type], by: other[type], indirectMap: indirectMap, testOnly: testOnly) else {
Log.graphReuse("Reuse failed: custom input \(Input.self)")
return false
}
return true
}
let stack = reusableInputs.stack
for value in stack {
guard project(value) else {
return false
}
continue
}
return true
}
}

//extension _GraphInputs {
// private func reuseCustomInputs(by other: _GraphInputs, indirectMap: IndirectAttributeMap, testOnly: Bool) -> Bool {
// Log.graphReuse("Reuse failed: custom input \(Self.self)")
// }
//}
// MARK: - Attribute + GraphReusable

extension Attribute: GraphReusable {
package mutating func makeReusable(indirectMap: IndirectAttributeMap) {
let indirect: AnyAttribute
if let result = indirectMap.map[identifier] {
identifier = result
indirect = result
} else {
let indirect = indirectMap.subgraph.apply {
IndirectAttribute(source: self)
indirect = indirectMap.subgraph.apply {
IndirectAttribute(source: self).identifier
}
indirectMap.map[identifier] = indirect.identifier
indirectMap.map[identifier] = indirect
}
identifier = indirect
}

package func tryToReuse(by other: Attribute<Value>, indirectMap: IndirectAttributeMap, testOnly: Bool) -> Bool {
if let result = indirectMap.map[identifier] {
if testOnly {
return true
} else {
result.source = other.identifier
return true
}
} else {
guard let result = indirectMap.map[identifier] else {
Log.graphReuse("Reuse failed: missing indirection for \(Value.self)")
return false
}
if !testOnly {
result.source = other.identifier
}
return true
}
}

import Foundation
#if canImport(Darwin)
import os.log
#endif

private struct EnableGraphReuseLogging: UserDefaultKeyedFeature {
static var key: String { "org.OpenSwiftUIProject.OpenSwiftUI.GraphReuseLogging" }

static var cachedValue: Bool?
}

extension Log {
#if canImport(Darwin)
private static let graphReuseLog: Logger = Logger(subsystem: Log.subsystem, category: "GraphReuse")
#endif


static func graphReuse(_ message: @autoclosure () -> String) {
#if canImport(Darwin)
if EnableGraphReuseLogging.isEnabled {
let message = message()
#if OPENSWIFTUI_SWIFT_LOG
graphReuseLog.log(level: .info, "\(message)")
#else
graphReuseLog.log("\(message)")
#endif
}
#endif
}
}
5 changes: 5 additions & 0 deletions Sources/OpenSwiftUICore/Tracing/ReuseTrace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ package struct ReuseTrace {
traceReuseFailure("reuse_bodyMismatched")
}

@inline(__always)
package static func traceNeverMadeReusableFailure(_ valueType: (any Any.Type)?) {
// TODO
}

// TODO

final package class Recorder {
Expand Down
10 changes: 5 additions & 5 deletions Sources/OpenSwiftUICore/View/Input/ViewList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1051,12 +1051,12 @@ extension ViewList.ID.Views: Sendable {}

// MARK: - UnaryViewGenerator

public protocol UnaryViewGenerator {
private protocol UnaryViewGenerator {
func makeView(inputs: _ViewInputs, indirectMap: IndirectAttributeMap?) -> _ViewOutputs
func tryToReuse(by other: Self, indirectMap: IndirectAttributeMap, testOnly: Bool) -> Bool
}

public struct BodyUnaryViewGenerator<V>: UnaryViewGenerator {
private struct BodyUnaryViewGenerator<V>: UnaryViewGenerator {
let body: ViewList.Elements.MakeElement

public func makeView(inputs: _ViewInputs, indirectMap: IndirectAttributeMap?) -> _ViewOutputs {
Expand All @@ -1073,10 +1073,10 @@ public struct BodyUnaryViewGenerator<V>: UnaryViewGenerator {
}
}

public struct TypedUnaryViewGenerator<V>: UnaryViewGenerator where V: View {
private struct TypedUnaryViewGenerator<V>: UnaryViewGenerator where V: View {
let view: WeakAttribute<V>

public func makeView(inputs: _ViewInputs, indirectMap: IndirectAttributeMap?) -> _ViewOutputs {
func makeView(inputs: _ViewInputs, indirectMap: IndirectAttributeMap?) -> _ViewOutputs {
guard var view = view.attribute else {
return .init()
}
Expand All @@ -1086,7 +1086,7 @@ public struct TypedUnaryViewGenerator<V>: UnaryViewGenerator where V: View {
return V.makeDebuggableView(view: _GraphValue(view), inputs: inputs)
}

public func tryToReuse(by other: TypedUnaryViewGenerator<V>, indirectMap: IndirectAttributeMap, testOnly: Bool) -> Bool {
func tryToReuse(by other: TypedUnaryViewGenerator<V>, indirectMap: IndirectAttributeMap, testOnly: Bool) -> Bool {
guard let view = view.attribute, let otherView = other.view.attribute else {
Log.graphReuse("Reuse failed: missing attribute for \(V.self)")
return false
Expand Down
3 changes: 1 addition & 2 deletions Tests/OpenSwiftUICoreTests/Graph/GraphHostTests.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//
// GraphHostTests.swift
// OpenSwiftUITests
// OpenSwiftUICoreTests

import OpenSwiftUICore
@_spi(ForOpenSwiftUIOnly) import OpenSwiftUICore
import Testing

Expand Down
Loading