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

Implement remove with swap instead of rotate_left #128

Merged
merged 1 commit into from
Jan 14, 2021

Conversation

saethlin
Copy link
Contributor

The codegen from slice::rotate_left is pretty ghastly; in my compiled benchmarks it's some 272 instructions with a number calls to memcpy and memmove, but only a few instructions are actually hot when it's run, and of course it isn't inlined (because it's huge?).

I think this implementation brings TinyVec::remove to parity with or possibly faster than SmallVec::remove.

@saethlin saethlin mentioned this pull request Jan 14, 2021
@Lokathor Lokathor merged commit 2d45d6e into Lokathor:main Jan 14, 2021
@Shnatsel
Copy link
Collaborator

This would also benefit from a comment on why it's written in this particular way. Doing it this way is important, but someone might later try to refactor it for readability and accidentally degrade performance. Something like:

    // This generates far fewer instructions than calling `targets.rotate_left(1);`
    // and performs much better for realistic ArrayVec lengths

@saethlin
Copy link
Contributor Author

@Shnatsel Thanks for the call-out. I'll have another PR up in a few hours that addresses this.

saethlin added a commit to saethlin/tinyvec that referenced this pull request Jan 16, 2021
add comparison script

Outline the drain to heap logic in TinyVec::push

Outline the drain to heap logic in TinyVec::push

Use #[cold] and explain the outlining in a comment

change gen_remove to rebuild sample Vec with clone instead of push

rename outer vec in gen_remove for clarity

Swap each element instead of calling rotate_left

add more loops to amortize away startup cost, tune tinyvec impls a bit

swap instead of rotate_right, explain in comment

drop loops again wheee

Outline the drain to heap logic in TinyVec::push (Lokathor#127)

* Outline the drain to heap logic in TinyVec::push

* Use #[cold] and explain the outlining in a comment

Swap each element instead of calling rotate_left (Lokathor#128)
saethlin added a commit to saethlin/tinyvec that referenced this pull request Jan 19, 2021
add comparison script

Outline the drain to heap logic in TinyVec::push

Outline the drain to heap logic in TinyVec::push

Use #[cold] and explain the outlining in a comment

change gen_remove to rebuild sample Vec with clone instead of push

rename outer vec in gen_remove for clarity

Swap each element instead of calling rotate_left

add more loops to amortize away startup cost, tune tinyvec impls a bit

swap instead of rotate_right, explain in comment

drop loops again wheee

Outline the drain to heap logic in TinyVec::push (Lokathor#127)

* Outline the drain to heap logic in TinyVec::push

* Use #[cold] and explain the outlining in a comment

Swap each element instead of calling rotate_left (Lokathor#128)
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.

3 participants