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

Only get draw_target-width when we actually draw #683

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

jaheba
Copy link

@jaheba jaheba commented Jan 13, 2025

When calling BarState::draw, there is a call to self.draw_target.width().

However, that operation can be expensive and the width is not needed, if there is no draw-target.

Thus, this PR delays the calling .width() if there is not target to draw to.

For comparison, I've used this minibenchmark:

    let bar = ProgressBar::new_spinner();

    for n in 0..1_000_000 {
        bar.set_message(format!("{}", n));
    }
    bar.finish();

Using the latest release it runs in about 400ms, whilst the fixed version uses about 100ms.

The issue surfaced with uv, when resolving many packages: astral-sh/uv#10384

src/state.rs Outdated

let mut draw_state = drawable.state();
// First check whether there is something to be drawn.
if self.draw_target.drawable(force_draw, now).is_none() {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This double call to drawable is sort of ugly in my eyes.

However, the returned drawable prevents us from calling self.draw_target.width(), since the mutable borrow is still active.

@jaheba
Copy link
Author

jaheba commented Jan 13, 2025

@djc Tests pass locally now.

@jaheba
Copy link
Author

jaheba commented Jan 13, 2025

Actually, this is not working, since self.draw_target.drawable(...) will set the time of latest update, and then second call to .drawable can return None.

@jaheba
Copy link
Author

jaheba commented Jan 13, 2025

Should (hopefully) work now.

The contrast in speed is actually bigger when using hyperfine --show-output. The fixed version takes ~120ms, while the current version takes ~700ms.

@jaheba
Copy link
Author

jaheba commented Jan 13, 2025

@djc Let me know if you need any else for this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant