Skip to content

Commit

Permalink
perf: introduce copy-on-write for event system to eliminate all memor…
Browse files Browse the repository at this point in the history
…y reallocations (#1962)
  • Loading branch information
sxyazi authored Nov 27, 2024
1 parent 5e48df5 commit 37292ad
Show file tree
Hide file tree
Showing 122 changed files with 532 additions and 448 deletions.
5 changes: 1 addition & 4 deletions yazi-config/src/keymap/chord.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{borrow::Cow, collections::VecDeque, hash::{Hash, Hasher}, sync::OnceLock};
use std::{borrow::Cow, hash::{Hash, Hasher}, sync::OnceLock};

use regex::Regex;
use serde::Deserialize;
Expand Down Expand Up @@ -53,7 +53,4 @@ impl Chord {
|| self.run().to_lowercase().contains(&s)
|| self.on().to_lowercase().contains(&s)
}

#[inline]
pub fn to_seq(&self) -> VecDeque<Cmd> { self.run.iter().map(|c| c.shallow_clone()).collect() }
}
16 changes: 8 additions & 8 deletions yazi-config/src/keymap/cow.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{collections::VecDeque, ops::Deref};

use yazi_shared::event::Cmd;
use yazi_shared::event::CmdCow;

use super::Chord;

Expand All @@ -10,14 +10,14 @@ pub enum ChordCow {
Borrowed(&'static Chord),
}

impl From<&'static Chord> for ChordCow {
fn from(c: &'static Chord) -> Self { Self::Borrowed(c) }
}

impl From<Chord> for ChordCow {
fn from(c: Chord) -> Self { Self::Owned(c) }
}

impl From<&'static Chord> for ChordCow {
fn from(c: &'static Chord) -> Self { Self::Borrowed(c) }
}

impl Deref for ChordCow {
type Target = Chord;

Expand All @@ -34,10 +34,10 @@ impl Default for ChordCow {
}

impl ChordCow {
pub fn into_seq(self) -> VecDeque<Cmd> {
pub fn into_seq(self) -> VecDeque<CmdCow> {
match self {
Self::Owned(c) => c.run.into(),
Self::Borrowed(c) => c.to_seq(),
Self::Owned(c) => c.run.into_iter().map(|c| c.into()).collect(),
Self::Borrowed(c) => c.run.iter().map(|c| c.into()).collect(),
}
}
}
6 changes: 3 additions & 3 deletions yazi-core/src/completion/commands/arrow.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use yazi_macro::render;
use yazi_shared::event::{Cmd, Data};
use yazi_shared::event::{CmdCow, Data};

use crate::completion::Completion;

struct Opt {
step: isize,
}

impl From<Cmd> for Opt {
fn from(c: Cmd) -> Self { Self { step: c.first().and_then(Data::as_isize).unwrap_or(0) } }
impl From<CmdCow> for Opt {
fn from(c: CmdCow) -> Self { Self { step: c.first().and_then(Data::as_isize).unwrap_or(0) } }
}

impl Completion {
Expand Down
6 changes: 3 additions & 3 deletions yazi-core/src/completion/commands/close.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use yazi_macro::render;
use yazi_proxy::InputProxy;
use yazi_shared::event::Cmd;
use yazi_shared::event::CmdCow;

use crate::completion::Completion;

struct Opt {
submit: bool,
}

impl From<Cmd> for Opt {
fn from(c: Cmd) -> Self { Self { submit: c.bool("submit") } }
impl From<CmdCow> for Opt {
fn from(c: CmdCow) -> Self { Self { submit: c.bool("submit") } }
}
impl From<bool> for Opt {
fn from(submit: bool) -> Self { Self { submit } }
Expand Down
20 changes: 12 additions & 8 deletions yazi-core/src/completion/commands/show.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
use std::{mem, ops::ControlFlow};
use std::{borrow::Cow, mem, ops::ControlFlow};

use yazi_macro::render;
use yazi_shared::event::{Cmd, Data};
use yazi_shared::event::{Cmd, CmdCow, Data};

use crate::completion::Completion;

const LIMIT: usize = 30;

struct Opt {
cache: Vec<String>,
cache_name: String,
word: String,
cache_name: Cow<'static, str>,
word: Cow<'static, str>,
ticket: usize,
}

impl From<Cmd> for Opt {
fn from(mut c: Cmd) -> Self {
impl From<CmdCow> for Opt {
fn from(mut c: CmdCow) -> Self {
Self {
cache: c.take_any("cache").unwrap_or_default(),
cache_name: c.take_str("cache-name").unwrap_or_default(),
Expand All @@ -25,6 +25,10 @@ impl From<Cmd> for Opt {
}
}

impl From<Cmd> for Opt {
fn from(c: Cmd) -> Self { Self::from(CmdCow::from(c)) }
}

impl Completion {
#[yazi_codegen::command]
pub fn show(&mut self, opt: Opt) {
Expand All @@ -33,9 +37,9 @@ impl Completion {
}

if !opt.cache.is_empty() {
self.caches.insert(opt.cache_name.to_owned(), opt.cache);
self.caches.insert(opt.cache_name.as_ref().to_owned(), opt.cache);
}
let Some(cache) = self.caches.get(&opt.cache_name) else {
let Some(cache) = self.caches.get(opt.cache_name.as_ref()) else {
return;
};

Expand Down
10 changes: 5 additions & 5 deletions yazi-core/src/completion/commands/trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{borrow::Cow, mem, path::{MAIN_SEPARATOR, MAIN_SEPARATOR_STR}};

use tokio::fs;
use yazi_macro::{emit, render};
use yazi_shared::{Layer, event::{Cmd, Data}};
use yazi_shared::{Layer, event::{Cmd, CmdCow, Data}};

use crate::completion::Completion;

Expand All @@ -13,12 +13,12 @@ const SEPARATOR: [char; 2] = ['/', '\\'];
const SEPARATOR: char = std::path::MAIN_SEPARATOR;

struct Opt {
word: String,
word: Cow<'static, str>,
ticket: usize,
}

impl From<Cmd> for Opt {
fn from(mut c: Cmd) -> Self {
impl From<CmdCow> for Opt {
fn from(mut c: CmdCow) -> Self {
Self {
word: c.take_first_str().unwrap_or_default(),
ticket: c.get("ticket").and_then(Data::as_usize).unwrap_or(0),
Expand All @@ -40,7 +40,7 @@ impl Completion {

if self.caches.contains_key(&parent) {
return self.show(
Cmd::new("show").with("cache-name", parent).with("word", child).with("ticket", opt.ticket),
Cmd::default().with("cache-name", parent).with("word", child).with("ticket", opt.ticket),
);
}

Expand Down
6 changes: 3 additions & 3 deletions yazi-core/src/confirm/commands/arrow.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use yazi_macro::render;
use yazi_shared::event::{Cmd, Data};
use yazi_shared::event::{CmdCow, Data};

use crate::{confirm::Confirm, manager::Manager};

struct Opt {
step: isize,
}

impl From<Cmd> for Opt {
fn from(c: Cmd) -> Self { Self { step: c.first().and_then(Data::as_isize).unwrap_or(0) } }
impl From<CmdCow> for Opt {
fn from(c: CmdCow) -> Self { Self { step: c.first().and_then(Data::as_isize).unwrap_or(0) } }
}

impl Confirm {
Expand Down
6 changes: 3 additions & 3 deletions yazi-core/src/confirm/commands/close.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use yazi_macro::render;
use yazi_shared::event::Cmd;
use yazi_shared::event::CmdCow;

use crate::confirm::Confirm;

struct Opt {
submit: bool,
}

impl From<Cmd> for Opt {
fn from(c: Cmd) -> Self { Self { submit: c.bool("submit") } }
impl From<CmdCow> for Opt {
fn from(c: CmdCow) -> Self { Self { submit: c.bool("submit") } }
}
impl From<bool> for Opt {
fn from(submit: bool) -> Self { Self { submit } }
Expand Down
6 changes: 3 additions & 3 deletions yazi-core/src/confirm/commands/show.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use tokio::sync::oneshot;
use yazi_config::popup::ConfirmCfg;
use yazi_macro::render;
use yazi_shared::event::Cmd;
use yazi_shared::event::CmdCow;

use crate::confirm::Confirm;

Expand All @@ -10,10 +10,10 @@ pub struct Opt {
tx: oneshot::Sender<bool>,
}

impl TryFrom<Cmd> for Opt {
impl TryFrom<CmdCow> for Opt {
type Error = ();

fn try_from(mut c: Cmd) -> Result<Self, Self::Error> {
fn try_from(mut c: CmdCow) -> Result<Self, Self::Error> {
Ok(Self { cfg: c.take_any("cfg").ok_or(())?, tx: c.take_any("tx").ok_or(())? })
}
}
Expand Down
6 changes: 3 additions & 3 deletions yazi-core/src/help/commands/arrow.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use yazi_macro::render;
use yazi_shared::event::{Cmd, Data};
use yazi_shared::event::{CmdCow, Data};

use crate::help::Help;

struct Opt {
step: isize,
}

impl From<Cmd> for Opt {
fn from(c: Cmd) -> Self { Self { step: c.first().and_then(Data::as_isize).unwrap_or(0) } }
impl From<CmdCow> for Opt {
fn from(c: CmdCow) -> Self { Self { step: c.first().and_then(Data::as_isize).unwrap_or(0) } }
}
impl From<isize> for Opt {
fn from(step: isize) -> Self { Self { step } }
Expand Down
4 changes: 2 additions & 2 deletions yazi-core/src/help/commands/escape.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use yazi_macro::render;
use yazi_shared::event::Cmd;
use yazi_shared::event::CmdCow;

use crate::help::Help;

impl Help {
pub fn escape(&mut self, _: Cmd) {
pub fn escape(&mut self, _: CmdCow) {
if self.keyword().is_none() {
return self.toggle(self.layer);
}
Expand Down
4 changes: 2 additions & 2 deletions yazi-core/src/help/commands/filter.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use yazi_config::popup::{Offset, Origin, Position};
use yazi_macro::render;
use yazi_shared::event::Cmd;
use yazi_shared::event::CmdCow;

use crate::{help::Help, input::Input};

impl Help {
pub fn filter(&mut self, _: Cmd) {
pub fn filter(&mut self, _: CmdCow) {
let mut input = Input::default();
input.position = Position::new(Origin::BottomLeft, Offset::line());

Expand Down
6 changes: 3 additions & 3 deletions yazi-core/src/input/commands/backspace.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use yazi_macro::render;
use yazi_shared::event::Cmd;
use yazi_shared::event::CmdCow;

use crate::input::Input;

struct Opt {
under: bool,
}

impl From<Cmd> for Opt {
fn from(c: Cmd) -> Self { Self { under: c.bool("under") } }
impl From<CmdCow> for Opt {
fn from(c: CmdCow) -> Self { Self { under: c.bool("under") } }
}
impl From<bool> for Opt {
fn from(under: bool) -> Self { Self { under } }
Expand Down
4 changes: 2 additions & 2 deletions yazi-core/src/input/commands/backward.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use yazi_shared::{CharKind, event::Cmd};
use yazi_shared::{CharKind, event::CmdCow};

use crate::input::Input;

impl Input {
pub fn backward(&mut self, _: Cmd) {
pub fn backward(&mut self, _: CmdCow) {
let snap = self.snap();
if snap.cursor == 0 {
return self.move_(0);
Expand Down
6 changes: 3 additions & 3 deletions yazi-core/src/input/commands/close.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use yazi_macro::render;
use yazi_proxy::CompletionProxy;
use yazi_shared::{errors::InputError, event::Cmd};
use yazi_shared::{errors::InputError, event::CmdCow};

use crate::input::Input;

struct Opt {
submit: bool,
}

impl From<Cmd> for Opt {
fn from(c: Cmd) -> Self { Self { submit: c.bool("submit") } }
impl From<CmdCow> for Opt {
fn from(c: CmdCow) -> Self { Self { submit: c.bool("submit") } }
}
impl From<bool> for Opt {
fn from(submit: bool) -> Self { Self { submit } }
Expand Down
10 changes: 5 additions & 5 deletions yazi-core/src/input/commands/complete.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::path::MAIN_SEPARATOR_STR;
use std::{borrow::Cow, path::MAIN_SEPARATOR_STR};

use yazi_macro::render;
use yazi_shared::event::{Cmd, Data};
use yazi_shared::event::{CmdCow, Data};

use crate::input::Input;

Expand All @@ -12,12 +12,12 @@ const SEPARATOR: [char; 2] = ['/', '\\'];
const SEPARATOR: char = std::path::MAIN_SEPARATOR;

struct Opt {
word: String,
word: Cow<'static, str>,
ticket: usize,
}

impl From<Cmd> for Opt {
fn from(mut c: Cmd) -> Self {
impl From<CmdCow> for Opt {
fn from(mut c: CmdCow) -> Self {
Self {
word: c.take_first_str().unwrap_or_default(),
ticket: c.get("ticket").and_then(Data::as_usize).unwrap_or(0),
Expand Down
6 changes: 3 additions & 3 deletions yazi-core/src/input/commands/delete.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use yazi_macro::render;
use yazi_shared::event::Cmd;
use yazi_shared::event::CmdCow;

use crate::input::{Input, op::InputOp};

Expand All @@ -8,8 +8,8 @@ struct Opt {
insert: bool,
}

impl From<Cmd> for Opt {
fn from(c: Cmd) -> Self { Self { cut: c.bool("cut"), insert: c.bool("insert") } }
impl From<CmdCow> for Opt {
fn from(c: CmdCow) -> Self { Self { cut: c.bool("cut"), insert: c.bool("insert") } }
}

impl Input {
Expand Down
6 changes: 3 additions & 3 deletions yazi-core/src/input/commands/escape.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use yazi_macro::render;
use yazi_proxy::CompletionProxy;
use yazi_shared::event::Cmd;
use yazi_shared::event::CmdCow;

use crate::input::{Input, InputMode, op::InputOp};

struct Opt;

impl From<Cmd> for Opt {
fn from(_: Cmd) -> Self { Self }
impl From<CmdCow> for Opt {
fn from(_: CmdCow) -> Self { Self }
}
impl From<()> for Opt {
fn from(_: ()) -> Self { Self }
Expand Down
Loading

0 comments on commit 37292ad

Please sign in to comment.