Skip to content

Commit 39f3156

Browse files
committed
Make underlying data structures of SyntaxData.Info classes insted of structs
This will allow us to add more data to `SyntaxData.Info.Root` without increasing the size of `SyntaxData`. It shouldn’t have a big performance impact at the moment since `Root.arena` is never accessed and only exists to keep `arena` a live.
1 parent fe8e6bd commit 39f3156

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

Sources/SwiftSyntax/SyntaxData.swift

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,34 +182,57 @@ struct AbsoluteRawSyntax {
182182
}
183183
}
184184

185+
class Wrapper<T> {
186+
var value: T
187+
188+
init(_ value: T) {
189+
self.value = value
190+
}
191+
192+
var pointer: UnsafeMutablePointer<T> {
193+
return withUnsafeMutablePointer(to: &value) { $0 }
194+
}
195+
}
196+
185197
/// SyntaxData is the underlying storage for each Syntax node.
186198
///
187199
/// SyntaxData is an implementation detail, and should not be exposed to clients
188200
/// of SwiftSyntax.
189201
struct SyntaxData {
190202
fileprivate enum Info {
191-
case root(Root)
203+
case root(Wrapper<Root>)
192204
indirect case nonRoot(NonRoot)
193205

194206
// For root node.
195207
struct Root {
196208
var arena: SyntaxArena
209+
210+
init(arena: SyntaxArena) {
211+
self.arena = arena
212+
}
197213
}
198214

199215
// For non-root nodes.
200216
struct NonRoot {
201217
var parent: SyntaxData
202218
var absoluteInfo: AbsoluteSyntaxInfo
219+
var rootInfo: UnsafeMutablePointer<Root>
220+
221+
init(parent: SyntaxData, absoluteInfo: AbsoluteSyntaxInfo, rootInfo: UnsafeMutablePointer<Root>) {
222+
self.parent = parent
223+
self.absoluteInfo = absoluteInfo
224+
self.rootInfo = rootInfo
225+
}
203226
}
204227
}
205228

206229
private let info: Info
207230
let raw: RawSyntax
208231

209-
private var rootInfo: Info.Root {
232+
private var rootInfo: UnsafeMutablePointer<Info.Root> {
210233
switch info {
211-
case .root(let info): return info
212-
case .nonRoot(let info): return info.parent.rootInfo
234+
case .root(let info): return info.pointer
235+
case .nonRoot(let info): return info.rootInfo
213236
}
214237
}
215238

@@ -274,7 +297,7 @@ struct SyntaxData {
274297
}
275298

276299
init(_ raw: RawSyntax, parent: SyntaxData, absoluteInfo: AbsoluteSyntaxInfo) {
277-
self.init(raw, info: .nonRoot(.init(parent: parent, absoluteInfo: absoluteInfo)))
300+
self.init(raw, info: .nonRoot(.init(parent: parent, absoluteInfo: absoluteInfo, rootInfo: parent.rootInfo)))
278301
}
279302

280303
/// Creates a `SyntaxData` with the provided raw syntax and parent.
@@ -294,7 +317,7 @@ struct SyntaxData {
294317
/// has a chance to retain it.
295318
static func forRoot(_ raw: RawSyntax, rawNodeArena: SyntaxArena) -> SyntaxData {
296319
precondition(rawNodeArena === raw.arena)
297-
return SyntaxData(raw, info: .root(.init(arena: rawNodeArena)))
320+
return SyntaxData(raw, info: .root(Wrapper(Info.Root(arena: rawNodeArena))))
298321
}
299322

300323
/// Returns the child data at the provided index in this data's layout.

0 commit comments

Comments
 (0)