Skip to content

Commit

Permalink
perf: inline performance-critical operations
Browse files Browse the repository at this point in the history
* Standardize inlining of simple, frequently-used operations
* Focus on performance-critical audio processing paths
* Inline UUID operations (deref, display, conversion, generation)
* Inline numeric type conversions in util module
  • Loading branch information
roderickvd committed Jan 11, 2025
1 parent 0f4b2ac commit 3785cf7
Show file tree
Hide file tree
Showing 25 changed files with 146 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/).
### Changed
- [decrypt] Add explicit `BufRead` implementation to standardize buffering behavior
- [decrypt] Inline performance-critical buffer operations
- [docs] Remove incorrect mention of "Hi-Res" audio quality

## [0.8.1] - 2025-01-11

Expand Down
3 changes: 3 additions & 0 deletions src/arl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ impl Arl {
/// assert!(Arl::new("spaces not allowed".to_string()).is_err());
/// assert!(Arl::new("控制字符".to_string()).is_err());
/// ```
#[inline]
pub fn new(arl: String) -> Result<Self> {
Ok(Self(arl))
}
Expand All @@ -110,6 +111,7 @@ impl Deref for Arl {
/// validation invariants.
type Target = String;

#[inline]
fn deref(&self) -> &Self::Target {
&self.0
}
Expand All @@ -135,6 +137,7 @@ impl Deref for Arl {
/// println!("{:?}", arl); // Prints: Arl("REDACTED")
/// ```
impl fmt::Display for Arl {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
Expand Down
7 changes: 7 additions & 0 deletions src/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ impl Gateway {
///
/// The license token is required for media access.
#[must_use]
#[inline]
pub fn license_token(&self) -> Option<&str> {
self.user_data
.as_ref()
Expand All @@ -374,6 +375,7 @@ impl Gateway {
/// * No user data is available
/// * Current time is past expiration time
#[must_use]
#[inline]
pub fn is_expired(&self) -> bool {
self.expires_at() <= SystemTime::now()
}
Expand All @@ -382,6 +384,7 @@ impl Gateway {
///
/// Returns UNIX epoch if no session is active.
#[must_use]
#[inline]
pub fn expires_at(&self) -> SystemTime {
if let Some(data) = &self.user_data {
return data.user.options.expiration_timestamp;
Expand All @@ -391,12 +394,14 @@ impl Gateway {
}

/// Updates the cached user data.
#[inline]
pub fn set_user_data(&mut self, data: UserData) {
self.user_data = Some(data);
}

/// Returns a reference to the current user data if available.
#[must_use]
#[inline]
pub fn user_data(&self) -> Option<&UserData> {
self.user_data.as_ref()
}
Expand Down Expand Up @@ -430,6 +435,7 @@ impl Gateway {

/// Returns the user's display name if available.
#[must_use]
#[inline]
pub fn user_name(&self) -> Option<&str> {
self.user_data.as_ref().map(|data| data.user.name.as_str())
}
Expand Down Expand Up @@ -601,6 +607,7 @@ impl Gateway {
///
/// Forces a refresh on next token request while preserving
/// other API functionality.
#[inline]
pub fn flush_user_token(&mut self) {
// Force refreshing user data, but do not set `user_data` to `None` so
// so we can continue using the `api_token` it contains.
Expand Down
3 changes: 3 additions & 0 deletions src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ impl Client {
/// );
/// let response = client.execute(request).await?;
/// ```
#[inline]
pub fn request<U, T>(&self, method: Method, url: U, body: T) -> reqwest::Request
where
U: Into<Url>,
Expand All @@ -225,6 +226,7 @@ impl Client {
///
/// * `url` - Request URL
/// * `body` - Request body content
#[inline]
pub fn post<U, T>(&self, url: U, body: T) -> reqwest::Request
where
U: Into<Url>,
Expand All @@ -241,6 +243,7 @@ impl Client {
///
/// * `url` - Request URL
/// * `body` - Request body content (usually empty)
#[inline]
pub fn get<U, T>(&self, url: U, body: T) -> reqwest::Request
where
U: Into<Url>,
Expand Down
16 changes: 16 additions & 0 deletions src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -990,12 +990,14 @@ impl Player {

/// Returns the currently playing track, if any.
#[must_use]
#[inline]
pub fn track(&self) -> Option<&Track> {
self.queue.get(self.position)
}

/// Returns a mutable reference to the currently playing track, if any.
#[must_use]
#[inline]
pub fn track_mut(&mut self) -> Option<&mut Track> {
self.queue.get_mut(self.position)
}
Expand All @@ -1015,13 +1017,15 @@ impl Player {

/// Returns a reference to the next track in the queue, if any.
#[must_use]
#[inline]
pub fn next_track(&self) -> Option<&Track> {
let next = self.position.saturating_add(1);
self.queue.get(next)
}

/// Returns a mutable reference to the next track in the queue, if any.
#[must_use]
#[inline]
pub fn next_track_mut(&mut self) -> Option<&mut Track> {
let next = self.position.saturating_add(1);
self.queue.get_mut(next)
Expand Down Expand Up @@ -1154,6 +1158,7 @@ impl Player {

/// Returns the current repeat mode.
#[must_use]
#[inline]
pub fn repeat_mode(&self) -> RepeatMode {
self.repeat_mode
}
Expand Down Expand Up @@ -1186,6 +1191,7 @@ impl Player {
///
/// Note: This returns the stored volume setting even if the audio device is closed.
#[must_use]
#[inline]
pub fn volume(&self) -> Percentage {
self.volume
}
Expand Down Expand Up @@ -1425,16 +1431,19 @@ impl Player {

/// Returns current position in the queue.
#[must_use]
#[inline]
pub fn position(&self) -> usize {
self.position
}

/// Sets the license token for media access.
#[inline]
pub fn set_license_token(&mut self, license_token: impl Into<String>) {
self.license_token = license_token.into();
}

/// Enables or disables volume normalization.
#[inline]
pub fn set_normalization(&mut self, normalization: bool) {
self.normalization = normalization;
}
Expand All @@ -1457,35 +1466,41 @@ impl Player {
///
/// Note: Actual quality may be lower if track is not
/// available in requested quality.
#[inline]
pub fn set_audio_quality(&mut self, quality: AudioQuality) {
self.audio_quality = quality;
}

/// Returns whether volume normalization is enabled.
#[must_use]
#[inline]
pub fn normalization(&self) -> bool {
self.normalization
}

/// Returns current license token.
#[must_use]
#[inline]
pub fn license_token(&self) -> &str {
&self.license_token
}

/// Returns current preferred audio quality setting.
#[must_use]
#[inline]
pub fn audio_quality(&self) -> AudioQuality {
self.audio_quality
}

/// Returns current normalization target gain.
#[must_use]
#[inline]
pub fn gain_target_db(&self) -> i8 {
self.gain_target_db
}

/// Sets the media content URL.
#[inline]
pub fn set_media_url(&mut self, url: Url) {
self.media_url = url;
}
Expand All @@ -1507,6 +1522,7 @@ impl Player {
/// assert!(!player.is_started());
/// ```
#[must_use]
#[inline]
pub fn is_started(&self) -> bool {
self.sink.is_some()
}
Expand Down
1 change: 1 addition & 0 deletions src/protocol/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ impl Codec {
/// * "flac"
/// * "mp3"
impl fmt::Display for Codec {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Codec::AAC => write!(f, "aac"),
Expand Down
2 changes: 2 additions & 0 deletions src/protocol/connect/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,7 @@ impl FromStr for Channel {
/// assert_eq!(unspec.to_string(), "-1");
/// ```
impl fmt::Display for UserId {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Id(id) => write!(f, "{id}"),
Expand Down Expand Up @@ -714,6 +715,7 @@ impl FromStr for UserId {
/// assert!(matches!(user, UserId::Id(_)));
/// ```
impl From<NonZeroU64> for UserId {
#[inline]
fn from(id: NonZeroU64) -> Self {
Self::Id(id)
}
Expand Down
8 changes: 8 additions & 0 deletions src/protocol/connect/contents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,7 @@ pub enum Status {
/// println!("Command completed with status: {}", Status::OK);
/// ```
impl fmt::Display for Status {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Status::OK => write!(f, "Ok"),
Expand Down Expand Up @@ -1386,6 +1387,7 @@ impl Percentage {
/// assert_eq!(p.as_percent_f32(), 75.0);
/// ```
#[must_use]
#[inline]
pub const fn from_ratio_f32(ratio: f32) -> Self {
Self(ratio as f64)
}
Expand All @@ -1406,6 +1408,7 @@ impl Percentage {
/// assert_eq!(p.as_percent_f64(), 50.0);
/// ```
#[must_use]
#[inline]
pub const fn from_ratio_f64(ratio: f64) -> Self {
Self(ratio)
}
Expand All @@ -1426,6 +1429,7 @@ impl Percentage {
/// assert_eq!(p.as_ratio_f32(), 0.75);
/// ```
#[must_use]
#[inline]
pub const fn from_percent_f32(percent: f32) -> Self {
Self(percent as f64 / 100.0)
}
Expand All @@ -1446,6 +1450,7 @@ impl Percentage {
/// assert_eq!(p.as_ratio_f64(), 0.75);
/// ```
#[must_use]
#[inline]
pub const fn from_percent_f64(percent: f64) -> Self {
Self(percent / 100.0)
}
Expand Down Expand Up @@ -1478,6 +1483,7 @@ impl Percentage {
/// assert_eq!(RATIO, 0.333);
/// ```
#[must_use]
#[inline]
pub const fn as_ratio_f64(&self) -> f64 {
self.0
}
Expand Down Expand Up @@ -1510,6 +1516,7 @@ impl Percentage {
/// assert_eq!(PERCENT, 33.3);
/// ```
#[must_use]
#[inline]
pub const fn as_percent_f64(&self) -> f64 {
self.0 * 100.0
}
Expand Down Expand Up @@ -2420,6 +2427,7 @@ pub enum DeviceType {
/// assert_eq!(DeviceType::Unknown.to_string(), "unknown");
/// ```
impl fmt::Display for DeviceType {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
DeviceType::Desktop => write!(f, "desktop"),
Expand Down
1 change: 1 addition & 0 deletions src/protocol/connect/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ enum Stanza {
/// assert_eq!(Stanza::Send.to_string(), "Send");
/// ```
impl fmt::Display for Stanza {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{self:?}")
}
Expand Down
3 changes: 3 additions & 0 deletions src/protocol/connect/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ impl Ident {
/// println!("{contents}");
/// ```
impl fmt::Display for Contents {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} {}", self.action, self.value.track_id)
}
Expand All @@ -399,6 +400,7 @@ impl fmt::Display for Contents {
/// assert_eq!(Action::Play.to_string(), "PLAY");
/// ```
impl fmt::Display for Action {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Play => write!(f, "{}", Self::PLAY),
Expand Down Expand Up @@ -445,6 +447,7 @@ impl FromStr for Action {
/// assert_eq!(Ident::Limitation.to_string(), "LIMITATION");
/// ```
impl fmt::Display for Ident {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Limitation => write!(f, "{}", Self::LIMITATION),
Expand Down
1 change: 1 addition & 0 deletions src/protocol/gateway/list_data/episodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ pub struct EpisodeData(pub ListData);
impl Deref for EpisodeData {
type Target = ListData;

#[inline]
fn deref(&self) -> &Self::Target {
&self.0
}
Expand Down
1 change: 1 addition & 0 deletions src/protocol/gateway/list_data/livestream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ pub struct LivestreamData(pub ListData);
impl Deref for LivestreamData {
type Target = ListData;

#[inline]
fn deref(&self) -> &Self::Target {
&self.0
}
Expand Down
Loading

0 comments on commit 3785cf7

Please sign in to comment.