Skip to content

Commit

Permalink
Use memchr to optimize get_text_slice
Browse files Browse the repository at this point in the history
  • Loading branch information
clubby789 committed Feb 20, 2024
1 parent ee771ba commit 1717db1
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 44 deletions.
1 change: 1 addition & 0 deletions fluent-syntax/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ include = [
]

[dependencies]
memchr = "2.7.1"
serde = { workspace = true, optional = true, features = ["derive"] }
serde_json = { workspace = true, optional = true }
thiserror.workspace = true
Expand Down
6 changes: 6 additions & 0 deletions fluent-syntax/src/parser/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ macro_rules! get_current_byte {
$s.source.as_ref().as_bytes().get($s.ptr)
};
}

macro_rules! get_remaining_bytes {
($s:expr) => {
$s.source.as_ref().as_bytes().get($s.ptr..)
};
}
101 changes: 57 additions & 44 deletions fluent-syntax/src/parser/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,51 +157,64 @@ where
&mut self,
) -> Result<(usize, usize, TextElementType, TextElementTermination)> {
let start_pos = self.ptr;
let mut text_element_type = TextElementType::Blank;

while let Some(b) = get_current_byte!(self) {
match b {
b' ' => self.ptr += 1,
b'\n' => {
self.ptr += 1;
return Ok((
start_pos,
self.ptr,
text_element_type,
TextElementTermination::LineFeed,
));
}
b'\r' if self.is_byte_at(b'\n', self.ptr + 1) => {
self.ptr += 1;
return Ok((
start_pos,
self.ptr - 1,
text_element_type,
TextElementTermination::Crlf,
));
}
b'{' => {
return Ok((
start_pos,
self.ptr,
text_element_type,
TextElementTermination::PlaceableStart,
));
}
b'}' => {
return error!(ErrorKind::UnbalancedClosingBrace, self.ptr);
}
_ => {
text_element_type = TextElementType::NonBlank;
self.ptr += 1
}
let Some(rest) = get_remaining_bytes!(self) else {
return Ok((
start_pos,
self.ptr,
TextElementType::Blank,
TextElementTermination::Eof,
));
};
let end = memchr::memchr3(b'\n', b'{', b'}', rest);
let element_type = |text: &[u8]| {
if text.iter().any(|&c| c != b' ') {
TextElementType::NonBlank
} else {
TextElementType::Blank
}
};
match end.map(|p| &rest[..=p]) {
Some([text @ .., b'}']) => {
self.ptr += text.len();
error!(ErrorKind::UnbalancedClosingBrace, self.ptr)
}
Some([text @ .., b'\r', b'\n']) => {
self.ptr += text.len() + 1;
Ok((
start_pos,
self.ptr - 1,
element_type(text),
TextElementTermination::Crlf,
))
}
Some([text @ .., b'\n']) => {
self.ptr += text.len() + 1;
Ok((
start_pos,
self.ptr,
element_type(text),
TextElementTermination::LineFeed,
))
}
Some([text @ .., b'{']) => {
self.ptr += text.len();
Ok((
start_pos,
self.ptr,
element_type(text),
TextElementTermination::PlaceableStart,
))
}
None => {
self.ptr += rest.len();
Ok((
start_pos,
self.ptr,
element_type(rest),
TextElementTermination::Eof,
))
}
_ => unreachable!(),
}
Ok((
start_pos,
self.ptr,
text_element_type,
TextElementTermination::Eof,
))
}
}

0 comments on commit 1717db1

Please sign in to comment.