perf: inline public getters/setters, and use non-generic inner functions

To reduce compile-times and avoid some overhead to binary size, this will modify some of our
generic functions to use non-generic inner functions where possible. The inner functions are
marked carefully with `#[inline(never)]` to prevent being inlined by LLVM at their callsites

While looking for generic functions to optimize, I have also taken the opportunity to annotate
public non-generic getters and setters with `#[inline]` to ensure that LLVM will inline them
across crate boundaries. By default, only generic functions are automatically inlined, and
only when enabling fat LTO are constant functions reliably inlined across crate boundaries.
This commit is contained in:
Michael Aaron Murphy 2025-03-21 03:17:59 +01:00
parent c538d672df
commit 8cf372c9b9
No known key found for this signature in database
GPG key ID: B2732D4240C9212C
55 changed files with 702 additions and 255 deletions

View file

@ -11,6 +11,7 @@ use super::{to_rgba, OutputError};
impl Theme {
#[must_use]
#[cold]
/// turn the theme into css
pub fn as_gtk4(&self) -> String {
let Self {
@ -145,6 +146,7 @@ impl Theme {
/// # Errors
///
/// Returns an `OutputError` if there is an error writing the CSS file.
#[cold]
pub fn write_gtk4(&self) -> Result<(), OutputError> {
let css_str = self.as_gtk4();
let Some(config_dir) = dirs::config_dir() else {
@ -174,6 +176,7 @@ impl Theme {
/// # Errors
///
/// Returns an `OutputError` if there is an error applying the CSS file.
#[cold]
pub fn apply_gtk(is_dark: bool) -> Result<(), OutputError> {
let Some(config_dir) = dirs::config_dir() else {
return Err(OutputError::MissingConfigDir);
@ -213,6 +216,7 @@ impl Theme {
/// # Errors
///
/// Returns an `OutputError` if there is an error resetting the CSS file.
#[cold]
pub fn reset_gtk() -> Result<(), OutputError> {
let Some(config_dir) = dirs::config_dir() else {
return Err(OutputError::MissingConfigDir);
@ -229,6 +233,7 @@ impl Theme {
res
}
#[cold]
fn backup_non_cosmic_css(path: &Path, cosmic_css: &Path) -> io::Result<()> {
if !Self::is_cosmic_css(path, cosmic_css)?.unwrap_or(true) {
let backup_path = path.with_extension("css.bak");
@ -237,6 +242,7 @@ impl Theme {
Ok(())
}
#[cold]
fn reset_cosmic_css(path: &Path, cosmic_css: &Path) -> io::Result<()> {
if Self::is_cosmic_css(path, cosmic_css)?.unwrap_or_default() {
fs::remove_file(path)?;

View file

@ -19,6 +19,7 @@ pub enum OutputError {
}
impl Theme {
#[inline]
pub fn apply_exports(&self) -> Result<(), OutputError> {
let gtk_res = Theme::apply_gtk(self.is_dark);
let vs_res = self.clone().apply_vs_code();
@ -27,12 +28,14 @@ impl Theme {
Ok(())
}
#[inline]
pub fn write_exports(&self) -> Result<(), OutputError> {
let gtk_res = self.write_gtk4();
gtk_res?;
Ok(())
}
#[inline]
pub fn reset_exports() -> Result<(), OutputError> {
let gtk_res = Theme::reset_gtk();
let vs_res = Theme::reset_vs_code();

View file

@ -266,6 +266,7 @@ impl From<Theme> for VsTheme {
}
impl Theme {
#[cold]
pub fn apply_vs_code(self) -> Result<(), OutputError> {
let vs_theme = VsTheme::from(self);
let config_dir = dirs::config_dir().ok_or(OutputError::MissingConfigDir)?;
@ -289,6 +290,7 @@ impl Theme {
Ok(())
}
#[cold]
pub fn reset_vs_code() -> Result<(), OutputError> {
let config_dir = dirs::config_dir().ok_or(OutputError::MissingConfigDir)?;
let vs_code_dir = config_dir.join("Code").join("User");