Skip to content

Commit

Permalink
perf: remove inline annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
BurntSushi committed Mar 7, 2020
1 parent a20083d commit 8bf84bc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 48 deletions.
2 changes: 1 addition & 1 deletion src/raw/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ impl<D: AsRef<[u8]>> Fst<D> {
}

/// Returns the root node of this fst.
#[inline(always)]
#[inline]
pub fn root(&self) -> Node<'_> {
self.as_ref().root()
}
Expand Down
57 changes: 10 additions & 47 deletions src/raw/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl<'f> Node<'f> {
}

/// Returns the transition at index `i`.
#[inline(always)]
#[inline]
pub fn transition(&self, i: usize) -> Transition {
use self::State::*;
match self.state {
Expand Down Expand Up @@ -156,7 +156,7 @@ impl<'f> Node<'f> {
}

/// Returns the transition address of the `i`th transition.
#[inline(always)]
#[inline]
pub fn transition_addr(&self, i: usize) -> CompiledAddr {
use self::State::*;
match self.state {
Expand All @@ -176,7 +176,7 @@ impl<'f> Node<'f> {
/// Finds the `i`th transition corresponding to the given input byte.
///
/// If no transition for this byte exists, then `None` is returned.
#[inline(always)]
#[inline]
pub fn find_input(&self, b: u8) -> Option<usize> {
use self::State::*;
match self.state {
Expand All @@ -191,46 +191,46 @@ impl<'f> Node<'f> {

/// If this node is final and has a terminal output value, then it is
/// returned. Otherwise, a zero output is returned.
#[inline(always)]
#[inline]
pub fn final_output(&self) -> Output {
self.final_output
}

/// Returns true if and only if this node corresponds to a final or "match"
/// state in the finite state transducer.
#[inline(always)]
#[inline]
pub fn is_final(&self) -> bool {
self.is_final
}

/// Returns the number of transitions in this node.
///
/// The maximum number of transitions is 256.
#[inline(always)]
#[inline]
pub fn len(&self) -> usize {
self.ntrans
}

/// Returns true if and only if this node has zero transitions.
#[inline(always)]
#[inline]
pub fn is_empty(&self) -> bool {
self.ntrans == 0
}

/// Return the address of this node.
#[inline(always)]
#[inline]
pub fn addr(&self) -> CompiledAddr {
self.start
}

#[doc(hidden)]
#[inline(always)]
#[inline]
pub fn as_slice(&self) -> &[u8] {
&self.data[self.end..]
}

#[doc(hidden)]
#[inline(always)]
#[inline]
pub fn state(&self) -> &'static str {
use self::State::*;
match self.state {
Expand Down Expand Up @@ -298,7 +298,6 @@ struct StateOneTrans(u8);
struct StateAnyTrans(u8);

impl State {
#[inline(always)]
fn new(data: &[u8], addr: CompiledAddr) -> State {
use self::State::*;
if addr == EMPTY_ADDRESS {
Expand Down Expand Up @@ -328,7 +327,6 @@ impl StateOneTransNext {
Ok(())
}

#[inline(always)]
fn new() -> Self {
StateOneTransNext(0b11_000000)
}
Expand All @@ -337,12 +335,10 @@ impl StateOneTransNext {
self.0 = (self.0 & 0b11_000000) | common_idx(input, 0b111111);
}

#[inline(always)]
fn common_input(&self) -> Option<u8> {
common_input(self.0 & 0b00_111111)
}

#[inline(always)]
fn input_len(&self) -> usize {
if self.common_input().is_none() {
1
Expand All @@ -351,12 +347,10 @@ impl StateOneTransNext {
}
}

#[inline(always)]
fn end_addr(&self, data: &[u8]) -> usize {
data.len() - 1 - self.input_len()
}

#[inline(always)]
fn input(&self, node: &Node<'_>) -> u8 {
if let Some(inp) = self.common_input() {
inp
Expand All @@ -365,7 +359,6 @@ impl StateOneTransNext {
}
}

#[inline(always)]
fn trans_addr(&self, node: &Node<'_>) -> CompiledAddr {
node.end as CompiledAddr - 1
}
Expand Down Expand Up @@ -404,12 +397,10 @@ impl StateOneTrans {
self.0 = (self.0 & 0b10_000000) | common_idx(input, 0b111111);
}

#[inline(always)]
fn common_input(&self) -> Option<u8> {
common_input(self.0 & 0b00_111111)
}

#[inline(always)]
fn input_len(&self) -> usize {
if self.common_input().is_none() {
1
Expand All @@ -418,13 +409,11 @@ impl StateOneTrans {
}
}

#[inline(always)]
fn sizes(&self, data: &[u8]) -> PackSizes {
let i = data.len() - 1 - self.input_len() - 1;
PackSizes::decode(data[i])
}

#[inline(always)]
fn end_addr(&self, data: &[u8], sizes: PackSizes) -> usize {
data.len() - 1
- self.input_len()
Expand All @@ -433,7 +422,6 @@ impl StateOneTrans {
- sizes.output_pack_size()
}

#[inline(always)]
fn input(&self, node: &Node<'_>) -> u8 {
if let Some(inp) = self.common_input() {
inp
Expand All @@ -442,7 +430,6 @@ impl StateOneTrans {
}
}

#[inline(always)]
fn output(&self, node: &Node<'_>) -> Output {
let osize = node.sizes.output_pack_size();
if osize == 0 {
Expand All @@ -456,7 +443,6 @@ impl StateOneTrans {
Output::new(bytes::unpack_uint(&node.data[i..], osize as u8))
}

#[inline(always)]
fn trans_addr(&self, node: &Node<'_>) -> CompiledAddr {
let tsize = node.sizes.transition_pack_size();
let i = node.start
Expand Down Expand Up @@ -541,7 +527,6 @@ impl StateAnyTrans {
Ok(())
}

#[inline(always)]
fn new() -> Self {
StateAnyTrans(0b00_000000)
}
Expand All @@ -552,7 +537,6 @@ impl StateAnyTrans {
}
}

#[inline(always)]
fn is_final_state(&self) -> bool {
self.0 & 0b01_000000 == 0b01_000000
}
Expand All @@ -563,7 +547,6 @@ impl StateAnyTrans {
}
}

#[inline(always)]
fn state_ntrans(&self) -> Option<u8> {
let n = self.0 & 0b00_111111;
if n == 0 {
Expand All @@ -573,13 +556,11 @@ impl StateAnyTrans {
}
}

#[inline(always)]
fn sizes(&self, data: &[u8]) -> PackSizes {
let i = data.len() - 1 - self.ntrans_len() - 1;
PackSizes::decode(data[i])
}

#[inline(always)]
fn total_trans_size(
&self,
version: u64,
Expand All @@ -590,7 +571,6 @@ impl StateAnyTrans {
ntrans + (ntrans * sizes.transition_pack_size()) + index_size
}

#[inline(always)]
fn trans_index_size(&self, version: u64, ntrans: usize) -> usize {
if version >= 2 && ntrans > TRANS_INDEX_THRESHOLD {
256
Expand All @@ -599,7 +579,6 @@ impl StateAnyTrans {
}
}

#[inline(always)]
fn ntrans_len(&self) -> usize {
if self.state_ntrans().is_none() {
1
Expand All @@ -608,7 +587,6 @@ impl StateAnyTrans {
}
}

#[inline(always)]
fn ntrans(&self, data: &[u8]) -> usize {
if let Some(n) = self.state_ntrans() {
n as usize
Expand All @@ -624,7 +602,6 @@ impl StateAnyTrans {
}
}

#[inline(always)]
fn final_output(
&self,
version: u64,
Expand All @@ -645,7 +622,6 @@ impl StateAnyTrans {
Output::new(bytes::unpack_uint(&data[at..], osize as u8))
}

#[inline(always)]
fn end_addr(
&self,
version: u64,
Expand All @@ -663,7 +639,6 @@ impl StateAnyTrans {
- final_osize // final output
}

#[inline(always)]
fn trans_addr(&self, node: &Node<'_>, i: usize) -> CompiledAddr {
assert!(i < node.ntrans);
let tsize = node.sizes.transition_pack_size();
Expand All @@ -677,7 +652,6 @@ impl StateAnyTrans {
unpack_delta(&node.data[at..], tsize, node.end)
}

#[inline(always)]
fn input(&self, node: &Node<'_>, i: usize) -> u8 {
let at = node.start
- self.ntrans_len()
Expand All @@ -688,7 +662,6 @@ impl StateAnyTrans {
node.data[at]
}

#[inline(always)]
fn find_input(&self, node: &Node<'_>, b: u8) -> Option<usize> {
if node.version >= 2 && node.ntrans > TRANS_INDEX_THRESHOLD {
let start = node.start
Expand All @@ -712,7 +685,6 @@ impl StateAnyTrans {
}
}

#[inline(always)]
fn output(&self, node: &Node<'_>, i: usize) -> Output {
let osize = node.sizes.output_pack_size();
if osize == 0 {
Expand All @@ -736,39 +708,32 @@ impl StateAnyTrans {
struct PackSizes(u8);

impl PackSizes {
#[inline(always)]
fn new() -> Self {
PackSizes(0)
}

#[inline(always)]
fn decode(v: u8) -> Self {
PackSizes(v)
}

#[inline(always)]
fn encode(&self) -> u8 {
self.0
}

#[inline(always)]
fn set_transition_pack_size(&mut self, size: u8) {
assert!(size <= 8);
self.0 = (self.0 & 0b0000_1111) | (size << 4);
}

#[inline(always)]
fn transition_pack_size(&self) -> usize {
((self.0 & 0b1111_0000) >> 4) as usize
}

#[inline(always)]
fn set_output_pack_size(&mut self, size: u8) {
assert!(size <= 8);
self.0 = (self.0 & 0b1111_0000) | size;
}

#[inline(always)]
fn output_pack_size(&self) -> usize {
(self.0 & 0b0000_1111) as usize
}
Expand Down Expand Up @@ -802,7 +767,6 @@ impl<'f, 'n> Iterator for Transitions<'f, 'n> {
///
/// Nevertheless, the *caller* may have a priori knowledge that could be
/// supplied to the builder manually, which could then be embedded in the FST.
#[inline(always)]
fn common_idx(input: u8, max: u8) -> u8 {
let val = ((COMMON_INPUTS[input as usize] as u32 + 1) % 256) as u8;
if val > max {
Expand All @@ -814,7 +778,6 @@ fn common_idx(input: u8, max: u8) -> u8 {

/// common_input translates a common input index stored in a serialized FST
/// to the corresponding byte.
#[inline(always)]
fn common_input(idx: u8) -> Option<u8> {
if idx == 0 {
None
Expand Down

0 comments on commit 8bf84bc

Please sign in to comment.