Skip to content

Commit 3e3e623

Browse files
committed
refactor(main): improve 60%+
raw = 145.710541ms new = 88.962291ms
1 parent 1692c52 commit 3e3e623

File tree

1 file changed

+34
-25
lines changed

1 file changed

+34
-25
lines changed

example-rust/src/main.rs

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -48,31 +48,40 @@ async fn main() -> std::io::Result<()> {
4848
let start = std::time::Instant::now();
4949
let path = glob(FILE_DIR).expect("failed to read glob pattern");
5050

51-
let submissions = path.map(|entry| async {
52-
let yaml_path = entry.expect("should be existed");
53-
let txt_path = yaml_path.with_extension("txt");
54-
55-
let outdir_submission = async { create_outdir().await.expect("unable to create outdir") };
56-
57-
let read_text_file_submission = async {
58-
let data = read_file(&txt_path).await;
59-
let tokens = get_unique_token(&data);
60-
61-
tokens.join("\n")
62-
};
63-
64-
let filename_submission = async {
65-
get_filename_from_meta(&yaml_path)
66-
.await
67-
.expect("should be existed")
68-
};
69-
70-
let (tokens, filename, _) = tokio::join!(
71-
read_text_file_submission,
72-
filename_submission,
73-
outdir_submission,
74-
);
75-
fs::write(filename, tokens).await.expect("failed to write");
51+
let submissions = path.map(|entry| {
52+
tokio::spawn(async {
53+
let yaml_path = entry.expect("should be existed");
54+
let txt_path = yaml_path.with_extension("txt");
55+
56+
let outdir_submission =
57+
tokio::spawn(async { create_outdir().await.expect("unable to create outdir") });
58+
59+
let read_text_file_submission = tokio::spawn(async move {
60+
let data = read_file(&txt_path).await;
61+
let tokens = get_unique_token(&data);
62+
63+
tokens.join("\n")
64+
});
65+
66+
let filename_submission = tokio::spawn(async move {
67+
get_filename_from_meta(&yaml_path)
68+
.await
69+
.expect("should be existed")
70+
});
71+
72+
let (tokens, filename, _) = tokio::join!(
73+
read_text_file_submission,
74+
filename_submission,
75+
outdir_submission,
76+
);
77+
78+
fs::write(
79+
filename.expect("failed to run filename"),
80+
tokens.expect("failed to get tokens"),
81+
)
82+
.await
83+
.expect("failed to write");
84+
})
7685
});
7786

7887
futures::future::join_all(submissions).await;

0 commit comments

Comments
 (0)