Skip to content

Commit 25e71bd

Browse files
committed
Save 2 bytes in RawSyntaxData.ParsedToken
Introduction of `LexerError` incrased the `ParsedToken` size by 2 bytes compared to storing the kind and offset separately. `MemoryLayoutTest` didn’t fail because we only run them for debug builds but CI only creates release builds. Reclaim those two bytes by storing kind and offset as two fields in `RawSyntaxData.ParsedToken` again and transparently mapping them to a `LexerError` on access.
1 parent 8e6ae6e commit 25e71bd

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

Sources/SwiftSyntax/Raw/RawSyntax.swift

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,28 @@ internal struct RawSyntaxData {
5555

5656
var presence: SourcePresence
5757

58-
/// If the token has a lexial error, the type of the error.
59-
var lexerError: LexerError?
58+
/// Store the members of `LexerError` individually so the compiler can pack
59+
/// `ParsedToken` more efficiently (saving 2 bytes)
60+
/// `lexerErrorByteOffset` is ignored if `lexerErrorKind` is `nil`
61+
private var lexerErrorKind: LexerError.Kind?
62+
private var lexerErrorByteOffset: UInt16
63+
64+
var lexerError: LexerError? {
65+
if let kind = lexerErrorKind {
66+
return LexerError(kind, byteOffset: lexerErrorByteOffset)
67+
} else {
68+
return nil
69+
}
70+
}
71+
72+
init(tokenKind: RawTokenKind, wholeText: SyntaxText, textRange: Range<SyntaxText.Index>, presence: SourcePresence, lexerError: LexerError?) {
73+
self.tokenKind = tokenKind
74+
self.wholeText = wholeText
75+
self.textRange = textRange
76+
self.presence = presence
77+
self.lexerErrorKind = lexerError?.kind
78+
self.lexerErrorByteOffset = lexerError?.byteOffset ?? 0
79+
}
6080
}
6181

6282
/// Token typically created with `TokenSyntax.<someToken>`.

0 commit comments

Comments
 (0)