perf: do not pre-allocate memory for Lua tables (#2879)

This commit is contained in:
三咲雅 misaki masa 2025-06-16 11:19:18 +08:00 committed by GitHub
parent 917fee939f
commit 51560c30b7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 16 additions and 17 deletions

View file

@ -20,7 +20,7 @@ impl TryFrom<CmdCow> for Opt {
impl Confirm { impl Confirm {
pub fn show(&mut self, opt: impl TryInto<Opt>) { pub fn show(&mut self, opt: impl TryInto<Opt>) {
let Ok(opt) = opt.try_into() else { let Ok(opt): Result<Opt, _> = opt.try_into() else {
return; return;
}; };

View file

@ -25,9 +25,8 @@ impl Widget for Confirm<'_> {
.title_alignment(Alignment::Center) .title_alignment(Alignment::Center)
.render(area, buf); .render(area, buf);
let content = confirm.content.clone();
let content_border = confirm.list.line_count(area.width) != 0; let content_border = confirm.list.line_count(area.width) != 0;
let content_height = content.line_count(area.width) as u16; let content_height = confirm.content.line_count(area.width) as u16;
let chunks = Layout::vertical([ let chunks = Layout::vertical([
Constraint::Length(if content_height == 0 { Constraint::Length(if content_height == 0 {

View file

@ -3,7 +3,7 @@ use mlua::{IntoLua, Lua, MetaMethod, Table, Value};
pub struct Composer; pub struct Composer;
impl Composer { impl Composer {
pub fn make<F>(lua: &Lua, cap: usize, f: F) -> mlua::Result<Value> pub fn make<F>(lua: &Lua, f: F) -> mlua::Result<Value>
where where
F: Fn(&Lua, &[u8]) -> mlua::Result<Value> + 'static, F: Fn(&Lua, &[u8]) -> mlua::Result<Value> + 'static,
{ {
@ -13,7 +13,7 @@ impl Composer {
Ok(v) Ok(v)
})?; })?;
let tbl = lua.create_table_with_capacity(0, cap)?; let tbl = lua.create_table()?;
tbl.set_metatable(Some(lua.create_table_from([(MetaMethod::Index.name(), index)])?)); tbl.set_metatable(Some(lua.create_table_from([(MetaMethod::Index.name(), index)])?));
tbl.into_lua(lua) tbl.into_lua(lua)
} }

View file

@ -8,7 +8,7 @@ pub(super) struct Plugin;
impl Plugin { impl Plugin {
pub(super) fn compose(lua: &Lua) -> mlua::Result<Value> { pub(super) fn compose(lua: &Lua) -> mlua::Result<Value> {
Composer::make(lua, 5, |lua, key| { Composer::make(lua, |lua, key| {
match key { match key {
b"fetchers" => Plugin::fetchers(lua)?, b"fetchers" => Plugin::fetchers(lua)?,
b"spotter" => Plugin::spotter(lua)?, b"spotter" => Plugin::spotter(lua)?,

View file

@ -12,7 +12,7 @@ pub struct Runtime;
impl Runtime { impl Runtime {
pub fn compose(lua: &Lua) -> mlua::Result<Value> { pub fn compose(lua: &Lua) -> mlua::Result<Value> {
Composer::make(lua, 10, |lua, key| { Composer::make(lua, |lua, key| {
match key { match key {
b"args" => Self::args(lua)?, b"args" => Self::args(lua)?,
b"term" => super::Term::compose(lua)?, b"term" => super::Term::compose(lua)?,
@ -27,7 +27,7 @@ impl Runtime {
} }
fn args(lua: &Lua) -> mlua::Result<Value> { fn args(lua: &Lua) -> mlua::Result<Value> {
Composer::make(lua, 5, |lua, key| match key { Composer::make(lua, |lua, key| match key {
b"entries" => lua.create_sequence_from(ARGS.entries.iter().map(Url::new))?.into_lua(lua), b"entries" => lua.create_sequence_from(ARGS.entries.iter().map(Url::new))?.into_lua(lua),
b"cwd_file" => ARGS.cwd_file.as_ref().map(Url::new).into_lua(lua), b"cwd_file" => ARGS.cwd_file.as_ref().map(Url::new).into_lua(lua),
b"chooser_file" => ARGS.chooser_file.as_ref().map(Url::new).into_lua(lua), b"chooser_file" => ARGS.chooser_file.as_ref().map(Url::new).into_lua(lua),
@ -36,7 +36,7 @@ impl Runtime {
} }
fn mgr(lua: &Lua) -> mlua::Result<Value> { fn mgr(lua: &Lua) -> mlua::Result<Value> {
Composer::make(lua, 15, |lua, key| { Composer::make(lua, |lua, key| {
let m = &YAZI.mgr; let m = &YAZI.mgr;
match key { match key {
b"ratio" => lua.to_value_with(&m.ratio, OPTS)?, b"ratio" => lua.to_value_with(&m.ratio, OPTS)?,
@ -60,7 +60,7 @@ impl Runtime {
} }
fn preview(lua: &Lua) -> mlua::Result<Value> { fn preview(lua: &Lua) -> mlua::Result<Value> {
Composer::make(lua, 15, |lua, key| { Composer::make(lua, |lua, key| {
let p = &YAZI.preview; let p = &YAZI.preview;
match key { match key {
b"wrap" => lua.to_value_with(&p.wrap, OPTS)?, b"wrap" => lua.to_value_with(&p.wrap, OPTS)?,
@ -83,7 +83,7 @@ impl Runtime {
} }
fn tasks(lua: &Lua) -> mlua::Result<Value> { fn tasks(lua: &Lua) -> mlua::Result<Value> {
Composer::make(lua, 10, |lua, key| { Composer::make(lua, |lua, key| {
let t = &YAZI.tasks; let t = &YAZI.tasks;
match key { match key {
b"micro_workers" => lua.to_value_with(&t.micro_workers, OPTS)?, b"micro_workers" => lua.to_value_with(&t.micro_workers, OPTS)?,

View file

@ -7,7 +7,7 @@ pub(super) struct Term;
impl Term { impl Term {
pub(super) fn compose(lua: &Lua) -> mlua::Result<Value> { pub(super) fn compose(lua: &Lua) -> mlua::Result<Value> {
Composer::make(lua, 5, |lua, key| match key { Composer::make(lua, |lua, key| match key {
b"light" => EMULATOR.get().light.into_lua(lua), b"light" => EMULATOR.get().light.into_lua(lua),
b"cell_size" => Self::cell_size(lua)?.into_lua(lua), b"cell_size" => Self::cell_size(lua)?.into_lua(lua),
_ => Ok(Value::Nil), _ => Ok(Value::Nil),

View file

@ -8,7 +8,7 @@ pub struct Theme;
impl Theme { impl Theme {
pub fn compose(lua: &Lua) -> mlua::Result<Value> { pub fn compose(lua: &Lua) -> mlua::Result<Value> {
Composer::make(lua, 15, |lua, key| { Composer::make(lua, |lua, key| {
match key { match key {
b"mgr" => lua.to_value_with(&THEME.mgr, OPTS)?, b"mgr" => lua.to_value_with(&THEME.mgr, OPTS)?,
b"tabs" => lua.to_value_with(&THEME.tabs, OPTS)?, b"tabs" => lua.to_value_with(&THEME.tabs, OPTS)?,

View file

@ -5,7 +5,7 @@ use super::Renderable;
use crate::Composer; use crate::Composer;
pub fn compose(lua: &Lua) -> mlua::Result<Value> { pub fn compose(lua: &Lua) -> mlua::Result<Value> {
Composer::make(lua, 20, |lua, key| { Composer::make(lua, |lua, key| {
match key { match key {
b"Align" => super::Align::compose(lua)?, b"Align" => super::Align::compose(lua)?,
b"Bar" => super::Bar::compose(lua)?, b"Bar" => super::Bar::compose(lua)?,

View file

@ -7,7 +7,7 @@ use yazi_fs::{mounts::PARTITIONS, remove_dir_clean};
use crate::{Composer, bindings::{Cha, SizeCalculator}, file::File}; use crate::{Composer, bindings::{Cha, SizeCalculator}, file::File};
pub fn compose(lua: &Lua) -> mlua::Result<Value> { pub fn compose(lua: &Lua) -> mlua::Result<Value> {
Composer::make(lua, 10, |lua, key| { Composer::make(lua, |lua, key| {
match key { match key {
b"op" => op(lua)?, b"op" => op(lua)?,
b"cwd" => cwd(lua)?, b"cwd" => cwd(lua)?,

View file

@ -7,7 +7,7 @@ use crate::Composer;
yazi_macro::mod_flat!(pubsub); yazi_macro::mod_flat!(pubsub);
pub(super) fn compose(lua: &Lua) -> mlua::Result<Value> { pub(super) fn compose(lua: &Lua) -> mlua::Result<Value> {
Composer::make(lua, 10, |lua, key| { Composer::make(lua, |lua, key| {
match key { match key {
b"pub" => Pubsub::r#pub(lua)?, b"pub" => Pubsub::r#pub(lua)?,
b"pub_to" => Pubsub::pub_to(lua)?, b"pub_to" => Pubsub::pub_to(lua)?,

View file

@ -5,7 +5,7 @@ use crate::Composer;
pub(super) struct Utils; pub(super) struct Utils;
pub fn compose(lua: &Lua, isolate: bool) -> mlua::Result<Value> { pub fn compose(lua: &Lua, isolate: bool) -> mlua::Result<Value> {
Composer::make(lua, 45, move |lua, key| { Composer::make(lua, move |lua, key| {
match key { match key {
// App // App
b"id" => Utils::id(lua)?, b"id" => Utils::id(lua)?,