Skip to content

Commit 3a03b85

Browse files
committed
docs(parser): improve docs for Token::read_bool (#13873)
Follow-on after #13867. Correct the assembly examples. AI copied its invented assembly from #13788. Replace it with the *actual* assembly! Also move the `# SAFETY` comment higher up, as that's more important than the perf analysis.
1 parent 8221a01 commit 3a03b85

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

crates/oxc_parser/src/lexer/token.rs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -193,35 +193,38 @@ impl Token {
193193

194194
/// Read `bool` from 8 bits starting at bit position `shift`.
195195
///
196+
/// # SAFETY
197+
///
198+
/// `shift` must be the location of a valid boolean "field" in [`Token`]
199+
/// e.g. `ESCAPED_SHIFT`. The caller must guarantee that the 8 bits at
200+
/// `shift` contain only 0 or 1, making it safe to read as a `bool`.
201+
///
202+
/// # Performance analysis
203+
///
196204
/// This method uses unsafe pointer arithmetic to directly read a boolean value
197205
/// from the token's 128-bit representation. This approach is deliberately chosen
198206
/// for performance optimization on hot paths.
199207
///
200-
/// # Performance Analysis
201-
///
202-
/// This unsafe pointer arithmetic approach generates only 3 CPU instructions:
208+
/// This unsafe pointer arithmetic approach generates only 1 CPU instruction:
203209
/// ```asm
204-
/// movzx eax, BYTE PTR [rdi+9] ; Load byte at offset
205-
/// and eax, 1 ; Mask to get boolean
206-
/// ret ; Return
210+
/// movzx eax, byte ptr [rdi + 9] ; Load byte at offset
211+
/// ```
212+
///
213+
/// Compared to the safe bit-shift alternative:
214+
/// ```ignore
215+
/// (token.0 >> shift) & 1 != 0
207216
/// ```
208217
///
209-
/// Compared to the safe bit-shift alternative which generates 4 instructions:
210218
/// ```asm
211-
/// mov rax, QWORD PTR [rdi+8] ; Load 64-bit value
212-
/// shr rax, 8 ; Shift to extract bit
213-
/// and eax, 1 ; Mask to get boolean
214-
/// ret ; Return
219+
/// movzx eax, byte ptr [rdi + 9] ; Load byte at offset
220+
/// and al, 1 ; Mask to lower bit only
215221
/// ```
216222
///
223+
/// <https://godbolt.org/z/7xxrP348P>
224+
///
217225
/// This optimization was retained after careful benchmarking (see PR #13788),
218226
/// where the single instruction difference on hot paths justified keeping
219227
/// the unsafe implementation.
220-
///
221-
/// # SAFETY
222-
/// `shift` must be the location of a valid boolean "field" in [`Token`]
223-
/// e.g. `ESCAPED_SHIFT`. The caller must guarantee that the 8 bits at
224-
/// `shift` contain only 0 or 1, making it safe to read as a `bool`.
225228
#[expect(clippy::inline_always)]
226229
#[inline(always)] // So `shift` is statically known
227230
unsafe fn read_bool(&self, shift: usize) -> bool {

0 commit comments

Comments
 (0)