Skip to content

Commit

Permalink
Pass shared PietText by ref instead of cloning
Browse files Browse the repository at this point in the history
Currently we create a copy of the PietText object every time we
need one. This changes that so that there is a single instance
in the ContextState, and the various contexts just pass out
a reference to this object as needed.

This should have few practical implications, except that in
trees with lots of text we will save a few microseconds. It's
mostly an API improvement, since the methods that tend to use
a PietText object generally want it passed by ref anyway.
  • Loading branch information
cmyr committed Sep 9, 2020
1 parent 8bd36c2 commit 75fe81c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
6 changes: 4 additions & 2 deletions druid/src/contexts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub(crate) struct ContextState<'a> {
pub(crate) ext_handle: &'a ExtEventSink,
pub(crate) window_id: WindowId,
pub(crate) window: &'a WindowHandle,
pub(crate) text: PietText,
/// The id of the widget that currently has focus.
pub(crate) focus_widget: Option<WidgetId>,
pub(crate) root_app_data_type: TypeId,
Expand Down Expand Up @@ -154,8 +155,8 @@ impl_context_method!(
}

/// Get an object which can create text layouts.
pub fn text(&self) -> PietText {
self.state.window.text()
pub fn text(&mut self) -> &mut PietText {
&mut self.state.text
}
}
);
Expand Down Expand Up @@ -675,6 +676,7 @@ impl<'a> ContextState<'a> {
window,
window_id,
focus_widget,
text: window.text(),
root_app_data_type: TypeId::of::<T>(),
}
}
Expand Down
16 changes: 8 additions & 8 deletions druid/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -986,16 +986,16 @@ mod tests {

let mut command_queue: CommandQueue = VecDeque::new();
let mut widget_state = WidgetState::new(WidgetId::next(), None);
let window = WindowHandle::default();
let ext_host = ExtEventHost::default();
let ext_handle = ext_host.make_sink();
let mut state = ContextState {
command_queue: &mut command_queue,
ext_handle: &ext_handle,
window_id: WindowId::next(),
window: &WindowHandle::default(),
root_app_data_type: std::any::TypeId::of::<Option<u32>>(),
focus_widget: None,
};
let mut state = ContextState::new::<Option<u32>>(
&mut command_queue,
&ext_handle,
&window,
WindowId::next(),
None,
);

let mut ctx = LifeCycleCtx {
widget_state: &mut widget_state,
Expand Down

0 comments on commit 75fe81c

Please sign in to comment.