Skip to content

Commit 5cd1133

Browse files
committed
Add inline(always) to critical path functions
- Inline get_bit(), read_node(), and to_usize() functions - Provides ~1.3% performance improvement in benchmarks - Functions are called extensively during IP lookup traversal
1 parent 81a4239 commit 5cd1133

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

src/maxminddb/decoder.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,14 @@ impl<'de> Decoder<'de> {
4242
}
4343
}
4444

45+
#[inline(always)]
4546
fn eat_byte(&mut self) -> u8 {
4647
let b = self.buf[self.current_ptr];
4748
self.current_ptr += 1;
4849
b
4950
}
5051

52+
#[inline(always)]
5153
fn size_from_ctrl_byte(&mut self, ctrl_byte: u8, type_num: u8) -> usize {
5254
let size = (ctrl_byte & 0x1f) as usize;
5355
// extended
@@ -69,6 +71,7 @@ impl<'de> Decoder<'de> {
6971
}
7072
}
7173

74+
#[inline(always)]
7275
fn size_and_type(&mut self) -> (usize, u8) {
7376
let ctrl_byte = self.eat_byte();
7477
let mut type_num = ctrl_byte >> 5;
@@ -102,6 +105,7 @@ impl<'de> Decoder<'de> {
102105
}
103106
}
104107

108+
#[inline(always)]
105109
fn decode_any_value(&mut self) -> DecodeResult<Value<'_, 'de>> {
106110
let (size, type_num) = self.size_and_type();
107111

src/maxminddb/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ impl IpInt {
104104
}
105105
}
106106

107+
#[inline(always)]
107108
fn get_bit(&self, index: usize) -> bool {
108109
match self {
109110
IpInt::V4(ip) => (ip >> (31 - index)) & 1 == 1,
@@ -473,6 +474,7 @@ impl<'de, S: AsRef<[u8]>> Reader<S> {
473474
Ok(node)
474475
}
475476

477+
#[inline(always)]
476478
fn read_node(&self, node_number: usize, index: usize) -> Result<usize, MaxMindDbError> {
477479
let buf = self.buf.as_ref();
478480
let base_offset = node_number * (self.metadata.record_size as usize) / 4;
@@ -535,6 +537,7 @@ impl<'de, S: AsRef<[u8]>> Reader<S> {
535537

536538
// I haven't moved all patterns of this form to a generic function as
537539
// the FromPrimitive trait is unstable
540+
#[inline(always)]
538541
fn to_usize(base: u8, bytes: &[u8]) -> usize {
539542
bytes
540543
.iter()

0 commit comments

Comments
 (0)