Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some more speed improvements to rust and rust_rayon #9

Merged
merged 3 commits into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ run_go() {
cd ./go &&
go build &&
if [ $HYPER == 1 ]; then
command hyperfine --show-output -r 10 -w 3 "./related"
command hyperfine -r 10 -w 3 --show-output "./related"
else
command time -f '%es %Mk' ./related
fi
Expand All @@ -24,7 +24,7 @@ run_go_concurrent() {
cd ./go_con &&
go build &&
if [ $HYPER == 1 ]; then
command hyperfine --show-output -r 10 -w 3 "./related_concurrent"
command hyperfine -r 10 -w 3 --show-output "./related_concurrent"
else
command time -f '%es %Mk' ./related_concurrent
fi
Expand All @@ -35,9 +35,9 @@ run_rust() {
cd ./rust &&
cargo build --release &&
if [ $HYPER == 1 ]; then
command hyperfine --show-output -r 10 -w 3 "./target/release/rust"
command hyperfine -r 10 -w 3 --show-output "./target/release/rust"
else
command ./target/release/rust
command time -f '%es %Mk' ./target/release/rust
fi
}

Expand All @@ -46,7 +46,7 @@ run_rust_rayon() {
cd ./rust_rayon &&
cargo build --release &&
if [ $HYPER == 1 ]; then
command hyperfine --show-output -r 10 -w 3 "./target/release/rust_rayon"
command hyperfine -r 10 -w 3 --show-output "./target/release/rust_rayon"
else
command time -f '%es %Mk' ./target/release/rust_rayon
fi
Expand All @@ -56,7 +56,7 @@ run_python() {
echo "Running Python" &&
cd ./python &&
if [ $HYPER == 1 ]; then
command hyperfine -r 1 "python3 ./related.py"
command hyperfine -r 2 "python3 ./related.py"
else
command time -f '%es %Mk' python3 ./related.py
fi
Expand Down Expand Up @@ -104,6 +104,15 @@ elif [ "$first_arg" = "all" ]; then
cd .. &&
run_python

elif [ "$first_arg" = "clean" ]; then

echo "cleaning" &&
cd go && rm -f related &&
cd .. &&
cd rust && cargo clean &&
cd .. &&
cd rust_rayon && cargo clean

else
echo "Valid args: go | rust | python | all. Unknown argument: $first_arg"
echo "Valid args: go | rust | python | all | clean. Unknown argument: $first_arg"
fi
20 changes: 20 additions & 0 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
mimalloc = "0.1.39"
rustc_data_structures = "0.0.1"
serde = { version = "1.0.188", features = ["derive"] }
serde_json = "1.0.107"
Expand Down
15 changes: 9 additions & 6 deletions rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ use rustc_data_structures::fx::FxHashMap;
use serde::{Deserialize, Serialize};
use serde_json::from_str;

use mimalloc::MiMalloc;
#[global_allocator]
static GLOBAL: MiMalloc = MiMalloc;

#[derive(Serialize, Deserialize)]
struct Post {
_id: String,
Expand Down Expand Up @@ -35,24 +39,23 @@ fn main() {

let mut related_posts: Vec<RelatedPosts> = Vec::with_capacity(posts.len());

let mut tagged_post_count = vec![0; posts.len()];

for (idx, post) in posts.iter().enumerate() {
tagged_post_count.fill(0);
let mut tagged_post_count = vec![0; posts.len()];

for tag in &post.tags {
if let Some(tag_posts) = post_tags_map.get(tag) {
for other_post_idx in tag_posts {
if idx != *other_post_idx {
tagged_post_count[*other_post_idx] += 1;
for &other_post_idx in tag_posts {
if idx != other_post_idx {
tagged_post_count[other_post_idx] += 1;
}
}
}
}

let mut top_five = BinaryHeap::new();
tagged_post_count
.iter()
.into_iter()
.enumerate()
.for_each(|(post, count)| {
if top_five.len() < 5 {
Expand Down
13 changes: 7 additions & 6 deletions rust_rayon/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,20 @@ fn main() {

posts.par_iter().enumerate().for_each(|(idx, post)| {
let mut tagged_post_count = vec![0; posts.len()];
tagged_post_count.fill(0);

for tag in &post.tags {
if let Some(tag_posts) = post_tags_map.get(tag) {
for other_post_idx in tag_posts {
if idx != *other_post_idx {
tagged_post_count[*other_post_idx] += 1;
for &other_post_idx in tag_posts {
if idx != other_post_idx {
tagged_post_count[other_post_idx] += 1;
}
}
}
}

let mut top_five = BinaryHeap::new();
tagged_post_count
.iter()
.into_iter()
.enumerate()
.for_each(|(post, count)| {
if top_five.len() < 5 {
Expand All @@ -76,10 +75,12 @@ fn main() {
}
});

let related = top_five.into_iter().map(|(_, post)| &posts[post]).collect();

related_posts.lock().unwrap().push(RelatedPosts {
_id: &post._id,
tags: &post.tags,
related: top_five.into_iter().map(|(_, post)| &posts[post]).collect(),
related,
});
});

Expand Down