Skip to content

Commit

Permalink
Simplify Source, read_unchecked is only ever used on single byte
Browse files Browse the repository at this point in the history
  • Loading branch information
kmicklas committed Mar 23, 2024
1 parent 0fd7e8a commit d2a0ebf
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 42 deletions.
15 changes: 3 additions & 12 deletions logos-codegen/src/generator/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,12 @@ impl Context {
self.available.saturating_sub(self.at)
}

pub fn read_unchecked(&mut self, len: usize) -> TokenStream {
pub fn read_byte_unchecked(&mut self) -> TokenStream {
let at = self.at;

match len {
0 => {
self.advance(1);
self.advance(1);

quote!(lex.read_unchecked::<u8>(#at))
}
l => {
self.advance(l);

quote!(lex.read_unchecked::<&[u8; #l]>(#at))
}
}
quote!(lex.read_byte_unchecked(#at))
}

pub fn read(&mut self, len: usize) -> TokenStream {
Expand Down
2 changes: 1 addition & 1 deletion logos-codegen/src/generator/fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ impl<'a> Generator<'a> {
let min_read = self.meta[this].min_read;

if ctx.remainder() >= max(min_read, 1) {
let read = ctx.read_unchecked(0);
let read = ctx.read_byte_unchecked();

return (quote!(byte), quote!(let byte = unsafe { #read };));
}
Expand Down
4 changes: 2 additions & 2 deletions src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ pub trait LexerInternal<'source> {
/// Read a chunk at current position, offset by `n`.
fn read_at<T: Chunk<'source>>(&self, n: usize) -> Option<T>;

/// Unchecked read a chunk at current position, offset by `n`.
unsafe fn read_unchecked<T: Chunk<'source>>(&self, n: usize) -> T;
/// Unchecked read a byte at current position, offset by `n`.
unsafe fn read_byte_unchecked(&self, n: usize) -> u8;

/// Test a chunk at current position with a closure.
fn test<T: Chunk<'source>, F: FnOnce(T) -> bool>(&self, test: F) -> bool;
Expand Down
7 changes: 2 additions & 5 deletions src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,11 +302,8 @@ where
}

#[inline]
unsafe fn read_unchecked<Chunk>(&self, n: usize) -> Chunk
where
Chunk: source::Chunk<'source>,
{
self.source.read_unchecked(self.token_end + n)
unsafe fn read_byte_unchecked(&self, n: usize) -> u8 {
self.source.read_byte_unchecked(self.token_end + n)
}

/// Test a chunk at current position with a closure.
Expand Down
23 changes: 6 additions & 17 deletions src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,12 @@ pub trait Source {
where
Chunk: self::Chunk<'a>;

/// Read a chunk of bytes into an array without doing bounds checks.
/// Read a byte without doing bounds checks.
///
/// # Safety
///
/// Offset should not exceed bounds.
unsafe fn read_unchecked<'a, Chunk>(&'a self, offset: usize) -> Chunk
where
Chunk: self::Chunk<'a>;
unsafe fn read_byte_unchecked(&self, offset: usize) -> u8;

/// Get a slice of the source at given range. This is analogous to
/// `slice::get(range)`.
Expand Down Expand Up @@ -119,10 +117,7 @@ impl Source for str {
}

#[inline]
unsafe fn read_unchecked<'a, Chunk>(&'a self, offset: usize) -> Chunk
where
Chunk: self::Chunk<'a>,
{
unsafe fn read_byte_unchecked(&self, offset: usize) -> u8 {
Chunk::from_ptr(self.as_ptr().add(offset))
}

Expand Down Expand Up @@ -179,10 +174,7 @@ impl Source for [u8] {
}

#[inline]
unsafe fn read_unchecked<'a, Chunk>(&'a self, offset: usize) -> Chunk
where
Chunk: self::Chunk<'a>,
{
unsafe fn read_byte_unchecked(&self, offset: usize) -> u8 {
Chunk::from_ptr(self.as_ptr().add(offset))
}

Expand Down Expand Up @@ -232,11 +224,8 @@ where
self.deref().read(offset)
}

unsafe fn read_unchecked<'a, Chunk>(&'a self, offset: usize) -> Chunk
where
Chunk: self::Chunk<'a>,
{
self.deref().read_unchecked(offset)
unsafe fn read_byte_unchecked(&self, offset: usize) -> u8 {
self.deref().read_byte_unchecked(offset)
}

fn slice(&self, range: Range<usize>) -> Option<Self::Slice<'_>> {
Expand Down
7 changes: 2 additions & 5 deletions tests/tests/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@ impl<'s, S: ?Sized + Source> Source for RefSource<'s, S> {
self.0.read(offset)
}

unsafe fn read_unchecked<'a, Chunk>(&'a self, offset: usize) -> Chunk
where
Chunk: logos::source::Chunk<'a>,
{
self.0.read_unchecked(offset)
unsafe fn read_byte_unchecked(&self, offset: usize) -> u8 {
self.0.read_byte_unchecked(offset)
}

fn slice(&self, range: Range<usize>) -> Option<Self::Slice<'_>> {
Expand Down

0 comments on commit d2a0ebf

Please sign in to comment.