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

You are advised to optimize format performance #16

Closed
zhuxiujia opened this issue Jul 10, 2022 · 4 comments
Closed

You are advised to optimize format performance #16

zhuxiujia opened this issue Jul 10, 2022 · 4 comments

Comments

@zhuxiujia
Copy link
Contributor

zhuxiujia commented Jul 10, 2022

You are advised to optimize format performance
this change Can improve format performance several times

for example: this code

write!(f, "{:04}-{:02}-{:02}", self.year, self.month, self.day)

to My Code

impl Display for LogDate {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
       let mut buf: [u8; 29] = *b"0000-00-00 00:00:00.000000000";

        buf[0] = b'0' + (self.year / 1000) as u8;
        buf[1] = b'0' + (self.year / 100 % 10) as u8;
        buf[2] = b'0' + (self.year / 10 % 10) as u8;
        buf[3] = b'0' + (self.year % 10) as u8;


        buf[5] = b'0' + (self.mon / 10) as u8;
        buf[6] = b'0' + (self.mon % 10) as u8;

        buf[8] = b'0' + (self.day / 10) as u8;
        buf[9] = b'0' + (self.day % 10) as u8;

        buf[11] = b'0' + (self.hour / 10) as u8;
        buf[12] = b'0' + (self.hour % 10) as u8;
        buf[14] = b'0' + (self.min / 10) as u8;
        buf[15] = b'0' + (self.min % 10) as u8;
        buf[17] = b'0' + (self.sec / 10) as u8;
        buf[18] = b'0' + (self.sec % 10) as u8;

        buf[19] = b'.';

        buf[20] = b'0' + (self.nano / 100000000) as u8;
        buf[21] = b'0' + (self.nano / 10000000 % 10) as u8;
        buf[22] = b'0' + (self.nano / 1000000 % 10) as u8;
        buf[23] = b'0' + (self.nano / 100000 % 10) as u8;
        buf[24] = b'0' + (self.nano / 10000 % 10) as u8;
        buf[25] = b'0' + (self.nano / 1000 % 10) as u8;
        buf[26] = b'0' + (self.nano / 100 % 10) as u8;
        buf[27] = b'0' + (self.nano / 10 % 10) as u8;
        buf[28] = b'0' + (self.nano % 10) as u8;

        f.write_str(std::str::from_utf8(&buf[..]).unwrap())
    }
}
@samuelcolvin
Copy link
Member

Humm, but how slow is Display now?

If it's from 2ns to 0.1ns, I really don't think it's worth it.

also, could some of this be moved into a look to save somewhat on verbosity?

PR welcome and I'll review from there.

@zhuxiujia
Copy link
Contributor Author

zhuxiujia commented Jul 10, 2022

Humm, but how slow is Display now?

If it's from 2ns to 0.1ns, I really don't think it's worth it.

also, could some of this be moved into a look to save somewhat on verbosity?

PR welcome and I'll review from there.

this befor and after benchmark (from 343ns to 85ns/iter)
微信图片_20220710184428
微信图片_20220710184437

@samuelcolvin
Copy link
Member

ok, PR welcome.

samuelcolvin added a commit that referenced this issue Aug 16, 2022
* impl for #16

* use black_box bench

* use black_box bench

* format add trim_end_matches

* clear doc_test

* Update benches/main.rs

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>

* Update benches/main.rs

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>

* Update benches/main.rs

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>

* clear annotation

* clear repeat code

* clear repeat code

* tweak display_num_buf

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
@samuelcolvin
Copy link
Member

fixed by #17.

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

No branches or pull requests

2 participants