Skip to content

Commit

Permalink
Add slice optimized readers
Browse files Browse the repository at this point in the history
  • Loading branch information
nickbabcock committed Dec 19, 2023
1 parent cb9b769 commit 9ff9fae
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/binary/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::{
lexer::{read_id, read_string, read_token},
LexError, LexemeId, LexerError, Token,
};
use crate::buffer::{BufferError, BufferWindow, BufferWindowBuilder};
use crate::buffer::{BufferError, BufferWindow, BufferWindowBuilder, SliceReader};
use std::{fmt, io::Read};

/// [Lexer](crate::binary::Lexer) that works over a [Read] implementation
Expand Down Expand Up @@ -56,6 +56,15 @@ where
TokenReader::builder().build(reader)
}

/// Read from a byte slice without memcpy's
#[inline]
pub fn from_slice(data: &[u8]) -> TokenReader<SliceReader<'_>> {
TokenReader {
reader: SliceReader::new(data),
buf: BufferWindow::from_slice(data),
}
}

/// Returns the byte position of the data stream that has been processed.
///
/// ```rust
Expand Down
27 changes: 26 additions & 1 deletion src/buffer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::Scalar;
use std::{io::Read, ops::Range};
use std::{io::Read, marker::PhantomData, ops::Range};

#[derive(Debug)]
pub struct BufferWindow {
Expand All @@ -21,6 +21,16 @@ pub enum BufferError {
}

impl BufferWindow {
#[inline]
pub fn from_slice(data: &[u8]) -> Self {
Self {
buf: Box::new([]),
start: data.as_ptr(),
end: data.as_ptr_range().end,
prior_reads: 0,
}
}

#[inline]
pub fn advance_to(&mut self, ptr: *const u8) {
debug_assert!((self.start..=self.end).contains(&ptr));
Expand Down Expand Up @@ -139,3 +149,18 @@ impl BufferWindowBuilder {
}
}
}

#[derive(Debug)]
pub struct SliceReader<'a>(PhantomData<&'a [u8]>);

impl<'a> SliceReader<'a> {
pub fn new(_data: &'a [u8]) -> Self {
SliceReader(PhantomData)
}
}

impl<'a> Read for SliceReader<'a> {
fn read(&mut self, _buf: &mut [u8]) -> std::io::Result<usize> {
Ok(0)
}
}
12 changes: 11 additions & 1 deletion src/text/reader.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::Operator;
use crate::{
buffer::{BufferError, BufferWindow, BufferWindowBuilder},
buffer::{BufferError, BufferWindow, BufferWindowBuilder, SliceReader},
data::is_boundary,
util::{contains_zero_byte, count_chunk, repeat_byte},
Scalar,
Expand Down Expand Up @@ -109,6 +109,16 @@ where
TokenReader::builder().build(reader)
}

/// Read from a byte slice without memcpy's
#[inline]
pub fn from_slice(data: &[u8]) -> TokenReader<SliceReader<'_>> {
TokenReader {
reader: SliceReader::new(data),
buf: BufferWindow::from_slice(data),
utf8: Utf8Bom::Unknown,
}
}

/// Returns the byte position of the data stream that has been processed.
///
/// ```rust
Expand Down

0 comments on commit 9ff9fae

Please sign in to comment.