Skip to content

Commit 9fa4d45

Browse files
committed
Speed up Status parsing in release mode
1 parent e10f722 commit 9fa4d45

File tree

2 files changed

+21
-23
lines changed

2 files changed

+21
-23
lines changed

src/error.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,6 @@ pub enum PtErrorCode {
8080
BadFile = pt_error_code_pte_bad_file as i32,
8181
/// Unknown cpu
8282
BadCpu = pt_error_code_pte_bad_cpu as i32,
83-
84-
/// No Error Information available
85-
NoInfo = -1,
8683
}
8784

8885
#[derive(Debug, Clone, Copy)]
@@ -162,14 +159,23 @@ pub(crate) fn ensure_ptok(code: i32) -> Result<(), PtError> {
162159
/// Returns the code as an unsigned int
163160
#[inline]
164161
pub(crate) fn extract_pterr(code: i32) -> Result<u32, PtError> {
165-
match code {
166-
x if x >= 0 => Ok(code as u32),
167-
_ => Err(PtError::from_code(code)),
162+
if code >= 0 {
163+
Ok(code as u32)
164+
} else {
165+
Err(PtError::from_code(code))
168166
}
169167
}
170168

171169
/// Turns a negative code into a PtErr and a positive code into a Status
172170
#[inline]
173171
pub(crate) fn extract_status_or_pterr(code: i32) -> Result<Status, PtError> {
174-
extract_pterr(code).map(Status::from_bits_or_pterror)?
172+
let raw_status = extract_pterr(code)?;
173+
if cfg!(debug_assertions) {
174+
Status::from_bits(raw_status).ok_or(PtError::new(
175+
PtErrorCode::Internal,
176+
"Unknown state returned by libipt, failed to convert to Status",
177+
))
178+
} else {
179+
Ok(Status::from_bits_retain(raw_status))
180+
}
175181
}

src/status.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Certain casts are required only on Windows. Inform Clippy to ignore them.
22
#![allow(clippy::unnecessary_cast)]
33

4-
use crate::error::{PtError, PtErrorCode};
54
use bitflags::bitflags;
65
use libipt_sys::{
76
pt_status_flag_pts_eos, pt_status_flag_pts_event_pending, pt_status_flag_pts_ip_suppressed,
@@ -11,29 +10,16 @@ bitflags! {
1110
/// Status flags for various IntelPT actions
1211
#[derive(Debug, Clone, Copy)]
1312
pub struct Status: u32 {
14-
/// There is no more trace data available.
15-
const EOS = pt_status_flag_pts_eos as u32;
1613
/// There is an event pending.
1714
const EVENT_PENDING = pt_status_flag_pts_event_pending as u32;
1815
/// The address has been suppressed.
1916
const IP_SUPRESSED = pt_status_flag_pts_ip_suppressed as u32;
17+
/// There is no more trace data available.
18+
const EOS = pt_status_flag_pts_eos as u32;
2019
}
2120
}
2221

2322
impl Status {
24-
pub(crate) fn from_bits_or_pterror(pt_status: u32) -> Result<Self, PtError> {
25-
Status::from_bits(pt_status).ok_or(PtError::new(
26-
PtErrorCode::Internal,
27-
"Unknown state returned by libipt, failed to convert to Status",
28-
))
29-
}
30-
31-
/// There is no more trace data available.
32-
#[must_use]
33-
pub const fn eos(&self) -> bool {
34-
self.contains(Status::EOS)
35-
}
36-
3723
/// There is an event pending.
3824
#[must_use]
3925
pub const fn event_pending(&self) -> bool {
@@ -45,4 +31,10 @@ impl Status {
4531
pub const fn ip_supressed(&self) -> bool {
4632
self.contains(Status::IP_SUPRESSED)
4733
}
34+
35+
/// There is no more trace data available.
36+
#[must_use]
37+
pub const fn eos(&self) -> bool {
38+
self.contains(Status::EOS)
39+
}
4840
}

0 commit comments

Comments
 (0)