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

Reduce code size of testing tokens if they're a number #5181

Merged
merged 1 commit into from
Oct 24, 2023

Conversation

alexcrichton
Copy link
Contributor

This commit is a tiny win in compiled code size of a final binary including clap which shaves off 19k of compiled code locally. Previously tokens were checked if they were a number by using .parse::<f64>().is_ok(), but parsing floats is relatively heavyweight in terms of code size. This replaces the check with a more naive "does this string have lots of ascii digits" check where the compiled size of this check should be much smaller.

I'll note that this is not a critical optimization at all and is one I'm mostly curious if it's of interest. There's other various edge cases that were previously classified as numbers which will no longer be classified as numbers such as nan, inf, +1, 1e10, etc. That would mean that this is technically a breaking change, and if that's not acceptable then this is most definitely not worth it.

@epage
Copy link
Member

epage commented Oct 23, 2023

I'll note that this is not a critical optimization at all and is one I'm mostly curious if it's of interest.

19k is a decent amount of savings so it seems worth it.

clap_lex/src/lib.rs Outdated Show resolved Hide resolved
clap_lex/src/lib.rs Outdated Show resolved Hide resolved
This commit is a tiny win in compiled code size of a final binary
including `clap` which shaves off 19k of compiled code locally.
Previously tokens were checked if they were a number by using
`.parse::<f64>().is_ok()`, but parsing floats is relatively heavyweight
in terms of code size. This replaces the check with a more naive "does
this string have lots of ascii digits" check where the compiled size of
this check should be much smaller.
@epage
Copy link
Member

epage commented Oct 24, 2023

Thanks!

@epage epage merged commit f801a03 into clap-rs:master Oct 24, 2023
21 of 22 checks passed
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.

2 participants