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

simplify eat_digits #81427

Merged
merged 1 commit into from
Feb 20, 2021
Merged

simplify eat_digits #81427

merged 1 commit into from
Feb 20, 2021

Conversation

klensy
Copy link
Contributor

@klensy klensy commented Jan 27, 2021

Simplify eat_digits by checking values in iterator, plus decrease function size, by returning unchecked slices.

https://godbolt.org/z/cxjav4

@rust-highfive
Copy link
Collaborator

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

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 27, 2021
@tesuji
Copy link
Contributor

tesuji commented Jan 27, 2021

I'd prefer to fix the LLVM optimizing issue to using unsafe code for this.

@klensy
Copy link
Contributor Author

klensy commented Jan 27, 2021

Also works that:

#![feature(core_intrinsics)]
use std::intrinsics::assume;

pub fn eat_digits(s: &[u8]) -> (&[u8], &[u8]) {
    let sep = s.iter().take_while(|x| (b'0'..=b'9').contains(x)).count();

    unsafe{ assume(sep <= s.len()); };

    (&s[..sep], &s[sep..])
}

@klensy
Copy link
Contributor Author

klensy commented Jan 27, 2021

Or that without bound check, but looks ugly:

pub fn eat_digits(s: &[u8]) -> (&[u8], &[u8]) {
    for i in 0..s.len(){
        if !s[i].is_ascii_digit(){
            return s.split_at(i);
        }
    }
    s.split_at(s.len())
}

https://godbolt.org/z/nxqz4n

@camsteffen
Copy link
Contributor

How about

pub fn eat_digits(s: &[u8]) -> (&[u8], &[u8]) {
    let pos = s.iter().position(|c| !c.is_ascii_digit()).unwrap_or(s.len());
    s.split_at(pos)
}

@LingMan
Copy link
Contributor

LingMan commented Jan 28, 2021

@camsteffen's version looks nice. It compiles to the exact same assembly as the versions with get_unchecked/assume and is the easiest to read if you ask me.

@klensy
Copy link
Contributor Author

klensy commented Jan 28, 2021

@camsteffen Yes, this looks nice, can i PR this version?

@camsteffen
Copy link
Contributor

Yes you can use it 🙂

@sanxiyn sanxiyn 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 Jan 31, 2021
@klensy
Copy link
Contributor Author

klensy commented Feb 19, 2021

This removes bound checks, and as this function get inlined, few more.

@rustbot label -S-waiting-on-author +S-waiting-on-review

@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 Feb 19, 2021
@m-ou-se
Copy link
Member

m-ou-se commented Feb 19, 2021

@bors r+

@bors
Copy link
Contributor

bors commented Feb 19, 2021

📌 Commit ec09d7f has been approved by m-ou-se

@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-review Status: Awaiting review from the assignee but also interested parties. labels Feb 19, 2021
@bors
Copy link
Contributor

bors commented Feb 20, 2021

⌛ Testing commit ec09d7f with merge da5f7f1...

@bors
Copy link
Contributor

bors commented Feb 20, 2021

☀️ Test successful - checks-actions
Approved by: m-ou-se
Pushing da5f7f1 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Feb 20, 2021
@bors bors merged commit da5f7f1 into rust-lang:master Feb 20, 2021
@rustbot rustbot added this to the 1.52.0 milestone Feb 20, 2021
@rust-log-analyzer
Copy link
Collaborator

A job failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at da5f7f109 Auto merge of #81427 - klensy:eat-digits, r=m-ou-se
##[group]Run src/ci/publish_toolstate.sh
src/ci/publish_toolstate.sh
env:
  SCCACHE_BUCKET: rust-lang-ci-sccache2
  DEPLOY_BUCKET: rust-lang-ci2
  TOOLSTATE_REPO: https://github.com/rust-lang-nursery/rust-toolstate
---
  CACHE_DOMAIN: ci-caches.rust-lang.org
  TOOLSTATE_REPO_ACCESS_TOKEN: ***
##[endgroup]
Cloning into 'rust-toolstate'...
/home/runner/work/rust/rust/src/tools/publish_toolstate.py:121: DeprecationWarning: 'U' mode is deprecated
📣 Toolstate changed by rust-lang/rust#81427!
  with open(path, 'rU') as f:

Tested on commit rust-lang/rust@da5f7f10936198bad22c370118bad1ac332d2f46.
Direct link to PR: <https://github.com/rust-lang/rust/pull/81427>

💔 miri on windows: test-fail → build-fail (cc @eddyb @RalfJung @oli-obk).
💔 miri on linux: test-fail → build-fail (cc @eddyb @RalfJung @oli-obk).
Traceback (most recent call last):
Traceback (most recent call last):
  File "/home/runner/work/rust/rust/src/tools/publish_toolstate.py", line 338, in <module>
    response = urllib2.urlopen(urllib2.Request(
  File "/usr/lib/python3.8/urllib/request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python3.8/urllib/request.py", line 522, in open
    req = meth(req)
  File "/usr/lib/python3.8/urllib/request.py", line 1281, in do_request_
    raise TypeError(msg)
TypeError: POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str.
##[error]Process completed with exit code 1.

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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants