Skip to content

Commit

Permalink
Performance improvement - hopefully shouldn't break anything
Browse files Browse the repository at this point in the history
  • Loading branch information
funnyboy-roks committed Mar 27, 2023
1 parent 37be241 commit e59eae8
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/target
*.svg
perf.*
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "marquee"
version = "1.0.0"
version = "1.1.0"
edition = "2021"
authors = ["funnyboy_roks <funnyboyroks@gmail.com>"]
description = "Convert stdin into a \"marquee\" style output"
Expand All @@ -13,3 +13,6 @@ repository = "https://github.com/funnyboy-roks/marquee"
clap = { version = "4.1.8", default-features = true, features = ["derive"] }
serde = { version = "1.0.152", default-features = true, features = ["derive"] }
serde_json = "1.0.94"

#[profile.release]
#debug = true
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ for more or PRs to implement these)
- This is sort of possible with `marquee -ld0`, but it's not too
elegant it doesn't work for multiple lines.
- Ideally, we move it to not need multiple threads, as well
- Perhaps we could make a command that returns an iterator that is
- Perhaps we could make a function that returns an iterator that is
used in all applications, something like `MarqueeIter::new(line: String)`
that will be an `Iterator<Item = String>` where `.next()` calculates
the next string. This would be infinite if `--no-loop` is not set.
Expand Down
16 changes: 15 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,27 @@ fn start_timer(current_str: &Arc<Mutex<String>>, options: Cli) -> thread::JoinHa
let mut prev_out = String::new();
loop {
let start = Instant::now();
let mut out = arc_str.lock().unwrap().clone();
let out = arc_str.lock().unwrap();

// If there is no input, don't print anything
if out.is_empty() {
// Manually drop the lock on `arc_str` so that the stdin thread can put
// something new into it.
// (this is probably not the best way, but it works :shrug:)
drop(out);

// sleep so that it doesn't loop as fast as possible and devour the CPU (totally
// not known from personal experience)
if let Some(remaining) = wait_time.checked_sub(start.elapsed()) {
thread::sleep(remaining);
}

continue;
}

let mut out = out.clone(); // Clone the String and drop the lock on `arc_str` so that we
// can receive new input

// If `--json`, then parse the json
let json: Option<JsonInput> = options
.json
Expand Down

0 comments on commit e59eae8

Please sign in to comment.