Store element arena on App and route element allocations through draw scope

This commit is contained in:
Nathan Sobo
2025-12-14 14:28:10 -07:00
parent 99eb28785f
commit 3d387f4bcf
2 changed files with 13 additions and 10 deletions

View File

@@ -34,11 +34,11 @@ use util::{ResultExt, debug_panic};
#[cfg(any(feature = "inspector", debug_assertions))]
use crate::InspectorElementRegistry;
use crate::{
Action, ActionBuildError, ActionRegistry, Any, AnyView, AnyWindowHandle, AppContext, Asset,
AssetSource, BackgroundExecutor, Bounds, ClipboardItem, CursorStyle, DispatchPhase, DisplayId,
EventEmitter, FocusHandle, FocusMap, ForegroundExecutor, Global, KeyBinding, KeyContext,
Keymap, Keystroke, LayoutId, Menu, MenuItem, OwnedMenu, PathPromptOptions, Pixels, Platform,
PlatformDisplay, PlatformKeyboardLayout, PlatformKeyboardMapper, Point, Priority,
Action, ActionBuildError, ActionRegistry, Any, AnyView, AnyWindowHandle, AppContext, Arena,
Asset, AssetSource, BackgroundExecutor, Bounds, ClipboardItem, CursorStyle, DispatchPhase,
DisplayId, EventEmitter, FocusHandle, FocusMap, ForegroundExecutor, Global, KeyBinding,
KeyContext, Keymap, Keystroke, LayoutId, Menu, MenuItem, OwnedMenu, PathPromptOptions, Pixels,
Platform, PlatformDisplay, PlatformKeyboardLayout, PlatformKeyboardMapper, Point, Priority,
PromptBuilder, PromptButton, PromptHandle, PromptLevel, Render, RenderImage,
RenderablePromptHandle, Reservation, ScreenCaptureSource, SharedString, SubscriberSet,
Subscription, SvgRenderer, Task, TextSystem, Window, WindowAppearance, WindowHandle, WindowId,
@@ -637,6 +637,9 @@ pub struct App {
pub(crate) name: Option<&'static str>,
quit_mode: QuitMode,
quitting: bool,
/// Per-App element arena. This isolates element allocations between different
/// App instances (important for tests where multiple Apps run concurrently).
pub(crate) element_arena: RefCell<Arena>,
}
impl App {
@@ -713,6 +716,7 @@ impl App {
#[cfg(any(test, feature = "test-support", debug_assertions))]
name: None,
element_arena: RefCell::new(Arena::new(1024 * 1024)),
}),
});

View File

@@ -32,9 +32,9 @@
//! your own custom layout algorithm or rendering a code editor.
use crate::{
App, ArenaBox, AvailableSpace, Bounds, Context, DispatchNodeId, ELEMENT_ARENA, ElementId,
FocusHandle, InspectorElementId, LayoutId, Pixels, Point, Size, Style, Window,
util::FluentBuilder,
App, ArenaBox, AvailableSpace, Bounds, Context, DispatchNodeId, ElementId, FocusHandle,
InspectorElementId, LayoutId, Pixels, Point, Size, Style, Window, util::FluentBuilder,
window::with_element_arena,
};
use derive_more::{Deref, DerefMut};
use std::{
@@ -579,8 +579,7 @@ impl AnyElement {
E: 'static + Element,
E::RequestLayoutState: Any,
{
let element = ELEMENT_ARENA
.with_borrow_mut(|arena| arena.alloc(|| Drawable::new(element)))
let element = with_element_arena(|arena| arena.alloc(|| Drawable::new(element)))
.map(|element| element as &mut dyn ElementObject);
AnyElement(element)
}