-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: Let the lexer operate on
u8
instead of char
to improve read…
… performance. (#23)
- Loading branch information
1 parent
dcc68f3
commit 9d058af
Showing
8 changed files
with
50 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
use crate::error_handling::Result; | ||
|
||
pub trait LexerStream { | ||
fn get_next_char(&mut self) -> Result<Option<char>>; | ||
fn get_next_char(&mut self) -> Result<Option<u8>>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,47 @@ | ||
use super::lexer_stream::LexerStream; | ||
use crate::error_handling::Error::IOError; | ||
use crate::error_handling::Result; | ||
use std::io::BufRead; | ||
use std::io::{self, BufReader, Read}; | ||
|
||
const BUF_SIZE: usize = 4096 * 8; | ||
|
||
pub struct BufferedFileStream { | ||
line_it: std::io::Lines<std::io::BufReader<std::fs::File>>, | ||
line: Option<Vec<char>>, | ||
buf_reader: BufReader<std::fs::File>, | ||
pos: usize, | ||
end: usize, | ||
buffer: [u8; BUF_SIZE], | ||
} | ||
|
||
impl BufferedFileStream { | ||
pub fn new(path: &str) -> Result<Self> { | ||
match std::fs::File::open(path) { | ||
Ok(file) => Ok(Self { | ||
line_it: std::io::BufReader::new(file).lines(), | ||
line: None, | ||
buf_reader: BufReader::new(file), | ||
pos: 0, | ||
end: 0, | ||
buffer: [0; BUF_SIZE], | ||
}), | ||
Err(e) => Err(IOError(e)), | ||
} | ||
} | ||
} | ||
|
||
impl LexerStream for BufferedFileStream { | ||
fn get_next_char(&mut self) -> Result<Option<char>> { | ||
if self.line.is_none() { | ||
let next_line = self.line_it.next(); | ||
if next_line.is_none() { | ||
return Ok(None); | ||
} | ||
match next_line.unwrap() { | ||
Ok(line) => { | ||
self.line = Some(line.chars().collect()); | ||
self.line.as_mut().unwrap().push('\n'); | ||
fn get_next_char(&mut self) -> Result<Option<u8>> { | ||
if self.pos == self.end { | ||
match self.buf_reader.read(&mut self.buffer) { | ||
Ok(byte_read) => { | ||
if 0 == byte_read { | ||
return Ok(None); | ||
} | ||
self.end = byte_read; | ||
self.pos = 0; | ||
} | ||
Err(e) => return Err(IOError(e)), | ||
} | ||
} | ||
|
||
let c = self.line.as_ref().unwrap()[self.pos]; | ||
let c = self.buffer[self.pos]; | ||
self.pos += 1; | ||
if self.pos == self.line.as_ref().unwrap().len() { | ||
self.line = None; | ||
} | ||
Ok(Some(c)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters