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

Improve the floating point parser in dec2flt. #107007

Merged
merged 1 commit into from
Apr 10, 2023

Conversation

TDecking
Copy link
Contributor

@TDecking TDecking commented Jan 17, 2023

Greetings everyone,

I've benn studying the rust floating point parser recently and made the following tweaks:

  • Remove all remaining traces of unsafe. The parser is now 100% safe Rust.
  • The trick in which eight digits are processed in parallel is now in a loop.
  • Parsing of inf/NaN values has been reworked.

On my system, the changes result in performance improvements for some input values.

@rustbot
Copy link
Collaborator

rustbot commented Jan 17, 2023

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @joshtriplett (or someone else) soon.

Please see the contribution instructions for more information.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jan 17, 2023
@TDecking TDecking marked this pull request as draft January 17, 2023 21:33
@TDecking TDecking marked this pull request as ready for review January 17, 2023 23:15
@rustbot
Copy link
Collaborator

rustbot commented Jan 17, 2023

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

let e = self[4];
let f = self[5];
let g = self[6];
let h = self[7];
Copy link
Member

Choose a reason for hiding this comment

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

suggestion: maybe use one of the unstable methods to get an array without needing to write this all out?

Maybe something like

let first_8 = self.split_array_ref().0;
u64::from_le_bytes(*first_8)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, I've already written it all out. The suggested code does produce the desired assembly while being a bit shorter,
but I otherwise don't see the need to change anything. How likely is stabilisation of this method?

Copy link
Member

Choose a reason for hiding this comment

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

Something like what @scottmcm suggests seems more readable here. You can also do something like u64::from_le_bytes(self[..8].try_into().unwrap()) which probably works equally well and should be stable. (But we should confirm against generated code).

@TDecking TDecking marked this pull request as draft March 21, 2023 23:47
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@TDecking TDecking marked this pull request as ready for review March 23, 2023 19:43
@TDecking
Copy link
Contributor Author

r? t-libs

@rustbot
Copy link
Collaborator

rustbot commented Mar 30, 2023

Failed to set assignee to t-libs: invalid assignee

Note: Only org members, users with write permissions, or people who have commented on the PR may be assigned.

@TDecking
Copy link
Contributor Author

r? libs

@Mark-Simulacrum Mark-Simulacrum 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 2, 2023
@rust-log-analyzer

This comment has been minimized.

@TDecking
Copy link
Contributor Author

TDecking commented Apr 3, 2023

@Mark-Simulacrum I've made some changes corresponding to your comments.

  • read_u64 and write_u64 now use copy_from_slice.
  • reinserted the parse_digits function by adding it to the ByteSlice trait.

I've analyzed the changes using godbolt. read_u64 and write_u64 are reduced to one memory access and one bound check.
The latter should be elided after inlining. parse_digits relies on pattern matching on the slice instead of using split_first,
as #109328 makes it neccessary in order to avoid possible regressions.

@TDecking
Copy link
Contributor Author

TDecking commented Apr 7, 2023

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 7, 2023
@Mark-Simulacrum
Copy link
Member

Please squash away the fixup commits, r=me with that done.

@Mark-Simulacrum Mark-Simulacrum 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 9, 2023
@klensy
Copy link
Contributor

klensy commented Apr 9, 2023

* Remove all remaining traces of unsafe.
* Put `parse_8digits` inside a loop.
* Rework parsing of inf/NaN values.
@TDecking
Copy link
Contributor Author

@Mark-Simulacrum Commits are squashed, and the merge commit has been removed to comply with the No-Merge policy.

@Mark-Simulacrum
Copy link
Member

@bors r+ rollup=never

@bors
Copy link
Contributor

bors commented Apr 10, 2023

📌 Commit 0f96c71 has been approved by Mark-Simulacrum

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 10, 2023
@bors
Copy link
Contributor

bors commented Apr 10, 2023

⌛ Testing commit 0f96c71 with merge a732883...

@bors
Copy link
Contributor

bors commented Apr 10, 2023

☀️ Test successful - checks-actions
Approved by: Mark-Simulacrum
Pushing a732883 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Apr 10, 2023
@bors bors merged commit a732883 into rust-lang:master Apr 10, 2023
@rustbot rustbot added this to the 1.70.0 milestone Apr 10, 2023
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (a732883): comparison URL.

Overall result: ❌ regressions - no action needed

@rustbot label: -perf-regression

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.5% [0.5%, 0.5%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.5% [0.5%, 0.5%] 1

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
1.2% [1.2%, 1.2%] 1
Regressions ❌
(secondary)
3.0% [3.0%, 3.0%] 1
Improvements ✅
(primary)
-0.7% [-1.5%, -0.4%] 9
Improvements ✅
(secondary)
-1.3% [-1.3%, -1.3%] 1
All ❌✅ (primary) -0.6% [-1.5%, 1.2%] 10

Cycles

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.4% [0.4%, 0.4%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.4% [0.4%, 0.4%] 1

@TDecking TDecking deleted the float_parsing_improvments branch April 10, 2023 18:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant 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.

9 participants