feat(parser): parallelize the initial parsing

This commit is contained in:
Orhun Parmaksız 2021-10-29 19:16:06 +03:00
parent 354bc5dd77
commit da927e010f
No known key found for this signature in database
GPG key ID: F83424824B3E4B90
4 changed files with 31 additions and 16 deletions

View file

@ -1,3 +1,7 @@
use crate::error::{Error, Result};
use rayon::prelude::*;
use std::path::Path;
use systeroid_parser::document::Document;
use systeroid_parser::parser::Parser;
use systeroid_parser::regex::RegexBuilder;
@ -20,3 +24,17 @@ lazy_static! {
},
];
}
/// Parses the kernel documentation using the defined parsers.
pub fn parse_kernel_docs(kernel_docs: &Path) -> Result<Vec<Document>> {
PARSERS
.par_iter()
.try_fold(Vec::new, |mut documents, parser| {
documents.extend(parser.parse(kernel_docs)?);
Ok::<Vec<Document>, Error>(documents)
})
.try_reduce(Vec::new, |mut v1, v2| {
v1.extend(v2);
Ok(v1)
})
}

View file

@ -1,4 +1,5 @@
use crate::error::Result;
use crate::parsers::parse_kernel_docs;
use rayon::prelude::*;
use std::fmt::{self, Display, Formatter};
use std::path::Path;
@ -112,10 +113,9 @@ impl Sysctl {
Ok(Self { parameters })
}
/// Updates the description of the kernel parameters based on the [`parsed document`].
///
/// [`parsed document`]: Document
pub fn update_docs(&mut self, documents: Vec<Document>) {
/// Updates the descriptions of the kernel parameters.
pub fn update_docs(&mut self, kernel_docs: &Path) -> Result<()> {
let documents = parse_kernel_docs(kernel_docs)?;
self.parameters
.par_iter_mut()
.filter(|p| p.description.is_none() || p.description.as_deref() == Some("[N/A]"))
@ -141,5 +141,6 @@ impl Sysctl {
}
}
});
Ok(())
}
}

View file

@ -17,6 +17,8 @@ For more details see {bin}(8)."#;
pub struct Args {
/// Path of the Linux kernel documentation.
pub kernel_docs: Option<PathBuf>,
/// Display all of the kernel parameters.
pub all: bool,
}
impl Args {
@ -25,10 +27,11 @@ impl Args {
let mut opts = Options::new();
opts.optflag("h", "help", "display this help and exit");
opts.optflag("V", "version", "output version information and exit");
opts.optflag("a", "all", "display all variables");
opts.optopt(
"d",
"kernel-docs",
"set the path of the linux kernel documentation",
"docs",
"set the path of the kernel documentation",
"<path>",
);
@ -51,6 +54,7 @@ impl Args {
} else {
Some(Args {
kernel_docs: matches.opt_str("d").map(PathBuf::from),
all: matches.opt_present("a"),
})
}
}

View file

@ -6,23 +6,15 @@
pub mod args;
use crate::args::Args;
use systeroid_core::error::{Error, Result};
use systeroid_core::parsers::PARSERS;
use systeroid_core::error::Result;
use systeroid_core::sysctl::Sysctl;
use systeroid_parser::document::Document;
/// Runs `systeroid`.
pub fn run(args: Args) -> Result<()> {
let mut sysctl = Sysctl::init()?;
if let Some(kernel_docs) = args.kernel_docs {
let documents = PARSERS
.iter()
.try_fold(Vec::new(), |mut documents, parser| {
documents.extend(parser.parse(&kernel_docs)?);
Ok::<Vec<Document>, Error>(documents)
})?;
sysctl.update_docs(documents);
sysctl.update_docs(&kernel_docs)?;
}
for param in sysctl.parameters {