Skip to content

Make tab extension lazy#684

Merged
djc merged 1 commit intoconsole-rs:mainfrom
jaheba:lazy-tab-expansion
Jan 15, 2025
Merged

Make tab extension lazy#684
djc merged 1 commit intoconsole-rs:mainfrom
jaheba:lazy-tab-expansion

Conversation

@jaheba
Copy link
Contributor

@jaheba jaheba commented Jan 13, 2025

Running the snippet of #683 a big chunk of the time is spent in allocating a new string in TabExpandedString. Delaying the expanding of tabs reduces the runtime of ~100ms to ~60ms.

The idea is simple: Rather than replacing \t with spaces immediately, this is done once expanded is actually called.

@chris-laplante
Copy link
Collaborator

Makes sense to me, thanks! Just kicked of CI to verify.

Copy link
Member

@djc djc left a comment

Choose a reason for hiding this comment

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

Thanks, that looks like a nice win!

Also, would be nice if you can squash your commits.

WithTabs {
original: Cow<'static, str>,
expanded: String,
expanded: OnceCell<String>,
Copy link
Member

Choose a reason for hiding this comment

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

Can we just use Option instead of OnceCell for this? Seems simpler.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, I don't think so: We don't have mut access in expanded. AFAIK, that's essentially what OnceCell is there for.

Copy link
Member

Choose a reason for hiding this comment

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

Fair enough!

@jaheba jaheba force-pushed the lazy-tab-expansion branch from e8de6a3 to 803b013 Compare January 15, 2025 15:50
@djc djc merged commit fff1218 into console-rs:main Jan 15, 2025
10 checks passed
@tgross35
Copy link
Contributor

This change seems to have made ProgressStyle non-Sync, we are getting some failures like https://github.com/rust-lang/libm/actions/runs/13008267806/job/36279959248?pr=483. I can probably work around this in my application, but if the change wasn't intentional, would it be possible to change to OnceLock?

@djc
Copy link
Member

djc commented Jan 28, 2025

@tgross35 ah, sorry about that. Would you be able to submit a PR, including a test for ProgressStyle being Sync?

tgross35 added a commit to tgross35/indicatif that referenced this pull request Jan 28, 2025
The change at [1] stores some data in a `OnceCell`, which made
`ProgressStyle` no longer `Sync`. Fix this by changing the `OnceCell` to
a `OnceLock`, and add a static test that all relevant public types are
`Send` and `Sync`.

[1]: console-rs#684
@tgross35
Copy link
Contributor

Sure! Done at #694

djc pushed a commit that referenced this pull request Jan 28, 2025
The change at [1] stores some data in a `OnceCell`, which made
`ProgressStyle` no longer `Sync`. Fix this by changing the `OnceCell` to
a `OnceLock`, and add a static test that all relevant public types are
`Send` and `Sync`.

[1]: #684
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.

4 participants