Skip to content

Faster parsing for lower numbers for radix up to 16 - #83371

Closed
gilescope wants to merge 8 commits into
rust-lang:masterfrom
gilescope:plan_b
Closed

Faster parsing for lower numbers for radix up to 16#83371
gilescope wants to merge 8 commits into
rust-lang:masterfrom
gilescope:plan_b

Conversation

@gilescope

Copy link
Copy Markdown
Contributor

While musing on #83088 I noticed that we could use str len to establish if the input was guarenteed not to overflow the containing type.

From what I can see this is conservative enough (i64 having the least wiggle room) that we can safely skip the overflow checks because we have ruled out overflow and that seems to improve performance by 1/4 to 1/3 for numbers in the range. Given that most numbers are statistically on the small side rather than near the type limits, I'm tempted by this. As it stands it doesn't really help i8 much as it's so small a range of numbers, but it does work well with u8 ( FF ) which is far more common.

I don't like adding unsafety, but this might be worth it.

@rust-highfive

Copy link
Copy Markdown
Contributor

r? @kennytm

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfiverust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 22, 2021
@gilescopegilescope changed the title WIP: Significantly faster parsing for lower numbers for radix up to 16WIP: Faster parsing for lower numbers for radix up to 16Mar 22, 2021
@rust-log-analyzer

This comment has been minimized.

@jyn514jyn514 added I-slow Issue: Problems and improvements with respect to performance of generated code. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Mar 22, 2021
Comment threadlibrary/core/src/num/mod.rs Outdated
Comment threadlibrary/core/src/num/mod.rs Outdated
@rust-log-analyzer

This comment has been minimized.

Comment threadlibrary/core/src/num/mod.rs Outdated
@rust-log-analyzer

This comment has been minimized.

Comment threadlibrary/core/src/num/mod.rs Outdated
@rust-log-analyzer

This comment has been minimized.

@crlf0710

Copy link
Copy Markdown
Member

@gilescope Ping from triage! CI is still red here. Mind fixing that?

@crlf0710crlf0710 added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 23, 2021
@pickfire

Copy link
Copy Markdown
Contributor

I believe the other pull request superseeds this pull request. Which @gilescope is still experimenting with.

@gilescope

Copy link
Copy Markdown
ContributorAuthor

@pickfire while I'd like to find a way to improve base10 parsing specifically, this is a simple speedup for all radix with little down side. It focuses on small numbers which are in general more common so it's likely to kick in most of the time.

I think given the simplicity we should move forward with this PR.

@gilescopegilescope changed the title WIP: Faster parsing for lower numbers for radix up to 16Faster parsing for lower numbers for radix up to 16May 3, 2021
@pickfire

Copy link
Copy Markdown
Contributor

small numbers

You may also want to write down how small there.

Comment threadlibrary/core/src/num/mod.rs Outdated
Comment threadlibrary/core/src/num/mod.rs Outdated
@rust-log-analyzer

This comment has been minimized.

Comment threadlibrary/core/src/num/mod.rs Outdated
Comment threadlibrary/core/src/num/mod.rs Outdated
Comment threadlibrary/core/src/num/mod.rs Outdated
Comment threadlibrary/core/src/num/mod.rs Outdated

@LingManLingMan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Two more nits and you might want to squash down your commits a bit but otherwise LGTM.

Comment threadlibrary/core/src/num/mod.rs Outdated
Comment threadlibrary/core/src/num/mod.rs Outdated
Comment threadlibrary/core/src/num/mod.rs Outdated
Co-authored-by: LingMan <LingMan@users.noreply.github.com>
@gilescope

Copy link
Copy Markdown
ContributorAuthor

Hmm, redoing the perf testing pulling out let unchecked_additive_op = to prevent the duplication does lead to a perormance penalty. :-(

@JohnCSimonJohnCSimon added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 5, 2021
@crlf0710

Copy link
Copy Markdown
Member

@gilescope Ping from triage, what's next steps here?

@crlf0710crlf0710 added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 24, 2021
@crlf0710

Copy link
Copy Markdown
Member

@gilescope Ping from triage, any updates here?

@camelidcamelid added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 2, 2021
@JohnCSimonJohnCSimon added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 20, 2021
@camelidcamelid added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 8, 2021
@JohnCSimonJohnCSimon added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 24, 2021
@JohnCSimon

Copy link
Copy Markdown

@gilescope Ping from triage. Can you please post the status of this MR?

@LingMan

Copy link
Copy Markdown
Contributor

@gilescope Does replacing the contents of the unsafe block with this, resolve the performance issue you saw?

macro_rules! run_loop {($unchecked_additive_op:ident) => {for&c in digits {
result = result.unchecked_mul(radix);let x = (c aschar).to_digit(radix).ok_or(PIE{ kind:InvalidDigit})?;
result = T::$unchecked_additive_op(&result, x);}}}if is_positive {run_loop!(unchecked_add)}else{run_loop!(unchecked_sub)};

@JohnCSimon

Copy link
Copy Markdown

@gilescope
Ping from triage: I'm closing this one due to not being touched since june , please feel to reopen when you are ready to continue with this.

@rustbot label: +S-inactive

@rustbotrustbot added the S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. label Dec 12, 2021
@gilescope

Copy link
Copy Markdown
ContributorAuthor

@JohnCSimon can we reopen this PR. I didn't see LingMan's comment. I think this is good now with LingMan's change. Is it possible to kick off a perf run? (I've rebased to bring this up to date with master)

@gilescope

Copy link
Copy Markdown
ContributorAuthor

(Sorry I've been away a long time.)

@kennytm

Copy link
Copy Markdown
Member

the branch was force-pushed or recreated so the PR can't be re-opened. you could submit a new PR.

bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 12, 2022
Faster parsing for lower numbers for radix up to 16 (cont.)
( Continuation of rust-lang#83371 )
With LingMan's change I think this is potentially ready.
workingjubilee pushed a commit to pgcentralfoundation/postgrestd that referenced this pull request Sep 15, 2022
Faster parsing for lower numbers for radix up to 16 (cont.)
( Continuation of rust-lang/rust#83371 )
With LingMan's change I think this is potentially ready.
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

I-slowIssue: Problems and improvements with respect to performance of generated code.S-inactiveStatus: Inactive and waiting on the author. This is often applied to closed PRs.S-waiting-on-authorStatus: This is awaiting some action (such as code changes or more information) from the author.T-libsRelevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

12 participants

@gilescope@rust-highfive@rust-log-analyzer@crlf0710@pickfire@JohnCSimon@LingMan@kennytm@joshtriplett@jyn514@camelid@rustbot