Reduce panel allocations and fix transient surface leaks (#19)
Repeated panel updates rebuilt unchanged controls and SVG artwork, cloned composition data, and kept unused workspace icon resources. This reuses controls when their layout stays the same, bounds the SVG cache, removes temporary layout allocations, and releases icon resources when apps close or the workspace style changes. Rapidly closing a modal could also leave a native surface behind when its asynchronous creation finished after the close. Late openings now get destroyed when their owner is gone. Ferese uses the published `ferese-v0.1.2` Iced/libcosmic fixes so a temporary software buffer/viewport size mismatch retries the frame instead of crashing. Two runs per version of the same workspace/overview/reload workload measured **26% fewer allocated bytes and 23% fewer allocation calls**. A separate 1,800-cycle notification/modal stress run retained about **40 MiB less shell memory**, including shared-buffer retention falling from 28.2 MiB to 272 KiB. These are measured workload results, not an overall desktop speedup claim. Validation: - 1,350 workspace tests passed, including GPU Settings text rendering (50 optional tests ignored); formatting and Clippy passed. - Native continuous/island panel tests passed rapid notification resizing, modal cancellation, and live material/input-region changes. - A Wayland lifetime trace confirmed all 280 created modal surfaces were destroyed; longer DHAT traces and Intel GPU memory measurements were checked separately. - All installation binaries rebuilt and the installation bundle passed inventory and checksum verification.
This commit is contained in:
1 parent
65fcaef13f
commit
cf540f1feb
14 files changed
+403
-74
No files matched your search
@@ -29,30 +29,34 @@ pub struct Resolution {
|
||||
}
|
||||
|
||||
fn zone_width(panel: &Panel, zone: &Zone, items: &BTreeMap<ItemId, Placement>) -> f32 {
|
||||
let mut groups = Vec::new();
|
||||
let mut width = 0.0;
|
||||
let mut group_count: usize = 0;
|
||||
for group in &zone.groups {
|
||||
let mut widths = Vec::new();
|
||||
let mut content_width = 0.0;
|
||||
let mut occupied = false;
|
||||
for item in &group.items {
|
||||
if let Some(Placement::Visible { width, .. }) = items.get(&item.id)
|
||||
&& *width > 0.0
|
||||
{
|
||||
if !widths.is_empty() {
|
||||
widths.push(item.gap_before.unwrap_or(group.spacing));
|
||||
if occupied {
|
||||
content_width += item.gap_before.unwrap_or(group.spacing);
|
||||
}
|
||||
widths.push(*width);
|
||||
content_width += *width;
|
||||
occupied = true;
|
||||
}
|
||||
}
|
||||
if !widths.is_empty() {
|
||||
if occupied {
|
||||
let padding = 2.0 * f32::from(group.padding[1]);
|
||||
let island = if panel.resolved_surface(group) == super::GroupSurface::Island {
|
||||
2.0 * group.island_padding
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
groups.push(widths.iter().sum::<f32>() + padding + island);
|
||||
width += content_width + padding + island;
|
||||
group_count += 1;
|
||||
}
|
||||
}
|
||||
groups.iter().sum::<f32>() + zone.spacing * groups.len().saturating_sub(1) as f32
|
||||
width + zone.spacing * group_count.saturating_sub(1) as f32
|
||||
}
|
||||
|
||||
impl Resolution {
|
||||
@@ -271,19 +275,14 @@ pub fn resolve_with_gap(
|
||||
let mut high = maximum;
|
||||
for _ in 0..20 {
|
||||
let width = (low + high) * 0.5;
|
||||
result
|
||||
.items
|
||||
.insert(measured.id.clone(), Placement::Visible { representation, width });
|
||||
*result.items.get_mut(&measured.id).unwrap() = Placement::Visible { representation, width };
|
||||
result.update_widths(panel, overflow_width);
|
||||
if fits(&result) { low = width } else { high = width }
|
||||
}
|
||||
result.items.insert(
|
||||
measured.id.clone(),
|
||||
Placement::Visible {
|
||||
representation,
|
||||
width: low,
|
||||
},
|
||||
);
|
||||
*result.items.get_mut(&measured.id).unwrap() = Placement::Visible {
|
||||
representation,
|
||||
width: low,
|
||||
};
|
||||
}
|
||||
result
|
||||
.overflow
|
||||
@@ -296,6 +295,55 @@ pub fn resolve_with_gap(
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn zone_width_ignores_empty_groups_and_gaps_before_hidden_items() {
|
||||
use super::super::{Group, GroupSurface, Item, ItemKind};
|
||||
let mut panel = Panel::default();
|
||||
let mut first = Item::new("first", ItemKind::Overview);
|
||||
first.gap_before = Some(99.);
|
||||
let hidden = Item::new("hidden", ItemKind::Network);
|
||||
let mut last = Item::new("last", ItemKind::Clock);
|
||||
last.gap_before = Some(7.);
|
||||
let mut group = Group::new("content", vec![first, hidden, last], [0, 3], 5.);
|
||||
group.surface = Some(GroupSurface::Island);
|
||||
group.island_padding = 4.;
|
||||
let empty = Group::new("empty", vec![Item::new("missing", ItemKind::Media)], [0, 99], 99.);
|
||||
let trailing = Group::new(
|
||||
"trailing",
|
||||
vec![Item::new("trailing", ItemKind::Battery { percentage: false })],
|
||||
[0, 2],
|
||||
5.,
|
||||
);
|
||||
panel.start.groups = vec![empty.clone(), group, empty, trailing];
|
||||
panel.start.spacing = 11.;
|
||||
let items = [
|
||||
(
|
||||
ItemId("first".into()),
|
||||
Placement::Visible {
|
||||
representation: Representation::Icon,
|
||||
width: 20.,
|
||||
},
|
||||
),
|
||||
(ItemId("hidden".into()), Placement::Overflow),
|
||||
(
|
||||
ItemId("last".into()),
|
||||
Placement::Visible {
|
||||
representation: Representation::Wide,
|
||||
width: 30.,
|
||||
},
|
||||
),
|
||||
(
|
||||
ItemId("trailing".into()),
|
||||
Placement::Visible {
|
||||
representation: Representation::Icon,
|
||||
width: 10.,
|
||||
},
|
||||
),
|
||||
]
|
||||
.into();
|
||||
assert_eq!(zone_width(&panel, &panel.start, &items), 96.);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_center_only_item_keeps_its_preferred_size_without_side_gaps() {
|
||||
use super::super::{Group, Item, ItemKind};
|
||||
|
||||
@@ -11,7 +11,7 @@ ferese-theme-model = { path = "../ferese-theme-model" }
|
||||
ferese-config = { path = "../ferese-config" }
|
||||
libcosmic = { workspace = true, features = ["a11y"] }
|
||||
|
||||
iced_accessibility = { git = "https://github.com/ferese-wm/libcosmic", tag = "ferese-v0.1.1" }
|
||||
iced_accessibility = { git = "https://github.com/ferese-wm/libcosmic", tag = "ferese-v0.1.2" }
|
||||
|
||||
[dev-dependencies]
|
||||
tokio = { version = "1", features = ["rt"] }
|
||||
@@ -76,6 +76,69 @@ pub fn symbolic(source: &'static [u8], size: u16) -> icon::Icon {
|
||||
icon::from_svg_bytes(source).symbolic(true).icon().size(size)
|
||||
}
|
||||
|
||||
const TINT_CACHE_ENTRIES: usize = 64;
|
||||
const TINT_CACHE_BYTES: usize = 128 * 1024;
|
||||
|
||||
struct TintedIcon {
|
||||
source: &'static [u8],
|
||||
colors: [u32; 8],
|
||||
handle: icon::Handle,
|
||||
bytes: usize,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct TintCache {
|
||||
entries: std::collections::VecDeque<TintedIcon>,
|
||||
bytes: usize,
|
||||
}
|
||||
|
||||
impl TintCache {
|
||||
fn get(&mut self, source: &'static [u8], foreground: Color, accent: Color) -> icon::Handle {
|
||||
let colors = [
|
||||
foreground.r,
|
||||
foreground.g,
|
||||
foreground.b,
|
||||
foreground.a,
|
||||
accent.r,
|
||||
accent.g,
|
||||
accent.b,
|
||||
accent.a,
|
||||
]
|
||||
.map(f32::to_bits);
|
||||
if let Some(index) = self
|
||||
.entries
|
||||
.iter()
|
||||
.position(|entry| std::ptr::eq(entry.source, source) && entry.colors == colors)
|
||||
{
|
||||
let entry = self.entries.remove(index).unwrap();
|
||||
let handle = entry.handle.clone();
|
||||
self.entries.push_back(entry);
|
||||
return handle;
|
||||
}
|
||||
let svg = tint_svg(source, foreground, accent);
|
||||
let bytes = svg.capacity();
|
||||
let handle = icon::from_svg_bytes(svg).symbolic(false);
|
||||
if bytes <= TINT_CACHE_BYTES {
|
||||
while self.entries.len() >= TINT_CACHE_ENTRIES || self.bytes + bytes > TINT_CACHE_BYTES {
|
||||
self.bytes -= self.entries.pop_front().unwrap().bytes;
|
||||
}
|
||||
self.bytes += bytes;
|
||||
self.entries.push_back(TintedIcon {
|
||||
source,
|
||||
colors,
|
||||
handle: handle.clone(),
|
||||
bytes,
|
||||
});
|
||||
}
|
||||
handle
|
||||
}
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
// Retain shared artwork across view rebuilds, with bounded storage as colors change.
|
||||
static TINT_CACHE: std::cell::RefCell<TintCache> = std::cell::RefCell::new(TintCache::default());
|
||||
}
|
||||
|
||||
pub fn accented(source: &'static [u8], size: u16, foreground: Color, accent: Color) -> icon::Icon {
|
||||
let svg = std::str::from_utf8(source).expect("embedded SVG must be UTF-8");
|
||||
// Battery canvases are wider; reserve that space instead of stretching
|
||||
@@ -85,8 +148,8 @@ pub fn accented(source: &'static [u8], size: u16, foreground: Color, accent: Col
|
||||
} else {
|
||||
1.0
|
||||
};
|
||||
icon::from_svg_bytes(tint_svg(source, foreground, accent))
|
||||
.symbolic(false)
|
||||
TINT_CACHE
|
||||
.with(|cache| cache.borrow_mut().get(source, foreground, accent))
|
||||
.icon()
|
||||
.size(size)
|
||||
.width(Length::Fixed(f32::from(size) * aspect))
|
||||
@@ -131,6 +194,45 @@ pub fn outline(path: &str, tint: Color, size: u16) -> icon::Icon {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use cosmic::iced::Color;
|
||||
#[test]
|
||||
fn tinted_icons_share_artwork_and_distinguish_accent_and_alpha() {
|
||||
let mut cache = super::TintCache::default();
|
||||
let foreground = Color::from_rgb8(205, 214, 244);
|
||||
let accent = Color::from_rgb8(203, 166, 247);
|
||||
let bytes = |handle: &cosmic::widget::icon::Handle| {
|
||||
let cosmic::widget::icon::Data::Svg(svg) = &handle.data else {
|
||||
panic!("expected SVG");
|
||||
};
|
||||
let cosmic::iced::advanced::svg::Data::Bytes(bytes) = svg.data() else {
|
||||
panic!("expected SVG bytes");
|
||||
};
|
||||
bytes.as_ptr()
|
||||
};
|
||||
let first = cache.get(super::BATTERY_50, foreground, accent);
|
||||
let second = cache.get(super::BATTERY_50, foreground, accent);
|
||||
assert_eq!(bytes(&first), bytes(&second), "rebuilds must reuse the same allocation");
|
||||
let changed_accent = cache.get(super::BATTERY_50, foreground, Color::from_rgb8(42, 90, 180));
|
||||
assert_ne!(bytes(&first), bytes(&changed_accent));
|
||||
let translucent = cache.get(super::BATTERY_50, Color { a: 0.5, ..foreground }, accent);
|
||||
assert_ne!(bytes(&first), bytes(&translucent));
|
||||
assert_eq!(cache.entries.len(), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn icon_tint_cache_evicts_old_colors_without_growing() {
|
||||
let mut cache = super::TintCache::default();
|
||||
for value in 0..512 {
|
||||
let _ = cache.get(
|
||||
super::FERESE,
|
||||
Color::from_rgba(0.5, 0.5, 0.5, value as f32 / 512.),
|
||||
Color::BLACK,
|
||||
);
|
||||
assert!(cache.entries.len() <= super::TINT_CACHE_ENTRIES);
|
||||
assert!(cache.bytes <= super::TINT_CACHE_BYTES);
|
||||
}
|
||||
assert_eq!(cache.entries.back().unwrap().colors[3], (511_f32 / 512.).to_bits());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn icons_use_theme_accent_and_preserve_battery_status_colors() {
|
||||
for (source, accent) in [
|
||||
|
||||
@@ -139,16 +139,19 @@ impl<M> Widget<M, Theme, cosmic::Renderer> for Adaptive<'_, M> {
|
||||
.find(|measurement| measurement.id.0 == "_overflow")
|
||||
.map_or(0.0, |measurement| measurement.alternatives[0].1);
|
||||
let trigger = trigger.min((limits.max().width - self.overflow_width).max(0.0));
|
||||
self.resolution = resolve_with_gap(
|
||||
let mut resolution = resolve_with_gap(
|
||||
&self.panel,
|
||||
&measurements,
|
||||
limits.max().width,
|
||||
(self.overflow_width + trigger).min(limits.max().width),
|
||||
self.zone_gap,
|
||||
);
|
||||
self.resolution.overflow_trigger_width = trigger;
|
||||
self.content = (self.build)(&self.resolution);
|
||||
tree.children[0].diff(self.content.as_widget_mut());
|
||||
resolution.overflow_trigger_width = trigger;
|
||||
if self.resolution != resolution {
|
||||
self.resolution = resolution;
|
||||
self.content = (self.build)(&self.resolution);
|
||||
tree.children[0].diff(self.content.as_widget_mut());
|
||||
}
|
||||
self.content
|
||||
.as_widget_mut()
|
||||
.layout(&mut tree.children[0], renderer, limits)
|
||||
@@ -383,12 +386,15 @@ mod tests {
|
||||
}))
|
||||
.collect();
|
||||
let id = id.clone();
|
||||
let builds = std::cell::Cell::new(0);
|
||||
let build_count = &builds;
|
||||
let mut view = frame(
|
||||
Cow::Borrowed(&panel),
|
||||
samples,
|
||||
0.,
|
||||
Resolution::default(),
|
||||
move |resolution| {
|
||||
build_count.set(build_count.get() + 1);
|
||||
let width = match resolution.items.get(&id) {
|
||||
Some(Placement::Visible { width, .. }) => *width,
|
||||
_ => resolution.overflow_trigger_width,
|
||||
@@ -406,6 +412,10 @@ mod tests {
|
||||
let viewport = Rectangle::new(Point::ORIGIN, Size::new(width, 28.));
|
||||
let limits = layout::Limits::new(Size::ZERO, viewport.size());
|
||||
let node = view.as_widget_mut().layout(tree, &renderer, &limits);
|
||||
let built = builds.get();
|
||||
assert_eq!(built, 2, "initial measurement must replace the unallocated content");
|
||||
view.as_widget_mut().layout(tree, &renderer, &limits);
|
||||
assert_eq!(builds.get(), built, "unchanged allocation must reuse its controls");
|
||||
let mut messages = Vec::new();
|
||||
for _ in 0..4 {
|
||||
view.as_widget_mut().update(
|
||||
|
||||
Reference in new issue
Block a user