From 28ef7455f00554424f2acee0af00b58ac6695ffc Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Thu, 20 Nov 2025 20:54:47 +0100 Subject: [PATCH] gpui: #[inline] some trivial functions (#43189) These appear in a lot of stacktraces (especially on windows) despite them being plain forwarding calls. Also removes some intermediate calls within gpui that will only turn into more unnecessary compiler work. Release Notes: - N/A *or* Added/Fixed/Improved ... --- crates/gpui/src/app/async_context.rs | 43 ++++++++++++++++------------ crates/gpui/src/app/context.rs | 10 +++++++ crates/gpui/src/app/entity_map.rs | 34 ++++++++++++++++++++++ crates/text/src/text.rs | 21 +++++++++++++- 4 files changed, 88 insertions(+), 20 deletions(-) diff --git a/crates/gpui/src/app/async_context.rs b/crates/gpui/src/app/async_context.rs index 381541d4b11377b988dd30e03155855c7ba25aed..f5dcd30ae943954cbc042e1ce02edad39370a04a 100644 --- a/crates/gpui/src/app/async_context.rs +++ b/crates/gpui/src/app/async_context.rs @@ -296,8 +296,8 @@ impl AsyncWindowContext { /// A convenience method for [`Window::on_next_frame`]. pub fn on_next_frame(&mut self, f: impl FnOnce(&mut Window, &mut App) + 'static) { - self.window - .update(self, |_, window, _| window.on_next_frame(f)) + self.app + .update_window(self.window, |_, window, _| window.on_next_frame(f)) .ok(); } @@ -306,8 +306,8 @@ impl AsyncWindowContext { &mut self, read: impl FnOnce(&G, &Window, &App) -> R, ) -> Result { - self.window - .update(self, |_, window, cx| read(cx.global(), window, cx)) + self.app + .update_window(self.window, |_, window, cx| read(cx.global(), window, cx)) } /// A convenience method for [`App::update_global`](BorrowAppContext::update_global). @@ -319,7 +319,7 @@ impl AsyncWindowContext { where G: Global, { - self.window.update(self, |_, window, cx| { + self.app.update_window(self.window, |_, window, cx| { cx.update_global(|global, cx| update(global, window, cx)) }) } @@ -350,8 +350,8 @@ impl AsyncWindowContext { where T: Clone + Into, { - self.window - .update(self, |_, window, cx| { + self.app + .update_window(self.window, |_, window, cx| { window.prompt(level, message, detail, answers, cx) }) .unwrap_or_else(|_| oneshot::channel().1) @@ -365,11 +365,13 @@ impl AppContext for AsyncWindowContext { where T: 'static, { - self.window.update(self, |_, _, cx| cx.new(build_entity)) + self.app + .update_window(self.window, |_, _, cx| cx.new(build_entity)) } fn reserve_entity(&mut self) -> Result> { - self.window.update(self, |_, _, cx| cx.reserve_entity()) + self.app + .update_window(self.window, |_, _, cx| cx.reserve_entity()) } fn insert_entity( @@ -377,8 +379,9 @@ impl AppContext for AsyncWindowContext { reservation: Reservation, build_entity: impl FnOnce(&mut Context) -> T, ) -> Self::Result> { - self.window - .update(self, |_, _, cx| cx.insert_entity(reservation, build_entity)) + self.app.update_window(self.window, |_, _, cx| { + cx.insert_entity(reservation, build_entity) + }) } fn update_entity( @@ -386,8 +389,8 @@ impl AppContext for AsyncWindowContext { handle: &Entity, update: impl FnOnce(&mut T, &mut Context) -> R, ) -> Result { - self.window - .update(self, |_, _, cx| cx.update_entity(handle, update)) + self.app + .update_window(self.window, |_, _, cx| cx.update_entity(handle, update)) } fn as_mut<'a, T>(&'a mut self, _: &Entity) -> Self::Result> @@ -452,8 +455,9 @@ impl VisualContext for AsyncWindowContext { &mut self, build_entity: impl FnOnce(&mut Window, &mut Context) -> T, ) -> Self::Result> { - self.window - .update(self, |_, window, cx| cx.new(|cx| build_entity(window, cx))) + self.app.update_window(self.window, |_, window, cx| { + cx.new(|cx| build_entity(window, cx)) + }) } fn update_window_entity( @@ -461,7 +465,7 @@ impl VisualContext for AsyncWindowContext { view: &Entity, update: impl FnOnce(&mut T, &mut Window, &mut Context) -> R, ) -> Self::Result { - self.window.update(self, |_, window, cx| { + self.app.update_window(self.window, |_, window, cx| { view.update(cx, |entity, cx| update(entity, window, cx)) }) } @@ -473,15 +477,16 @@ impl VisualContext for AsyncWindowContext { where V: 'static + Render, { - self.window - .update(self, |_, window, cx| window.replace_root(cx, build_view)) + self.app.update_window(self.window, |_, window, cx| { + window.replace_root(cx, build_view) + }) } fn focus(&mut self, view: &Entity) -> Self::Result<()> where V: Focusable, { - self.window.update(self, |_, window, cx| { + self.app.update_window(self.window, |_, window, cx| { view.read(cx).focus_handle(cx).focus(window); }) } diff --git a/crates/gpui/src/app/context.rs b/crates/gpui/src/app/context.rs index 41d6cac82b7c179040d61ddfd22b003c143a5fb9..65bb5521e32bb6fcfac2bcd95009949499589df1 100644 --- a/crates/gpui/src/app/context.rs +++ b/crates/gpui/src/app/context.rs @@ -736,14 +736,17 @@ impl Context<'_, T> { impl AppContext for Context<'_, T> { type Result = U; + #[inline] fn new(&mut self, build_entity: impl FnOnce(&mut Context) -> U) -> Entity { self.app.new(build_entity) } + #[inline] fn reserve_entity(&mut self) -> Reservation { self.app.reserve_entity() } + #[inline] fn insert_entity( &mut self, reservation: Reservation, @@ -752,6 +755,7 @@ impl AppContext for Context<'_, T> { self.app.insert_entity(reservation, build_entity) } + #[inline] fn update_entity( &mut self, handle: &Entity, @@ -760,6 +764,7 @@ impl AppContext for Context<'_, T> { self.app.update_entity(handle, update) } + #[inline] fn as_mut<'a, E>(&'a mut self, handle: &Entity) -> Self::Result> where E: 'static, @@ -767,6 +772,7 @@ impl AppContext for Context<'_, T> { self.app.as_mut(handle) } + #[inline] fn read_entity( &self, handle: &Entity, @@ -778,6 +784,7 @@ impl AppContext for Context<'_, T> { self.app.read_entity(handle, read) } + #[inline] fn update_window(&mut self, window: AnyWindowHandle, update: F) -> Result where F: FnOnce(AnyView, &mut Window, &mut App) -> R, @@ -785,6 +792,7 @@ impl AppContext for Context<'_, T> { self.app.update_window(window, update) } + #[inline] fn read_window( &self, window: &WindowHandle, @@ -796,6 +804,7 @@ impl AppContext for Context<'_, T> { self.app.read_window(window, read) } + #[inline] fn background_spawn(&self, future: impl Future + Send + 'static) -> Task where R: Send + 'static, @@ -803,6 +812,7 @@ impl AppContext for Context<'_, T> { self.app.background_executor.spawn(future) } + #[inline] fn read_global(&self, callback: impl FnOnce(&G, &App) -> R) -> Self::Result where G: Global, diff --git a/crates/gpui/src/app/entity_map.rs b/crates/gpui/src/app/entity_map.rs index bea98cb06a5f80fc8141a52bc47f48e8734b40c9..81dbfdbf5733eed92a77fc2dc18fb971bd9bd4a7 100644 --- a/crates/gpui/src/app/entity_map.rs +++ b/crates/gpui/src/app/entity_map.rs @@ -244,11 +244,13 @@ impl AnyEntity { } /// Returns the id associated with this entity. + #[inline] pub fn entity_id(&self) -> EntityId { self.entity_id } /// Returns the [TypeId] associated with this entity. + #[inline] pub fn entity_type(&self) -> TypeId { self.entity_type } @@ -332,18 +334,21 @@ impl Drop for AnyEntity { } impl From> for AnyEntity { + #[inline] fn from(entity: Entity) -> Self { entity.any_entity } } impl Hash for AnyEntity { + #[inline] fn hash(&self, state: &mut H) { self.entity_id.hash(state); } } impl PartialEq for AnyEntity { + #[inline] fn eq(&self, other: &Self) -> bool { self.entity_id == other.entity_id } @@ -352,12 +357,14 @@ impl PartialEq for AnyEntity { impl Eq for AnyEntity {} impl Ord for AnyEntity { + #[inline] fn cmp(&self, other: &Self) -> Ordering { self.entity_id.cmp(&other.entity_id) } } impl PartialOrd for AnyEntity { + #[inline] fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } @@ -384,6 +391,7 @@ pub struct Entity { impl Sealed for Entity {} impl Entity { + #[inline] fn new(id: EntityId, entity_map: Weak>) -> Self where T: 'static, @@ -395,11 +403,13 @@ impl Entity { } /// Get the entity ID associated with this entity + #[inline] pub fn entity_id(&self) -> EntityId { self.any_entity.entity_id } /// Downgrade this entity pointer to a non-retaining weak pointer + #[inline] pub fn downgrade(&self) -> WeakEntity { WeakEntity { any_entity: self.any_entity.downgrade(), @@ -408,16 +418,19 @@ impl Entity { } /// Convert this into a dynamically typed entity. + #[inline] pub fn into_any(self) -> AnyEntity { self.any_entity } /// Grab a reference to this entity from the context. + #[inline] pub fn read<'a>(&self, cx: &'a App) -> &'a T { cx.entities.read(self) } /// Read the entity referenced by this handle with the given function. + #[inline] pub fn read_with( &self, cx: &C, @@ -427,6 +440,7 @@ impl Entity { } /// Updates the entity referenced by this handle with the given function. + #[inline] pub fn update( &self, cx: &mut C, @@ -436,6 +450,7 @@ impl Entity { } /// Updates the entity referenced by this handle with the given function. + #[inline] pub fn as_mut<'a, C: AppContext>(&self, cx: &'a mut C) -> C::Result> { cx.as_mut(self) } @@ -451,6 +466,7 @@ impl Entity { /// Updates the entity referenced by this handle with the given function if /// the referenced entity still exists, within a visual context that has a window. /// Returns an error if the entity has been released. + #[inline] pub fn update_in( &self, cx: &mut C, @@ -461,6 +477,7 @@ impl Entity { } impl Clone for Entity { + #[inline] fn clone(&self) -> Self { Self { any_entity: self.any_entity.clone(), @@ -479,12 +496,14 @@ impl std::fmt::Debug for Entity { } impl Hash for Entity { + #[inline] fn hash(&self, state: &mut H) { self.any_entity.hash(state); } } impl PartialEq for Entity { + #[inline] fn eq(&self, other: &Self) -> bool { self.any_entity == other.any_entity } @@ -493,18 +512,21 @@ impl PartialEq for Entity { impl Eq for Entity {} impl PartialEq> for Entity { + #[inline] fn eq(&self, other: &WeakEntity) -> bool { self.any_entity.entity_id() == other.entity_id() } } impl Ord for Entity { + #[inline] fn cmp(&self, other: &Self) -> std::cmp::Ordering { self.entity_id().cmp(&other.entity_id()) } } impl PartialOrd for Entity { + #[inline] fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } @@ -520,6 +542,7 @@ pub struct AnyWeakEntity { impl AnyWeakEntity { /// Get the entity ID associated with this weak reference. + #[inline] pub fn entity_id(&self) -> EntityId { self.entity_id } @@ -618,18 +641,21 @@ impl std::fmt::Debug for AnyWeakEntity { } impl From> for AnyWeakEntity { + #[inline] fn from(entity: WeakEntity) -> Self { entity.any_entity } } impl Hash for AnyWeakEntity { + #[inline] fn hash(&self, state: &mut H) { self.entity_id.hash(state); } } impl PartialEq for AnyWeakEntity { + #[inline] fn eq(&self, other: &Self) -> bool { self.entity_id == other.entity_id } @@ -638,12 +664,14 @@ impl PartialEq for AnyWeakEntity { impl Eq for AnyWeakEntity {} impl Ord for AnyWeakEntity { + #[inline] fn cmp(&self, other: &Self) -> Ordering { self.entity_id.cmp(&other.entity_id) } } impl PartialOrd for AnyWeakEntity { + #[inline] fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } @@ -740,6 +768,7 @@ impl WeakEntity { } /// Create a new weak entity that can never be upgraded. + #[inline] pub fn new_invalid() -> Self { Self { any_entity: AnyWeakEntity::new_invalid(), @@ -749,12 +778,14 @@ impl WeakEntity { } impl Hash for WeakEntity { + #[inline] fn hash(&self, state: &mut H) { self.any_entity.hash(state); } } impl PartialEq for WeakEntity { + #[inline] fn eq(&self, other: &Self) -> bool { self.any_entity == other.any_entity } @@ -763,18 +794,21 @@ impl PartialEq for WeakEntity { impl Eq for WeakEntity {} impl PartialEq> for WeakEntity { + #[inline] fn eq(&self, other: &Entity) -> bool { self.entity_id() == other.any_entity.entity_id() } } impl Ord for WeakEntity { + #[inline] fn cmp(&self, other: &Self) -> Ordering { self.entity_id().cmp(&other.entity_id()) } } impl PartialOrd for WeakEntity { + #[inline] fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } diff --git a/crates/text/src/text.rs b/crates/text/src/text.rs index e476103879d700dc6121882055bc7e2cabf3ed5a..fe9fe26f1bcc89b66753703e03f0a8bfeec628bd 100644 --- a/crates/text/src/text.rs +++ b/crates/text/src/text.rs @@ -3129,13 +3129,13 @@ pub trait ToOffset { } impl ToOffset for Point { + #[inline] fn to_offset(&self, snapshot: &BufferSnapshot) -> usize { snapshot.point_to_offset(*self) } } impl ToOffset for usize { - #[track_caller] fn to_offset(&self, snapshot: &BufferSnapshot) -> usize { assert!( *self <= snapshot.len(), @@ -3148,24 +3148,28 @@ impl ToOffset for usize { } impl ToOffset for Anchor { + #[inline] fn to_offset(&self, snapshot: &BufferSnapshot) -> usize { snapshot.summary_for_anchor(self) } } impl ToOffset for &T { + #[inline] fn to_offset(&self, content: &BufferSnapshot) -> usize { (*self).to_offset(content) } } impl ToOffset for PointUtf16 { + #[inline] fn to_offset(&self, snapshot: &BufferSnapshot) -> usize { snapshot.point_utf16_to_offset(*self) } } impl ToOffset for Unclipped { + #[inline] fn to_offset(&self, snapshot: &BufferSnapshot) -> usize { snapshot.unclipped_point_utf16_to_offset(*self) } @@ -3176,24 +3180,28 @@ pub trait ToPoint { } impl ToPoint for Anchor { + #[inline] fn to_point(&self, snapshot: &BufferSnapshot) -> Point { snapshot.summary_for_anchor(self) } } impl ToPoint for usize { + #[inline] fn to_point(&self, snapshot: &BufferSnapshot) -> Point { snapshot.offset_to_point(*self) } } impl ToPoint for Point { + #[inline] fn to_point(&self, _: &BufferSnapshot) -> Point { *self } } impl ToPoint for Unclipped { + #[inline] fn to_point(&self, snapshot: &BufferSnapshot) -> Point { snapshot.unclipped_point_utf16_to_point(*self) } @@ -3204,24 +3212,28 @@ pub trait ToPointUtf16 { } impl ToPointUtf16 for Anchor { + #[inline] fn to_point_utf16(&self, snapshot: &BufferSnapshot) -> PointUtf16 { snapshot.summary_for_anchor(self) } } impl ToPointUtf16 for usize { + #[inline] fn to_point_utf16(&self, snapshot: &BufferSnapshot) -> PointUtf16 { snapshot.offset_to_point_utf16(*self) } } impl ToPointUtf16 for PointUtf16 { + #[inline] fn to_point_utf16(&self, _: &BufferSnapshot) -> PointUtf16 { *self } } impl ToPointUtf16 for Point { + #[inline] fn to_point_utf16(&self, snapshot: &BufferSnapshot) -> PointUtf16 { snapshot.point_to_point_utf16(*self) } @@ -3232,18 +3244,21 @@ pub trait ToOffsetUtf16 { } impl ToOffsetUtf16 for Anchor { + #[inline] fn to_offset_utf16(&self, snapshot: &BufferSnapshot) -> OffsetUtf16 { snapshot.summary_for_anchor(self) } } impl ToOffsetUtf16 for usize { + #[inline] fn to_offset_utf16(&self, snapshot: &BufferSnapshot) -> OffsetUtf16 { snapshot.offset_to_offset_utf16(*self) } } impl ToOffsetUtf16 for OffsetUtf16 { + #[inline] fn to_offset_utf16(&self, _snapshot: &BufferSnapshot) -> OffsetUtf16 { *self } @@ -3254,24 +3269,28 @@ pub trait FromAnchor { } impl FromAnchor for Anchor { + #[inline] fn from_anchor(anchor: &Anchor, _snapshot: &BufferSnapshot) -> Self { *anchor } } impl FromAnchor for Point { + #[inline] fn from_anchor(anchor: &Anchor, snapshot: &BufferSnapshot) -> Self { snapshot.summary_for_anchor(anchor) } } impl FromAnchor for PointUtf16 { + #[inline] fn from_anchor(anchor: &Anchor, snapshot: &BufferSnapshot) -> Self { snapshot.summary_for_anchor(anchor) } } impl FromAnchor for usize { + #[inline] fn from_anchor(anchor: &Anchor, snapshot: &BufferSnapshot) -> Self { snapshot.summary_for_anchor(anchor) }