Skip to content

Commit 2cf6eb3

Browse files
committed
document: wrap parsed data in storage ref
Reduce retain traffic when copying TOMLDocument.
1 parent 1f57f34 commit 2cf6eb3

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

Sources/TOMLDecoder/Parsing/TOMLDocument.swift

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import Foundation
22

3-
struct TOMLDocument: Equatable, @unchecked Sendable {
3+
final class DocumentStorage: @unchecked Sendable, Equatable {
44
let tables: [InternalTOMLTable]
55
let arrays: [InternalTOMLArray]
66
let keyTables: [KeyTablePair]
77
let keyArrays: [KeyArrayPair]
88
let keyValues: [KeyValuePair]
9-
109
let source: String
1110

1211
init(
@@ -25,6 +24,44 @@ struct TOMLDocument: Equatable, @unchecked Sendable {
2524
self.keyValues = keyValues
2625
}
2726

27+
static func == (lhs: DocumentStorage, rhs: DocumentStorage) -> Bool {
28+
lhs.source == rhs.source &&
29+
lhs.tables == rhs.tables &&
30+
lhs.arrays == rhs.arrays &&
31+
lhs.keyTables == rhs.keyTables &&
32+
lhs.keyArrays == rhs.keyArrays &&
33+
lhs.keyValues == rhs.keyValues
34+
}
35+
}
36+
37+
struct TOMLDocument: Equatable, @unchecked Sendable {
38+
fileprivate let storage: DocumentStorage
39+
40+
var tables: [InternalTOMLTable] { storage.tables }
41+
var arrays: [InternalTOMLArray] { storage.arrays }
42+
var keyTables: [KeyTablePair] { storage.keyTables }
43+
var keyArrays: [KeyArrayPair] { storage.keyArrays }
44+
var keyValues: [KeyValuePair] { storage.keyValues }
45+
var source: String { storage.source }
46+
47+
init(
48+
source: String,
49+
tables: [InternalTOMLTable],
50+
arrays: [InternalTOMLArray],
51+
keyTables: [KeyTablePair],
52+
keyArrays: [KeyArrayPair],
53+
keyValues: [KeyValuePair]
54+
) {
55+
self.storage = DocumentStorage(
56+
source: source,
57+
tables: tables,
58+
arrays: arrays,
59+
keyTables: keyTables,
60+
keyArrays: keyArrays,
61+
keyValues: keyValues
62+
)
63+
}
64+
2865
init(source: String, keyTransform: (@Sendable (String) -> String)?) throws(TOMLError) {
2966
var hasContinousStorage = false
3067
var parser = Parser(keyTransform: keyTransform)

0 commit comments

Comments
 (0)