Reduce string allocations in Editor::insert

Co-authored-by: Nathan Sobo <nathan@zed.dev>
Co-authored-by: Keith Simmons <keith@zed.dev>
This commit is contained in:
Max Brunsfeld 2022-04-29 10:07:21 -07:00
parent 920601cb94
commit 2c2ca1bfbd

View file

@ -1945,6 +1945,7 @@ impl Editor {
} }
pub fn insert(&mut self, text: &str, cx: &mut ViewContext<Self>) { pub fn insert(&mut self, text: &str, cx: &mut ViewContext<Self>) {
let text: Arc<str> = text.into();
self.transact(cx, |this, cx| { self.transact(cx, |this, cx| {
let old_selections = this.local_selections::<usize>(cx); let old_selections = this.local_selections::<usize>(cx);
let selection_anchors = this.buffer.update(cx, |buffer, cx| { let selection_anchors = this.buffer.update(cx, |buffer, cx| {
@ -1956,7 +1957,9 @@ impl Editor {
.collect::<Vec<_>>() .collect::<Vec<_>>()
}; };
buffer.edit_with_autoindent( buffer.edit_with_autoindent(
old_selections.iter().map(|s| (s.start..s.end, text)), old_selections
.iter()
.map(|s| (s.start..s.end, text.clone())),
cx, cx,
); );
anchors anchors