Skip to content

Commit

Permalink
perf: set static mmap threshold on gnu target env by default
Browse files Browse the repository at this point in the history
  • Loading branch information
mmstick authored and jackpot51 committed Feb 12, 2025
1 parent 9426a98 commit f59eb77
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,11 @@ pub(crate) fn iced_settings<App: Application>(
///
/// Returns error on application failure.
pub fn run<App: Application>(settings: Settings, flags: App::Flags) -> iced::Result {
let default_font = settings.default_font;
#[cfg(target_env = "gnu")]
if let Some(threshold) = settings.default_mmap_threshold {
crate::malloc::limit_mmap_threshold(threshold);
}

let (settings, mut flags, window_settings) = iced_settings::<App>(settings, flags);
#[cfg(not(feature = "multi-window"))]
{
Expand Down
4 changes: 4 additions & 0 deletions src/app/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ pub struct Settings {
/// Default size of fonts.
pub(crate) default_text_size: f32,

/// Set the default mmap threshold for malloc with mallopt.
pub(crate) default_mmap_threshold: Option<i32>,

/// Whether the window should be resizable or not.
/// and the size of the window border which can be dragged for a resize
pub(crate) resizable: Option<f64>,
Expand Down Expand Up @@ -85,6 +88,7 @@ impl Default for Settings {
default_font: font::default(),
default_icon_theme: None,
default_text_size: 14.0,
default_mmap_threshold: Some(128 * 1024),
resizable: Some(8.0),
scale_factor: std::env::var("COSMIC_SCALE")
.ok()
Expand Down
8 changes: 5 additions & 3 deletions src/applet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ impl Context {
self.size = Size::Hardcoded((width, height));
}

#[must_use]
#[allow(clippy::cast_precision_loss)]
pub fn window_settings(&self) -> crate::app::Settings {
let (width, height) = self.suggested_size(true);
Expand Down Expand Up @@ -183,7 +182,6 @@ impl Context {
matches!(self.anchor, PanelAnchor::Top | PanelAnchor::Bottom)
}

#[must_use]
pub fn icon_button_from_handle<'a, Message: 'static>(
&self,
icon: widget::icon::Handle,
Expand Down Expand Up @@ -213,7 +211,6 @@ impl Context {
.class(Button::AppletIcon)
}

#[must_use]
pub fn icon_button<'a, Message: 'static>(
&self,
icon_name: &'a str,
Expand Down Expand Up @@ -385,6 +382,11 @@ pub fn run<App: Application>(flags: App::Flags) -> iced::Result {
let mut settings = helper.window_settings();
settings.resizable = None;

#[cfg(target_env = "gnu")]
if let Some(threshold) = settings.default_mmap_threshold {
crate::malloc::limit_mmap_threshold(threshold);
}

if let Some(icon_theme) = settings.default_icon_theme.clone() {
crate::icon_theme::set_default(icon_theme);
}
Expand Down
8 changes: 6 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ pub use cosmic_config;
#[doc(inline)]
pub use cosmic_theme;

#[cfg(feature = "desktop")]
pub mod desktop;

#[cfg(any(feature = "xdg-portal", feature = "rfd"))]
pub mod dialog;

Expand Down Expand Up @@ -73,8 +76,9 @@ pub use iced_wgpu;
pub mod icon_theme;
pub mod keyboard_nav;

#[cfg(feature = "desktop")]
pub mod desktop;
#[cfg(target_env = "gnu")]
pub(crate) mod malloc;

#[cfg(all(feature = "process", not(windows)))]
pub mod process;

Expand Down
14 changes: 14 additions & 0 deletions src/malloc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use std::os::raw::c_int;

const M_MMAP_THRESHOLD: c_int = -3;

extern "C" {
fn mallopt(param: c_int, value: c_int) -> c_int;
}

/// Prevents glibc from hoarding memory via memory fragmentation.
pub fn limit_mmap_threshold(threshold: i32) {
unsafe {
mallopt(M_MMAP_THRESHOLD, threshold as c_int);
}
}

0 comments on commit f59eb77

Please sign in to comment.