Skip to content

Commit b54e7ea

Browse files
committed
add branch hints
1 parent 35d63ba commit b54e7ea

File tree

4 files changed

+15
-14
lines changed

4 files changed

+15
-14
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ members = [
2222
[dependencies]
2323
soa_derive_internal = {path = "soa-derive-internal", version = "0.14"}
2424
permutation = "0.4.0"
25+
branches = { version = "0.4.0", default-features = false }
2526

2627
[dev-dependencies]
2728
bencher = "0.1"

soa-derive-internal/src/ptr.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ pub fn derive(input: &Input) -> TokenStream {
108108
/// Similar to [`*const T::as_ref()`](https://doc.rust-lang.org/std/primitive.pointer.html#method.as_ref),
109109
/// with the same safety caveats.
110110
pub unsafe fn as_ref<'a>(self) -> Option<#ref_name<'a>> {
111-
if self.is_null() {
111+
if ::branches::unlikely(self.is_null()) {
112112
None
113113
} else {
114114
Some(#ref_name {
@@ -214,7 +214,7 @@ pub fn derive(input: &Input) -> TokenStream {
214214
/// Similar to [`*mut T::as_ref()`](https://doc.rust-lang.org/std/primitive.pointer.html#method.as_ref),
215215
/// with the same safety caveats.
216216
pub unsafe fn as_ref<'a>(self) -> Option<#ref_name<'a>> {
217-
if self.is_null() {
217+
if ::branches::unlikely(self.is_null()) {
218218
None
219219
} else {
220220
Some(#ref_name {
@@ -226,7 +226,7 @@ pub fn derive(input: &Input) -> TokenStream {
226226
/// Similar to [`*mut T::as_mut()`](https://doc.rust-lang.org/std/primitive.pointer.html#method.as_mut),
227227
/// with the same safety caveats.
228228
pub unsafe fn as_mut<'a>(self) -> Option<#ref_mut_name<'a>> {
229-
if self.is_null() {
229+
if ::branches::unlikely(self.is_null()) {
230230
None
231231
} else {
232232
Some(#ref_mut_name {

soa-derive-internal/src/slice.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ pub fn derive(input: &Input) -> TokenStream {
101101
#[doc = #slice_name_str]
102102
/// ::first()`](https://doc.rust-lang.org/std/primitive.slice.html#method.first).
103103
pub fn first(&self) -> Option<#ref_name<'a>> {
104-
if self.is_empty() {
104+
if ::branches::unlikely(self.is_empty()) {
105105
None
106106
} else {
107107
#(
@@ -115,7 +115,7 @@ pub fn derive(input: &Input) -> TokenStream {
115115
#[doc = #slice_name_str]
116116
/// ::split_first()`](https://doc.rust-lang.org/std/primitive.slice.html#method.split_first).
117117
pub fn split_first(&self) -> Option<(#ref_name<'a>, #slice_name<'a>)> {
118-
if self.is_empty() {
118+
if ::branches::unlikely(self.is_empty()) {
119119
None
120120
} else {
121121
#(
@@ -131,7 +131,7 @@ pub fn derive(input: &Input) -> TokenStream {
131131
#[doc = #slice_name_str]
132132
/// ::last()`](https://doc.rust-lang.org/std/primitive.slice.html#method.last).
133133
pub fn last(&self) -> Option<#ref_name<'a>> {
134-
if self.is_empty() {
134+
if ::branches::unlikely(self.is_empty()) {
135135
None
136136
} else {
137137
#(
@@ -145,7 +145,7 @@ pub fn derive(input: &Input) -> TokenStream {
145145
#[doc = #slice_name_str]
146146
/// ::split_last()`](https://doc.rust-lang.org/std/primitive.slice.html#method.split_last).
147147
pub fn split_last(&self) -> Option<(#ref_name<'a>, #slice_name<'a>)> {
148-
if self.is_empty() {
148+
if ::branches::unlikely(self.is_empty()) {
149149
None
150150
} else {
151151
#(
@@ -402,7 +402,7 @@ pub fn derive_mut(input: &Input) -> TokenStream {
402402
#[doc = #slice_name_str]
403403
/// ::first_mut()`](https://doc.rust-lang.org/std/primitive.slice.html#method.first_mut).
404404
pub fn first_mut(&mut self) -> Option<#ref_mut_name> {
405-
if self.is_empty() {
405+
if ::branches::unlikely(self.is_empty()) {
406406
None
407407
} else {
408408
#(
@@ -420,7 +420,7 @@ pub fn derive_mut(input: &Input) -> TokenStream {
420420
/// You should use [`Self::reborrow()`] first if you want the
421421
/// returned values to have a shorter lifetime.
422422
pub fn split_first_mut(mut self) -> Option<(#ref_mut_name<'a>, #slice_mut_name<'a>)> {
423-
if self.is_empty() {
423+
if ::branches::unlikely(self.is_empty()) {
424424
None
425425
} else {
426426
#(
@@ -436,7 +436,7 @@ pub fn derive_mut(input: &Input) -> TokenStream {
436436
#[doc = #slice_name_str]
437437
/// ::last_mut()`](https://doc.rust-lang.org/std/primitive.slice.html#method.last_mut).
438438
pub fn last_mut(&mut self) -> Option<#ref_mut_name> {
439-
if self.is_empty() {
439+
if ::branches::unlikely(self.is_empty()) {
440440
None
441441
} else {
442442
#(
@@ -454,7 +454,7 @@ pub fn derive_mut(input: &Input) -> TokenStream {
454454
/// You should use [`Self::reborrow()`] first if you want the
455455
/// returned values to have a shorter lifetime.
456456
pub fn split_last_mut(mut self) -> Option<(#ref_mut_name<'a>, #slice_mut_name<'a>)> {
457-
if self.is_empty() {
457+
if ::branches::unlikely(self.is_empty()) {
458458
None
459459
} else {
460460
#(

soa-derive-internal/src/vec.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ pub fn derive(input: &Input) -> TokenStream {
196196
/// ::insert()`](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.insert).
197197
#[allow(clippy::forget_non_drop)]
198198
pub fn insert(&mut self, index: usize, element: #name) {
199-
if index >= self.len() {
199+
if ::branches::unlikely(index >= self.len()) {
200200
panic!("index out of bounds: the len is {} but the index is {}", self.len(), index);
201201
}
202202

@@ -213,7 +213,7 @@ pub fn derive(input: &Input) -> TokenStream {
213213
/// Similar to [`std::mem::replace()`](https://doc.rust-lang.org/std/mem/fn.replace.html).
214214
#[allow(clippy::forget_non_drop)]
215215
pub fn replace(&mut self, index: usize, element: #name) -> #name {
216-
if index >= self.len() {
216+
if ::branches::unlikely(index >= self.len()) {
217217
panic!("index out of bounds: the len is {} but the index is {}", self.len(), index);
218218
}
219219

@@ -244,7 +244,7 @@ pub fn derive(input: &Input) -> TokenStream {
244244
#[doc = #vec_name_str]
245245
/// ::pop()`](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.pop).
246246
pub fn pop(&mut self) -> Option<#name> {
247-
if self.is_empty() {
247+
if ::branches::unlikely(self.is_empty()) {
248248
None
249249
} else {
250250
#(

0 commit comments

Comments
 (0)