Skip to content

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

Merged
Lokathor merged 2 commits intoLokathor:mainfrom
saethlin:optimize-push
Jan 14, 2021
Merged

Outline the drain to heap logic in TinyVec::push#127
Lokathor merged 2 commits intoLokathor:mainfrom
saethlin:optimize-push

Conversation

@saethlin
Copy link
Contributor

Per #39

This is a strategy I've seen be very useful in other places; keeping code size down by forcibly outlining algorithmically cold paths tends to make a huge difference.


if let Some(x) = arr.try_push(val) {
#[inline(never)]
fn drain_to_heap_and_push<A: Array>(
Copy link
Collaborator

Choose a reason for hiding this comment

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

You can also try #[cold] annotation, this should provide branch predictor hints for the match below.

Also, could you add a comment explaining why this code is in a separate function?

Copy link
Owner

Choose a reason for hiding this comment

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

A comment would be good about what's going on here.

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'm unable to measure a difference between #[inline(never)] and #[cold]. I've opted for #[cold] on the basis that if the optimizer somehow concludes that this function is profitable to inline, that is permitted.

@Lokathor
Copy link
Owner

Looks good. @saethlin i can merge this if you're satisfied.

@saethlin
Copy link
Contributor Author

@Lokathor Yes please

@Lokathor Lokathor merged commit 7bb8a78 into Lokathor:main Jan 14, 2021
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