Skip to content

Commit

Permalink
perf: do not lookup keys manually for hashmaps
Browse files Browse the repository at this point in the history
Signed-off-by: Amin Yahyaabadi <aminyahyaabadi74@gmail.com>
  • Loading branch information
aminya committed Dec 6, 2023
1 parent 4365997 commit 3f3bc7f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 15 deletions.
13 changes: 4 additions & 9 deletions src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,11 @@ impl Engine {
/// Given a Dag name, execute this Dag.
/// Returns true if the given Dag executes successfully, otherwise false.
pub fn run_dag(&mut self, name: &str) -> bool {
if !self.dags.contains_key(name) {
if let Some(dag) = self.dags.get(name) {
self.runtime.block_on(dag.run())
} else {
error!("No job named '{}'", name);
false
} else {
let job = self.dags.get(name).unwrap();
self.runtime.block_on(job.run())
}
}

Expand All @@ -92,11 +91,7 @@ impl Engine {

/// Given the name of the Dag, get the execution result of the specified Dag.
pub fn get_dag_result<T: Send + Sync + Clone + 'static>(&self, name: &str) -> Option<Arc<T>> {
if self.dags.contains_key(name) {
self.dags.get(name).unwrap().get_result()
} else {
None
}
self.dags.get(name).map(|dag| dag.get_result()).flatten()

Check failure on line 94 in src/engine/mod.rs

View workflow job for this annotation

GitHub Actions / Clippy

called `map(..).flatten()` on `Option`
}
}

Expand Down
10 changes: 4 additions & 6 deletions src/yaml/yaml_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,10 @@ impl Parser for YamlParser {
// Read tasks
for (v, w) in yaml_tasks {
let id = v.as_str().unwrap();
let task = if specific_actions.contains_key(id) {
let action = specific_actions.remove(id).unwrap();
self.parse_one(id, w, Some(action))?
} else {
self.parse_one(id, w, None)?
};
let task = specific_actions.remove(id).map_or_else(
|| self.parse_one(id, w, None),
|action| self.parse_one(id, w, Some(action)),
)?;
map.insert(id, task.id());
tasks.push(task);
}
Expand Down

0 comments on commit 3f3bc7f

Please sign in to comment.