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

refactor: make clippy almost silent, some optimizations #916

Closed
wants to merge 4 commits into from

Conversation

yusdacra
Copy link
Contributor

@yusdacra yusdacra commented Jun 14, 2021

I was annoyed by the many clippy warnings so I tried to make it shut up as much as possible.

This PR also reduces allocations in text_input. It adds one new dependency, smol_str.

Commit 9be7a36 changes some public API (especially Toggler) to make them take Into<SmolStr> instead of Into<String>. This causes some minimal breakages (easily fixable, see tour example changes + i think it looks nicer), but if it's not okay I can leave it out.

@yusdacra yusdacra force-pushed the refactor/clippy-fixes branch from 9c3e001 to c8f1b19 Compare June 14, 2021 22:19
@yusdacra yusdacra changed the title refactor: make clippy almost silent refactor: make clippy almost silent, some optimizations Jun 14, 2021
@yusdacra yusdacra force-pushed the refactor/clippy-fixes branch from c8f1b19 to 8dc5e86 Compare June 14, 2021 22:23
@yusdacra yusdacra force-pushed the refactor/clippy-fixes branch 4 times, most recently from d10e3f6 to a94de1d Compare June 15, 2021 00:50
@yusdacra yusdacra force-pushed the refactor/clippy-fixes branch from a94de1d to 9be7a36 Compare June 15, 2021 01:37
f32::from(text_size + self.padding.vertical())
* self.options.len() as f32,
f32::from(
self.text_size.unwrap_or(renderer.default_size())

Choose a reason for hiding this comment

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

self.text_size.unwrap_or_else(|| renderer.default_size()) better.
A little memory optimization here and not have to make an exclusion for the linter.

I would even make a separate structure with Default and Deref into u16. Then you could do .unwrap_or_default().

Especially since such an expression is very often used in the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This also returns a u16 which is stack allocated. Since it will be allocated in the stack we don't need lazy eval, and it's very cheap anyways (ideally default_size would be marked as const, but trait const fn's aren't finished yet), so we don't need the indirection.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

On second thought; Rust should figure out and just inline the value, bypassing the lazyness.

.filter(|(_, word)| !word.trim_start().is_empty())
.next()
#[allow(clippy::or_fun_call)]
// self.len() is very cheap, we don't need lazy eval

Choose a reason for hiding this comment

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

But why allocate memory every time it is not used?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

self.len() doesn't allocate any memory; it returns a usize which is stored on the stack. It will be stored on the stack even if we put it in a closure; so the closure is needless indirection.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same goes for here; Rust should inline the value.

@hecrj hecrj force-pushed the master branch 2 times, most recently from e6ab610 to 8b0f2e6 Compare January 19, 2022 15:04
@hecrj
Copy link
Member

hecrj commented Aug 27, 2022

Superseded by #1379.

@hecrj hecrj closed this Aug 27, 2022
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.

3 participants