Skip to content

Commit

Permalink
perf: avoid using alt when parsing Program
Browse files Browse the repository at this point in the history
Signed-off-by: ljedrz <[email protected]>
  • Loading branch information
ljedrz committed May 31, 2024
1 parent 140ff26 commit c9a7676
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 18 deletions.
2 changes: 2 additions & 0 deletions console/network/environment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,10 @@ pub mod prelude {
bytes::{complete::tag, streaming::take},
character::complete::{alpha1, alphanumeric1, char, one_of},
combinator::{complete, fail, map, map_res, opt, recognize},
error::{make_error, ErrorKind},
multi::{count, many0, many0_count, many1, separated_list0, separated_list1},
sequence::{pair, terminated},
Err,
};
pub use num_traits::{AsPrimitive, One, Pow, Zero};
pub use rand::{
Expand Down
2 changes: 0 additions & 2 deletions console/program/src/data_types/record_type/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ impl<N: Network> Parser for RecordType<N> {
Ok((string, (identifier, value_type)))
}

// Parse the whitespace and comments from the string.
let (string, _) = Sanitizer::parse(string)?;
// Parse the type name from the string.
let (string, _) = tag(Self::type_name())(string)?;
// Parse the whitespace from the string.
Expand Down
2 changes: 0 additions & 2 deletions console/program/src/data_types/struct_type/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ impl<N: Network> Parser for StructType<N> {
Ok((string, (identifier, plaintext_type)))
}

// Parse the whitespace and comments from the string.
let (string, _) = Sanitizer::parse(string)?;
// Parse the type name from the string.
let (string, _) = tag(Self::type_name())(string)?;
// Parse the whitespace from the string.
Expand Down
2 changes: 0 additions & 2 deletions synthesizer/program/src/closure/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ impl<N: Network, Instruction: InstructionTrait<N>> Parser for ClosureCore<N, Ins
/// Parses a string into a closure.
#[inline]
fn parse(string: &str) -> ParserResult<Self> {
// Parse the whitespace and comments from the string.
let (string, _) = Sanitizer::parse(string)?;
// Parse the 'closure' keyword from the string.
let (string, _) = tag(Self::type_name())(string)?;
// Parse the whitespace from the string.
Expand Down
2 changes: 0 additions & 2 deletions synthesizer/program/src/function/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ impl<N: Network, Instruction: InstructionTrait<N>, Command: CommandTrait<N>> Par
/// Parses a string into a function.
#[inline]
fn parse(string: &str) -> ParserResult<Self> {
// Parse the whitespace and comments from the string.
let (string, _) = Sanitizer::parse(string)?;
// Parse the 'function' keyword from the string.
let (string, _) = tag(Self::type_name())(string)?;
// Parse the whitespace from the string.
Expand Down
4 changes: 3 additions & 1 deletion synthesizer/program/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ mod serialize;

use console::{
network::prelude::{
alt,
anyhow,
bail,
de,
ensure,
error,
fmt,
make_error,
many0,
many1,
map,
Expand All @@ -65,7 +65,9 @@ use console::{
Deserialize,
Deserializer,
Display,
Err,
Error,
ErrorKind,
Formatter,
FromBytes,
FromBytesDeserializer,
Expand Down
2 changes: 0 additions & 2 deletions synthesizer/program/src/mapping/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ impl<N: Network> Parser for Mapping<N> {
/// Parses a string into a mapping.
#[inline]
fn parse(string: &str) -> ParserResult<Self> {
// Parse the whitespace and comments from the string.
let (string, _) = Sanitizer::parse(string)?;
// Parse the 'mapping' keyword from the string.
let (string, _) = tag(Self::type_name())(string)?;
// Parse the whitespace from the string.
Expand Down
29 changes: 22 additions & 7 deletions synthesizer/program/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,29 @@ impl<N: Network, Instruction: InstructionTrait<N>, Command: CommandTrait<N>> Par
// Parse the semicolon ';' keyword from the string.
let (string, _) = tag(";")(string)?;

fn intermediate<N: Network, Instruction: InstructionTrait<N>, Command: CommandTrait<N>>(
string: &str,
) -> ParserResult<P<N, Instruction, Command>> {
// Parse the whitespace and comments from the string.
let (string, _) = Sanitizer::parse(string)?;

if string.starts_with(Mapping::<N>::type_name()) {
map(Mapping::parse, |mapping| P::<N, Instruction, Command>::M(mapping))(string)
} else if string.starts_with(StructType::<N>::type_name()) {
map(StructType::parse, |struct_| P::<N, Instruction, Command>::I(struct_))(string)
} else if string.starts_with(RecordType::<N>::type_name()) {
map(RecordType::parse, |record| P::<N, Instruction, Command>::R(record))(string)
} else if string.starts_with(ClosureCore::<N, Instruction>::type_name()) {
map(ClosureCore::parse, |closure| P::<N, Instruction, Command>::C(closure))(string)
} else if string.starts_with(FunctionCore::<N, Instruction, Command>::type_name()) {
map(FunctionCore::parse, |function| P::<N, Instruction, Command>::F(function))(string)
} else {
Err(Err::Error(make_error(string, ErrorKind::Alt)))
}
}

// Parse the struct or function from the string.
let (string, components) = many1(alt((
map(Mapping::parse, |mapping| P::<N, Instruction, Command>::M(mapping)),
map(StructType::parse, |struct_| P::<N, Instruction, Command>::I(struct_)),
map(RecordType::parse, |record| P::<N, Instruction, Command>::R(record)),
map(ClosureCore::parse, |closure| P::<N, Instruction, Command>::C(closure)),
map(FunctionCore::parse, |function| P::<N, Instruction, Command>::F(function)),
)))(string)?;
let (string, components) = many1(intermediate)(string)?;
// Parse the whitespace and comments from the string.
let (string, _) = Sanitizer::parse(string)?;

Expand Down

0 comments on commit c9a7676

Please sign in to comment.