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

Add inline tags to UninitSlice methods #443

Merged
merged 3 commits into from Apr 10, 2021

Conversation

danburkert
Copy link
Contributor

This appears to be the primary cause of significant performance
regressions in the prost test suite in the 0.5 to 0.6 transition. See
danburkert/prost#381.

This appears to be the primary cause of significant performance
regressions in the `prost` test suite in the 0.5 to 0.6 transition.  See
danburkert/prost#381.
Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

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

Thanks!

src/buf/uninit_slice.rs Outdated Show resolved Hide resolved
Co-authored-by: Alice Ryhl <alice@ryhl.io>
pub fn write_byte(&mut self, index: usize, byte: u8) {
assert!(index < self.len());

unsafe { self[index..].as_mut_ptr().write(byte) }
unsafe { self.as_mut_ptr().add(index).write(byte) }
Copy link
Member

Choose a reason for hiding this comment

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

I'd expect the bounds check to have been elided, since it's asserted just before... It's not?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't check the assembly, but empirically I found in #442 that a similar change had a measurable impact on microbenchmarks in the prost repo. It's very possible that the issue there was that the UninitSlice index call was not inlined, and so the similar double check wasn't eligible to be optimized away. Realistically I'm not going to have time to read the ASM here, but I'm happy to back out this part of the change if you'd prefer that.

Copy link
Member

Choose a reason for hiding this comment

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

Using s[i] vs s.as_ptr().add(i) gets me the same thing with the assert in the function:

example::index:
        push    rax
        cmp     rsi, rdx
        jbe     .LBB7_1
        mov     al, byte ptr [rdi + rdx]
        pop     rcx
        ret
.LBB7_1:
        lea     rdi, [rip + .L__unnamed_4]
        call    std::panicking::begin_panic
        ud2

example::ptr:
        push    rax
        cmp     rsi, rdx
        jbe     .LBB8_2
        mov     al, byte ptr [rdi + rdx]
        pop     rcx
        ret
.LBB8_2:
        lea     rdi, [rip + .L__unnamed_5]
        call    std::panicking::begin_panic
        ud2

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, would you like me to switch it back?

Copy link
Member

Choose a reason for hiding this comment

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

It seems reasonable to me to keep to safe things if they make no performance difference.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sounds good, updated.

@taiki-e taiki-e merged commit 3d5624a into tokio-rs:master Apr 10, 2021
@taiki-e
Copy link
Member

taiki-e commented Apr 10, 2021

Thanks!

@Darksonn Darksonn mentioned this pull request Aug 25, 2021
lelongg pushed a commit to lelongg/bytes that referenced this pull request Jan 9, 2023
This appears to be the primary cause of significant performance
regressions in the `prost` test suite in the 0.5 to 0.6 transition.  See
danburkert/prost#381.
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.

None yet

4 participants